Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Other:
- `mix graphql.schema` — writes the schema SDL to `priv/graphql/schema.graphql`. A test fails when that file is stale, and the frontend GraphQL check reads it, so run this after every schema change and commit the result.
- `bin/setup` — first-time bootstrap.

Credentials live in `config/dev.secret.exs` (gitignored, optional: the fake stack boots without it) and `config/runtime.exs` via env vars (`NEW_RELIC_LICENSE_KEY`, `SENTRY_DSN`, supermarket auth). Don't commit those.
Credentials live in `config/dev.secret.exs` (gitignored, optional: the fake stack boots without it) and `config/runtime.exs` via env vars (`NEW_RELIC_LICENSE_KEY`, `SENTRY_DSN`, supermarket auth, `PICNIC_AUTH_KEY` plus optional `PICNIC_BASE_URL`/`PICNIC_AGENT`/`PICNIC_DID`/`PICNIC_USER_AGENT` for picnic mode). Don't commit those.

## Deploy

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ It is a route in the Phoenix router, so it is up whenever the app is, on the dat
| --- | --- |
| `search_ingredients` | Search the ingredients Picape knows, by name |
| `search_supermarket` | Search the supermarket's product catalogue |
| `search_picnic` | Search the Picnic product catalogue |
| `map_ingredient_picnic` | Set or clear the Picnic product behind an ingredient |
| `get_shopping_list` | Read the current order: items, quantities, totals, availability and the freeze state |
| `get_previous_orders` | Read closed orders: items, quantities and when each closed, including products added in the supermarket's own app |
| `set_ingredient_quantity` | Put an ingredient on the list, change it, or remove it with quantity 0 |
| `set_mode` | Switch where the list lives: the supermarket basket or Picape's own checklist |
| `set_mode` | Switch where the list lives: the supermarket basket, the Picnic cart or Picape's own checklist |
| `check_item` | Tick an item off the checklist, or untick it |
| `add_ingredient` | Teach Picape an ingredient, backed by a supermarket product ID |
| `edit_ingredient` | Change an ingredient, for example onto another supermarket product |
Expand Down Expand Up @@ -84,7 +86,8 @@ Notes on the tools:
- `edit_ingredient` is the other way round: it changes only the fields you send and leaves the rest, tags included. When the supermarket drops a product, `search_ingredients` reports a `warning` and `edit_ingredient` moves the ingredient onto a replacement product, which keeps the recipes that use it.
- `set_ingredient_replacement` is different from repointing an ingredient: the ingredient keeps its product, and sync orders the replacement's product only while the wanted one cannot be delivered. The cart row then says what it stands in for (`substituted_for`).
- `get_shopping_list` marks products the supermarket cannot deliver right now with `is_orderable: false`; `only_unavailable: true` returns just those. While the supermarket processes a paid order the answer's `is_placed` is true and Picape freezes: `set_ingredient_quantity` and `plan_recipe` still record the intent, but nothing reaches the supermarket until the order is delivered.
- `get_shopping_list` also reports `mode`. `ah` means the list lives in the supermarket basket; `checklist` means it lives in Picape's own database and planning, ordering and ticking work without the supermarket. Switch with `set_mode`; switching back to `ah` pushes the accumulated list into the basket and the reply's `pushed_to_supermarket` says whether that worked. `check_item` ticks items only in checklist mode.
- `get_shopping_list` also reports `mode`, and `store` per item. `ah` means the list lives in the supermarket basket; `picnic` means it lives in the Picnic cart; `checklist` means it lives in Picape's own database and planning, ordering and ticking work without a supermarket. In picnic mode an ingredient without a Picnic mapping shows up as a `checklist` item. Switch with `set_mode`; switching to `ah` or `picnic` pushes the accumulated list into that basket and the reply's `pushed_to_supermarket` says whether that worked. `check_item` ticks items in checklist and picnic mode.
- Picnic mode syncs the plan into the Picnic cart for every ingredient that has a `picnic_product_id` (set with `map_ingredient_picnic`, found with `search_picnic`). Picape never checks out, pays or picks a delivery slot at Picnic. Ordering finishes in the Picnic app, and Picape cannot see that checkout: after the Picnic order is in, tap "Bestelling ontvangen" in Picape to close the order the way the supermarket's delivered order does. Picnic mode needs `PICNIC_AUTH_KEY` in the environment: the `x-picnic-auth` value the Picnic app itself sends on every request, taken from an app session. The storefront api has no refresh flow, so Picape uses the key as it is; when Picnic rejects it the calls fail and the list falls back to the checklist until a fresh key is configured. `PICNIC_BASE_URL`, `PICNIC_AGENT`, `PICNIC_DID` and `PICNIC_USER_AGENT` have defaults.
- `get_previous_orders` reads the basket contents Picape snapshot at the moment each order closed, so products that were added in the supermarket's own app appear with `ingredient_id: null` and `external: true`. Orders closed before snapshots existed fall back to what Picape's plan rows recorded, so their external items are lost.
- There is no delete for recipes or ingredients.
- The two history tools read finished orders only, nine years of them. The order being planned now is not history yet, an ingredient taken off a list again was never bought, and a recipe that was unplanned again was never cooked, so none of the three count.
Expand Down
16 changes: 16 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,29 @@ if config_env() == :prod do
"X-Clientname": System.get_env("SUPERMARKET_CLIENT_NAME"),
"User-Agent": System.get_env("SUPERMARKET_USER_AGENT")
]

