Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store link to open backorder #12960

Merged
merged 8 commits into from
Nov 21, 2024

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Nov 1, 2024

ℹ️ Funded Feature. Please track ALL ASSOCIATED WORK under the associated tracking code #11678 DFC Orders

What? Why?

A new scenario is be possible now:

  • Distributor has two order cycles open with products from the same DFC supplier.
  • Previously, we would create only one backorder.
  • The backorder got finalised at the end of the first order cycle.
  • Now we create a backorder for each order cycle.
  • The backorders are amended independently for each order cycle.
  • The backorders get finalised at the end of their respective order cycle.

What should we test?

  • Testing for @RaggedStaff:
  • Open new order cycle with DFC products.
  • Place orders which trigger a backorder.
  • Cancel one order -> backorder should get adjusted as before.
  • At OC end, the backorder should get finalised like before.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@mkllnk mkllnk added the technical changes only These pull requests do not contain user facing changes and are grouped in release notes label Nov 1, 2024
@mkllnk mkllnk self-assigned this Nov 1, 2024
@mkllnk mkllnk marked this pull request as ready for review November 1, 2024 04:36
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good ! Thanks for all the comments on the lookup_open_order 🥇 I don't think I would have understood without them !

&.tap { |o| o.orderStatus = "dfc-v:Held" }
end

# DEPRICATED
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:

Suggested change
# DEPRICATED
# DEPRECATED

app/services/fdc_backorderer.rb Show resolved Hide resolved
@RaggedStaff RaggedStaff added the pr-staged-uk staging.openfoodnetwork.org.uk label Nov 4, 2024
@RaggedStaff
Copy link
Collaborator

Heads up - this is not working!

DO NOT MERGE

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, a nice step-by-step approach which seems like it should work perfectly..

But apparently it didn't 😞

@@ -13,7 +13,7 @@ class BackorderJob < ApplicationJob
sidekiq_options retry: 0

def self.check_stock(order)
links = SemanticLink.where(variant_id: order.line_items.select(:variant_id))
links = SemanticLink.where(subject: order.variants)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💎✨

app/services/fdc_backorderer.rb Show resolved Hide resolved
@mkllnk
Copy link
Member Author

mkllnk commented Nov 14, 2024

@RaggedStaff, do you have any more information on this?

You shared a couple of screenshots on Slack with me which state a completed order of:

  • Baked Beans * 2
  • Apple Vinegar * 2
  • Camelina Seed * 1
  • Carlin Peas * 1

I found two orders on the staging server that seem to be related. Both were cancelled. But they contain a mix of retail and wholesale items. I don't think we specified that scenario, did we?

Also, the resulting Shopify order doesn't have all the items of the two cancelled orders. So something must have gotten cancelled, right?

I also noticed that the updated_at timestamps of the orders are only 100ms apart. Did you cancel them both at the same time? I'm not sure we guard against race conditions during cancellations.

Can you explain exactly what you did and what you expected? I'm wondering if the real pilots are actually simpler. We had some assumptions:

  • Either stock controlled or on demand.
  • Either retail or wholesale variants sold in OFN.
  • Canellations are triggered by the customer and therefore rare and don't happen in bulk.

Some of this may be solved later when we support any way of amending orders but we are not there yet. This pull request just needs to work as well as before.

@mkllnk mkllnk force-pushed the dfc-link-backorder branch from 9431eff to 4945f4d Compare November 15, 2024 00:19
@mkllnk mkllnk added pr-staged-uk staging.openfoodnetwork.org.uk and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Nov 15, 2024
@mkllnk
Copy link
Member Author

mkllnk commented Nov 15, 2024

@RaggedStaff, I just did a little test on staging and everything seemed to work. Steps:

  • Ordered beans and chia seed.
  • Draft order open with bean while chia was still in stock.
  • Second order with beans and chia.
  • Draft order now contained chia as well but no additional beans, still in stock.
  • Cancel second order.
  • Draft order got reverted to first state of only having beans.
  • At the end of the order cycle the draft order was completed with beans.

Can you check again? Maybe we can find the edge cases. I guess though that the edge cases never worked. This PR doesn't solve everything, only one little aspect that will allow more.

