Skip to content

Empty the Picnic cart when an order closes - #455

Draft
adri wants to merge 1 commit into
masterfrom
cursor/picnic-close-clears-cart-ad8b
Draft

adri wants to merge 1 commit into
masterfrom
cursor/picnic-close-clears-cart-ad8b

Conversation

@adri

@adri adri commented Oct 22, 2026

Copy link
Copy Markdown
Owner

Root cause

Receiving a Picnic order (start_shopping / "Bestelling ontvangen") archived the plan rows, which is why get_previous_orders showed the 19 Oct order closed. It never touched the Picnic cart.

Two things then kept those 44 lines on the new order:

  1. The cart is a separate basket. Unlike the supermarket, nothing empties it when Picape records the order as received. start_shopping moved PlannedRecipe and ManualIngredient off line "1" and, in picnic mode, stored no snapshot (order_item.product_id is numeric; Picnic selling-unit ids are strings).
  2. Sync leaves unnamed cart rows alone. Order.Sync treats a product the plan does not mention (new_count == nil) as something the Picnic app added itself, and does not remove it. After close the plan on line "1" is empty, so the daily sync neither deletes the old rows nor has to re-add them — they just stay. picnic_line renders that cart as the current order, with planned_recipes empty because the recipes now live on the closed order.

There is a second way the same lines get back into the cart: if Picnic itself empties the cart at checkout before Picape closes the order, a sync in that window still has the plan on line "1" and pushes every line into the fresh cart. Closing then has to clear that refill too. It did not.

Quantity 0

Substitution rewrites a wanted id onto the replacement and drops the original. A quantity of 0 was moved onto the replacement and added to whatever else had landed there (0 + 1 = 1), and the original id left the wanted map. Sync then treated the missing id as "leave untouched", so the cart row stayed at its old quantity.

Fix

  • In picnic mode, start_shopping removes every article currently in the Picnic cart, using the existing remove_product delta. The closed order still keeps its plan rows. The next sync sees an empty plan and an empty cart, so it pushes nothing back. A product added in the Picnic app after that is still left alone.
  • A direct quantity of 0 is written back onto that product after substitution, so it wins over a quantity that was added on top, and a later sync removes the row again instead of restoring it.
  • That 0 also stays on the original id, so an unavailable cart line is removed rather than left in place.
  • The same 0 removes the replacement when it is only a stand-in (unavailable, or already swapped out so the cart holds the replacement and not the original) and nothing else wants that product at a positive quantity. An independently planned replacement is kept, as before.
  • start_shopping now triggers order_changed, so an open app picks up the empty order.

No checkout, payment, or delivery-slot call was added. The AH substitution path, AH product ids, and replacement mappings are unchanged. No migration.

If Picnic is unreachable at the moment of close, the plan is still archived and the clear is logged and skipped. Tapping "Bestelling ontvangen" again retries the clear.

Tests

mix check (format, credo --strict, compile --warnings-as-errors, mix test --warnings-as-errors, sobelow): 197 tests, 0 failures.

New coverage in test/picnic_mode_test.exs:

  • Closing a picnic order empties the cart, including a product added in the Picnic app, and a later sync does not refill it. A product added after the close is still left alone. The close only calls GET /api/15/cart and remove_product. The AH basket is unchanged.
  • Closing still archives the order when Picnic is down.
  • Closing an AH order does not call Picnic.
  • A quantity of 0 sticks when another ingredient substitutes onto that product, including across a second sync (the Miso case).
  • Setting the original to 0 removes both the unavailable product and the replacement that was swapped in, and a later sync does not swap the replacement back.
  • Setting the original to 0 removes a replacement that is the only cart row.
  • A quantity of 0 on the replacement sticks while the original is still wanted.

Existing picnic and AH replacement tests still pass, including "a manual 0 on an unavailable product keeps the planned replacement".

Open in Web Open in Cursor 

Receiving a Picnic order archived the plan but left the cart in place.
Sync leaves unnamed cart rows alone, so the next order showed those
lines again. A quantity of 0 also failed to stick when substitution
moved it onto a replacement.

Closing now removes every cart article. A direct zero stays on that
product and clears a stand-in replacement that nothing else wants.

Co-authored-by: Adrian Philipp <adri@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

2 participants