config :picape, Picape.Picnic,
base_url: System.get_env("PICNIC_BASE_URL"),
auth_key: System.get_env("PICNIC_AUTH_KEY"),
agent: System.get_env("PICNIC_AGENT"),
did: System.get_env("PICNIC_DID"),
user_agent: System.get_env("PICNIC_USER_AGENT")
end

if config_env() == :dev do
if base_url = System.get_env("SUPERMARKET_BASE_URL") do
config :picape, Picape.Supermarket, base_url: base_url
end

if System.get_env("PICNIC_BASE_URL") || System.get_env("PICNIC_AUTH_KEY") do
config :picape, Picape.Picnic,
base_url: System.get_env("PICNIC_BASE_URL"),
auth_key: System.get_env("PICNIC_AUTH_KEY"),
agent: System.get_env("PICNIC_AGENT"),
did: System.get_env("PICNIC_DID"),
user_agent: System.get_env("PICNIC_USER_AGENT")
end

if System.get_env("SUPERMARKET_REFRESH") == "0" do
config :picape, Picape.Supermarket.KeepLogin, refresh: false
end
Expand Down
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ config :picape, Picape.Repo,
ownership_timeout: 60_000

config :picape, Picape.Supermarket, base_url: "http://localhost:4021", headers: []

config :picape, Picape.Picnic, base_url: "http://localhost:4021/api/15", auth_key: "test-auth-key"
Binary file modified e2e/tests/__screenshots__/ipad/shop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2e/tests/__screenshots__/iphone-standalone/shop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2e/tests/__screenshots__/iphone/shop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 29 additions & 6 deletions frontend/components/Search/SearchIngredients.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLazyQuery, gql } from '@apollo/client';
import { useLazyQuery, useQuery, gql } from '@apollo/client';
import { useNavigation } from '@react-navigation/native';
import { LinearGradient } from 'expo-linear-gradient';
import * as React from 'react';
Expand All @@ -15,7 +15,7 @@ import { ListItem } from '../ListItem/ListItem';
import SearchBar from '../Search/SearchBar';

