Skip to content

Fix Picnic mapping and mode switch dropping quantities - #453

Merged
adri merged 8 commits into
masterfrom
adri/picnic-quantity-fix
Oct 18, 2026
Merged

adri merged 8 commits into
masterfrom
adri/picnic-quantity-fix

Conversation

@adri

@adri adri commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes four quantity bugs from #452 that Greg Grocery hit on production:

  • Mapping dropped checklist quantities to 0. map_ingredient_picnic wrote the mapping but never synced, so the item left the checklist fallback without entering the Picnic cart. Gist, Baksoda and Optimel went 1 → 0.
  • Mapping let the cart's quantity win. Melk was 2 on the list plus 1 in the Picnic cart and ended at 1. New merge rule, documented in the tool description: the list's quantity and the cart's quantity add up (2 + 1 = 3). Ingredients.edit_ingredient/1 hands the cart's quantity of the new product to Order.adopt_picnic_quantity/2, which records the sum as the ingredient's manual quantity and syncs. Mapping can never lower what either side asked for. A manual row pinning the ingredient at 0 wins over adoption.
  • set_mode("picnic") dropped unmapped ingredients. Order.cart/1 keyed everything by supermarket_product_id, so ingredients without a product (Basilicum) or without a Picnic line fell out of the list. The cart is now keyed by ingredient, and any wanted item the Picnic cart does not hold stays visible as a checklist row — including mapped ones whose push never landed.
  • get_shopping_list total disagreed with its items. LineFromPicnic trusted the cart's stored total_price/total_count, which the cache can return stale. Both totals are now summed over the listed articles. The tool description now states the scope: only Picnic cart rows carry a price; checklist items count towards total_count but not total_price.
  • Picnic availability never reached is_orderable. LineFromPicnic left the field at its default true, so a mapped product Picnic cannot sell (Bio avocados, s1015273) never flagged and the replacement fallback never ran. The api/15 ORDER_ARTICLE carries an UNAVAILABLE decorator; LineFromPicnic now maps it to is_orderable and availability_label, and the sync runs the same substitution Order already did for AH: the wanted id swaps for the replacement ingredient's picnic_product_id, the unavailable product stays in the cart flagged, and substituted_for names what it stands in for.

Also fixed along the way: ordered_item_quantities/1 added 2 instead of the item's quantity on a repeated id, and manual_ingredients/1/Recipe.item_quantities/1 could hand nil product keys to the supermarket sync.

Review round 2

  • Substitution ran before the planned/manual merge, on both paths. A manual row on an unavailable product overwrote the plan's quantity on its replacement: manual 2 on Avocado bio plus planned 1 on regular Avocado pushed 2, not 3; manual 0 removed the planned replacement outright. Planned and manual now merge by original product id first (manual still wins per product), substitution runs once, and quantities landing on the same replacement add up. lib/picape/order.ex:222 (ah) and lib/picape/order.ex:561 (picnic).
  • Mapping pinned the wanted quantity even when the cart held 0. adopt_picnic_quantity wrote wanted + cart_quantity whenever the sum was positive, so unplanning the recipe later left the item on the list forever. Only a cart quantity above 0 is adopted now. lib/picape/order.ex:328.
  • Quantity adoption read the 5-hour cart cache. picnic_cart_quantity now invalidates the cache first, so the adopted sum uses the live cart. lib/picape/ingredients.ex:178.
  • Unavailable Picnic products counted towards the total. ORDER_ARTICLE.price is per unit (verified against the api/15 shape), so the multiply stays — but articles with an UNAVAILABLE decorator are now skipped in total_price. lib/picape/order/line_from_picnic.ex:22.

Test plan

  • mix check green (format, credo strict, warnings-as-errors compile, 190 tests, sobelow)
  • cd frontend && npm run check green
  • bin/e2e green (95 passed, 10 skipped, WebKit)
  • New tests cover: checklist-only mapping pushes its quantity, list + cart merge on mapping, unmapped and product-less ingredients surviving a picnic switch, mapped-but-not-in-cart items staying visible, total_price consistency, an ah → picnic → checklist → ah round trip keeping every quantity, and an unavailable mapped product falling back to its replacement
  • Review round 2 tests: manual + planned adding up on the same replacement (ah and picnic, quantities 2 and 0), no manual pin when the cart holds 0, live-cart adoption under a stale cache, total_price at quantity 2 and its exclusion of unavailable items