@RaggedStaff
Copy link
Collaborator

@RaggedStaff, do you have any more information on this?

I found two orders on the staging server that seem to be related. Both were cancelled. But they contain a mix of retail and wholesale items. I don't think we specified that scenario, did we?

No we didn't, the staging Shopify store is in a mess though, cos we keep switching back & forth to test different scenarios.

Also, the resulting Shopify order doesn't have all the items of the two cancelled orders. So something must have gotten cancelled, right?

I think some stuff was in stock, so didn't ever appear on the order.

I also noticed that the updated_at timestamps of the orders are only 100ms apart. Did you cancel them both at the same time? I'm not sure we guard against race conditions during cancellations.

Ah, yeah that may well be it. I'm pretty sure I used the bulk cancel Order function (selected both orders on the Order screen & cancelled). I think I expected that to cancel both orders and either:

  1. Recalculate Producer backorder for new demand (summing up remaining orders)
  2. Submit an adjustment for each cancellation in sequence

I don't think I'm bothered which way we go, it should result in the same outcome, right? 🤔

I think the edge here may be an empty order... Shopify will not allow an Order without at least 1 OrderLine with hasQuantity > 0. Have you tried cancelling all the Orders & seeing what happens? I think that may explain my issue - the first order was left on the Shopify order (i.e. it failed attempting to back out the last orderline).

I would like to confirm that as I'd like better error capture on that in Shopify (& possibly OFN). I'll do a bit more testing now.

If that is it, I agree the issue probably existed already & we don't need to address it in this PR. It's (hopefully) quite an unlikely edge case, at least for our current pilots. 🤞🏻

@RaggedStaff
Copy link
Collaborator

Ok. That's not the edge case... I checked with a single order & cancellation... steps to reproduce:

  1. Set up up Test OC (18) on FDC Test Hub 2
  2. Place Order for Red Fox Carlin Peas (Order #R363653617)
  3. Confirm Draft Order is present on test-hodmedod (Draft #D124)
  4. Cancel Order #R363653617
  5. Confirm Draft Shopify order ( #D124) now only has placeholder product.

Seems it is multi-order cancellations. I will warn our test hubs not to attempt that & I think we can proceed with this now.

Thanks @mkllnk

@mkllnk
Copy link
Member Author

mkllnk commented Nov 19, 2024

multi-order cancellations

Yes, we probably have multiple cancellations run at the same time going into a race condition. I would like to fix that a bit later though, when we amend everything consistently. Hm, maybe it doesn't matter when I fix this. Just a matter of priority.

And ActiveRecord magic does the rest when used correctly.
We don't use the link yet, but it's there.
Backorder can become empty after a customer cancels their order. Then we
don't want to fail but also don't need to place an order.
@mkllnk mkllnk force-pushed the dfc-link-backorder branch from 4945f4d to 46048dc Compare November 19, 2024 04:54
@mkllnk mkllnk added pr-staged-uk staging.openfoodnetwork.org.uk and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Nov 19, 2024
@mkllnk
Copy link
Member Author

mkllnk commented Nov 19, 2024

@RaggedStaff, how shall we proceed here?

  • The race condition of multiple cancellations at the same time is a pre-existing issue and can be fixed later.
  • The error on completing the backorder was caused by an empty backorder which doesn't need placing anyway. But I added a commit to handle that case gracefully.

Do you approve of this PR? Can we merge it?

@mkllnk mkllnk removed the pr-staged-uk staging.openfoodnetwork.org.uk label Nov 21, 2024
@mkllnk
Copy link
Member Author

mkllnk commented Nov 21, 2024

After chatting with David, we agree that we can merge this. It has been tested and even though the test failed, we know that this issues have been either addressed or were pre-existing. So let's unblock further work.

@RaggedStaff, if you have concerns then you can still opt out of the next release until we fix whatever your concern is.

@mkllnk mkllnk merged commit 3ec8cd2 into openfoodfoundation:master Nov 21, 2024
53 checks passed
@mkllnk mkllnk deleted the dfc-link-backorder branch November 21, 2024 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
technical changes only These pull requests do not contain user facing changes and are grouped in release notes
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[DFC Orders] Store links to open backorders
4 participants