const SEARCH_INGREDIENTS = gql`
query SearchIngredient($query: String!, $supermarket: Boolean!) {
query SearchIngredient($query: String!, $supermarket: Boolean!, $picnic: Boolean!) {
ingredients: searchIngredient(query: $query) @skip(if: $supermarket) {
id
name
Expand All @@ -26,7 +26,13 @@ const SEARCH_INGREDIENTS = gql`
description
}
}
ingredients: searchSupermarket(query: $query) @include(if: $supermarket) {
ingredients: searchSupermarket(query: $query) @include(if: $supermarket) @skip(if: $picnic) {
id
name
imageUrl
unitQuantity
}
ingredients: searchPicnic(query: $query) @include(if: $picnic) {
id
name
imageUrl
Expand All @@ -35,6 +41,14 @@ const SEARCH_INGREDIENTS = gql`
}
`;

const GET_MODE = gql`
query SearchIngredientsMode {
currentOrder {
mode
}
}
`;

const renderItem = ({ navigator, item: ingredient, supermarket }) => (
<ListItem
title={`${ingredient.name}${ingredient.warning ? ' ⚠️' : ''}`}
Expand Down Expand Up @@ -76,6 +90,10 @@ export function SearchIngredients({
const navigator = useNavigation();
const colors = useTheme();
const bottomInset = useBottomBarInset();
// The store search hits whichever store the order lives in: AH's catalogue
// normally, Picnic's in picnic mode.
const { data: { currentOrder: { mode } = {} } = {} } = useQuery(GET_MODE);
const picnic = mode === 'picnic';
const [
searchIngredients,
{
Expand All @@ -85,6 +103,11 @@ export function SearchIngredients({
},
] = useLazyQuery(SEARCH_INGREDIENTS, { fetchPolicy: 'cache-and-network' });

const runSearch = (text, toStore) =>
searchIngredients({
variables: { query: text, supermarket: toStore, picnic: toStore && picnic },
});

const ingredients = query === '' ? [] : foundIngredients;
const renderRow = (item) =>
customRenderItem
Expand All @@ -98,14 +121,14 @@ export function SearchIngredients({
placeholder={placeholder}
showLoading={searchLoading}
loadingProps={{ color: Colors.tintColor }}
onChangeText={(query) => searchIngredients({ variables: { query, supermarket } })}
onChangeText={(text) => runSearch(text, supermarket)}
value={query}
rightIcon={
supermarketOnly ? null : (
<Text
onPress={() => {
setSupermarket(!supermarket);
return searchIngredients({ variables: { query, supermarket: !supermarket } });
return runSearch(query, !supermarket);
}}
style={{
color: Colors.text,
Expand All @@ -115,7 +138,7 @@ export function SearchIngredients({
borderRadius: 7,
padding: 5,
}}>
{supermarket ? 'AH' : 'Picape'}
{supermarket ? (picnic ? 'Picnic' : 'AH') : 'Picape'}
</Text>
)
}
Expand Down
1 change: 1 addition & 0 deletions frontend/operations/getOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const orderFields = gql`
availabilityLabel
substitutedFor
checked
store
ingredient {
id
name
Expand Down
16 changes: 14 additions & 2 deletions frontend/screens/AddIngredientScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import { FloatingTop, Spacing } from '../constants/Spacing';
import { useSheetGesture } from '../navigation/useSheetGesture';

const ADD_INGREDIENT = gql`
mutation AddIngredient($name: String!, $isEssential: Boolean!, $supermarketProductId: String!) {
mutation AddIngredient(
$name: String!
$isEssential: Boolean!
$supermarketProductId: String
$picnicProductId: String
) {
addIngredient(
name: $name
isEssential: $isEssential
supermarketProductId: $supermarketProductId
picnicProductId: $picnicProductId
) {
id
name
Expand All @@ -29,6 +35,10 @@ const ADD_INGREDIENT = gql`
}
`;

// A Picnic selling unit id looks like "s2001"; those go in picnicProductId,
// never in supermarketProductId.
const isPicnicId = (id) => /^s\d+$/.test(String(id || ''));

export function AddIngredientScreen({
navigation,
route: {
Expand All @@ -38,7 +48,8 @@ export function AddIngredientScreen({
const [form, changeForm] = useState({
name: ingredient.name,
isEssential: false,
supermarketProductId: ingredient.id,
supermarketProductId: isPicnicId(ingredient.id) ? null : ingredient.id,
picnicProductId: isPicnicId(ingredient.id) ? ingredient.id : null,
});
const insets = useSafeAreaInsets();
const colors = useTheme();
Expand Down Expand Up @@ -107,6 +118,7 @@ export function AddIngredientScreen({
variables: {
name: form.name,
supermarketProductId: form.supermarketProductId,
picnicProductId: form.picnicProductId,
isEssential: form.isEssential,
},
});
Expand Down
77 changes: 57 additions & 20 deletions frontend/screens/EditIngredientScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const GET_INGREDIENT = gql`
isEssential
supermarketProductId
supermarketName
picnicProductId
picnicName
picnicImageUrl
tags {
id
name
Expand All @@ -51,6 +54,9 @@ const GET_INGREDIENT = gql`
}
}
}
currentOrder {
mode
}
ingredients(first: 1000) {
tags {
id
Expand Down Expand Up @@ -84,18 +90,25 @@ export function EditIngredientScreen({
name: '',
isEssential: false,
supermarketProductId: '',
picnicProductId: null,
picnicName: '',
});

const { data: { node: ingredient } = {}, loading } = useQuery(GET_INGREDIENT, {
variables: { ingredientId },
onCompleted: (data) => {
changeForm({
name: data.node.name,
isEssential: data.node.isEssential,
supermarketProductId: data.node.supermarketProductId,
});
},
});
const { data: { node: ingredient, currentOrder: { mode } = {} } = {}, loading } = useQuery(
GET_INGREDIENT,
{
variables: { ingredientId },
onCompleted: (data) => {
changeForm({
name: data.node.name,
isEssential: data.node.isEssential,
supermarketProductId: data.node.supermarketProductId,
picnicProductId: data.node.picnicProductId,
});
},
}
);
const picnic = mode === 'picnic';

if (loading) return null;
if (!loading && !ingredient) {
Expand Down Expand Up @@ -155,30 +168,53 @@ export function EditIngredientScreen({
</View>

<View style={[styles.searchContainer]}>
<Text style={[styles.label, { color: colors.text }]}>Supermarket</Text>
<Text style={[styles.label, { color: colors.text }]}>
{picnic ? 'Picnic' : 'Supermarket'}
</Text>
<SearchIngredients
autoFocus={false}
embedded
supermarketOnly
placeholder={form.supermarketName || ingredient.supermarketName}
customRenderItem={({ item: ingredient, searchRef }) => (
<ListItem autoFocus={false} title={ingredient.name} imageUrl={ingredient.imageUrl}>
placeholder={
picnic
? form.picnicName || ingredient.picnicName || 'Zoek in Picnic'
: form.supermarketName || ingredient.supermarketName
}
customRenderItem={({ item: product, searchRef }) => (
<ListItem autoFocus={false} title={product.name} imageUrl={product.imageUrl}>
<PlusIcon
style={{ margin: 10 }}
onPress={(e) => {
e.preventDefault();
searchRef.current.clear();
console.log(ingredient);
return changeForm({
...form,
supermarketProductId: ingredient.id,
supermarketName: ingredient.name,
});
return changeForm(
picnic
? {
...form,
picnicProductId: product.id,
picnicName: product.name,
}
: {
...form,
supermarketProductId: product.id,
supermarketName: product.name,
}
);
}}
/>
</ListItem>
)}
/>
{picnic && form.picnicProductId && (
<ListItem
title={form.picnicName || ingredient.picnicName || 'Picnic product'}
imageUrl={ingredient.picnicImageUrl}
chevron={false}
onPress={() =>
changeForm({ ...form, picnicProductId: null, picnicName: '(geen Picnic product)' })
}
/>
)}
</View>
</ScrollView>

Expand All @@ -199,6 +235,7 @@ export function EditIngredientScreen({
ingredientId,
name: form.name,
supermarketProductId: form.supermarketProductId,
picnicProductId: form.picnicProductId,
isEssential: form.isEssential,
tagIds: ingredient.tags.map((tag) => tag.id),
},
Expand Down
Loading
Loading