No checkout, payment or delivery-slot calls are made anywhere. AH product ids and replacement mappings are untouched. No migrations.

Impact

impact:
  not-measurable: The regression tests in this PR prove the behaviour end to end against the HTTP supermarket fake; no production metric tracks shopping-list quantities.

cc @adri

Generated with Devin

adri and others added 8 commits October 18, 2026 15:57
Order.cart/1 keyed everything by supermarket_product_id, so an
ingredient without a supermarket product silently dropped out of the
computed list — in picnic mode those items vanished from the shopping
list instead of staying as checklist rows (Avocado bio, Eieren,
Basilicum). The cart is now keyed by ingredient and falls back to the
ingredient id as item key when there is no product.

picnic_line/1 only fell back ingredients with no Picnic mapping. A
mapped ingredient whose product never made it into the cart — the push
failed or never ran — disappeared the same way. Any wanted item the
cart does not hold now shows as a checklist row.

manual_ingredients/1 and Recipe.item_quantities/1 could hand nil keys
to the supermarket sync; both now filter them.
ordered_item_quantities/1 added 2 instead of the item's quantity when
an id repeated.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Mapping a checklist ingredient to a Picnic product used to drop its
quantity to 0: edit_ingredient wrote the mapping but never synced, so
the item left the checklist fallback and never entered the cart (Gist,
Baksoda, Optimel went 1 -> 0). And when the Picnic cart already held
the product, the cart's quantity silently stood in for the list's
(Melk: 2 on the list plus 1 in the cart ended at 1).

Merge rule, now documented in the map_ingredient_picnic tool
description: the quantity the list wants and the quantity the cart
holds add up. Ingredients.edit_ingredient/1, on a changed mapping in
picnic mode, hands the cart's quantity of the new product to
Order.adopt_picnic_quantity/2, which records list + cart as the
ingredient's manual quantity and syncs — mapping can never lower what
either side asked for. A manual row pinning the ingredient at 0 wins
over adoption.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
get_shopping_list reported the Picnic cart's stored total_price and
total_count, which can disagree with the items the cart holds — the
cached cart keeps a stale total (the list showed 15 items priced above
the reported EUR 18.55). LineFromPicnic now sums both totals over the
articles it lists, so the reply is always consistent with its own
items. The get_shopping_list tool description now says what counts:
in picnic mode only the Picnic cart rows carry a price; checklist
items add to total_count but not total_price.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
A Picnic cart article the store cannot sell carries an UNAVAILABLE
decorator. Picape never read it, so picnic items always reported
is_orderable true and the replacement fallback only ran in ah mode.
LineFromPicnic now maps the decorator to is_orderable and the sync
and substitution code generalizes over the product id field, so a
mapped product Picnic cannot sell falls back to the replacement
ingredient's Picnic product, flagged substituted_for.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sync substituted the two maps separately and let Sync.changes merge
them, so a manual row on an unavailable product overwrote the plan's
quantity on its replacement instead of adding to it. Manual 2 on an
unavailable product plus planned 1 on the replacement pushed 2, not 3;
manual 0 removed the planned replacement outright. Merge by original
product id first (manual still wins per product) and substitute once,
on both the supermarket and the Picnic path.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Mapping adopted `wanted + cart_quantity` as a manual row whenever the
sum was positive, so a product absent from the cart still got its
wanted quantity pinned as manual. Unplanning the recipe then left the
item on the list forever. Only a cart quantity above zero is worth
adopting; without one the sync keeps the plan's quantity on its own.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
`picnic_cart_quantity` read the ConCache cart, which can be five hours
old, so mapping adopted whatever the cache remembered rather than what
the cart really holds. Invalidate before reading so the adopted sum
uses the live cart.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
An ORDER_ARTICLE's price is per unit, so multiplying by quantity stays.
But an article carrying the UNAVAILABLE decorator cannot be sold and
should not add to what the order would cost.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@adri
adri marked this pull request as ready for review October 18, 2026 18:19
@adri
adri merged commit d7009ae into master Oct 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant