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

Disallow changes of canceled order #7287

Merged
merged 10 commits into from
Apr 8, 2021
Merged

Disallow changes of canceled order #7287

merged 10 commits into from
Apr 8, 2021

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Mar 31, 2021

What? Why?

Closes #7166.
Complements #7228 and #7265.

Updating cancelled orders messes with stock and payment amounts. So we decided to not allow that. Previous PRs removed the UI but here I'm also putting guards in the controllers so that this doesn't happen accidentally or via the API.

What should we test?

  • Checkout for a completed order.
  • Add, remove and adjust line items of the order as shop owner. Everything should work as normal.
  • Open a new tab to change adjustments, as normal.
  • Open a third tab to cancel the order.
  • Going back to the previous two tabs, try to change adjustments or line items. It should not change anything.
  • Reload the pages and observe that the edit buttons are gone.

Release notes

Changelog Category: User facing changes

It's not possible to change the contents of cancelled orders any more to avoid inconsistencies.

@mkllnk mkllnk self-assigned this Mar 31, 2021
@mkllnk mkllnk marked this pull request as ready for review March 31, 2021 04:38

expect(response.status).to eq(200)
expect(inventory_units_for(variant).size).to eq(1)
it "doesn't adjusts stock when adding a variant" do
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ typo on adjusts

}.to_not change { existing_variant.reload.on_hand }
end

it "doesn't adjusts stock when removing a variant" do
Copy link
Contributor

Choose a reason for hiding this comment

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

here as well

id: order.shipments.first.to_param
context "for canceled orders" do
before do
expect(order.cancel).to eq true
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a big fan of asserting things in the test setup phase. It feels a bit like defensive and unconfident programming and it adds up to the build time. If cancel didn't work we would notice because the test would fail, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not a big fan of asserting in the setup either. But I do want to know if something goes wrong here and I don't think that this assertion slows the tests down much.

If cancel doesn't work then the order might be in an unknown state with unknown behaviour and we don't know if that would pass or fail the test. I wish I could rely on it to work. My experience is though that it can be tricky to get the test setup right and then things don't behave as expected.

The best solution would probably be to add a new factory: create(:cancelled_order). Another time...

Copy link
Contributor

@sauloperez sauloperez left a comment

Choose a reason for hiding this comment

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

You nailed it with those tests 👏

Copy link
Contributor

@andrewpbrett andrewpbrett left a comment

Choose a reason for hiding this comment

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

Thanks @mkllnk!

@@ -28,6 +29,10 @@ def set_order_id
@adjustment.order_id = parent.id
end

def skip_changing_canceled_orders
redirect_to admin_order_adjustments_path(@order) if @order.canceled?
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley Apr 5, 2021

Choose a reason for hiding this comment

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

It feels like a flash message might be helpful here to give some user feedback?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a commit for this.

@mkllnk mkllnk requested a review from Matt-Yorkley April 7, 2021 06:54
@codecov
Copy link

codecov bot commented Apr 7, 2021

Codecov Report

Merging #7287 (34308ab) into master (52c7abf) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 34308ab differs from pull request most recent head 42543bf. Consider uploading reports for the commit 42543bf to get more accurate results
Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7287   +/-   ##
=======================================
  Coverage   89.70%   89.71%           
=======================================
  Files         649      649           
  Lines       18865    18873    +8     
=======================================
+ Hits        16923    16931    +8     
  Misses       1942     1942           
Impacted Files Coverage Δ
app/controllers/api/v0/shipments_controller.rb 97.05% <100.00%> (+0.13%) ⬆️
.../controllers/spree/admin/adjustments_controller.rb 97.87% <100.00%> (+0.25%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52c7abf...42543bf. Read the comment docs.

@filipefurtad0 filipefurtad0 self-assigned this Apr 7, 2021
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 7, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Apr 7, 2021

Hey @mkllnk ,

Thanks for the awesome work and the detailed steps on how to test this. Going though them, step-wise:

  • Checked out for a completed order.

  • Added, removed and adjusted line items of the order as shop owner.
    At this point, I met this bug again, when deleting adjustments (not related to this issue) - Can't delete (some) adjustments - Error 401 Unauthorized #7341

  • Opened a new tab to change adjustments, as normal.

  • Opened a third tab to cancel the order.

After this, going back to the previous two tabs:
i) tried to change line items, changing shipping method: all good here. Changes don't take effect at all - OK

ii) Editing or creating new adjustments displays the warning below, and takes no effect - OK
image
iii) deleting adjustments :

So maybe on point iii) there would be room for improvement. I'll create an issue to prevent deleting of adjustments, if an order was cancelled on a different tab - which is in a way an edge case.

But I think this brings plenty of value, so we should merge it 👍 Moving to ready to go.

@filipefurtad0 filipefurtad0 removed the pr-staged-uk staging.openfoodnetwork.org.uk label Apr 7, 2021
mkllnk added 10 commits April 8, 2021 09:24
This commit is best viewed ignoring whitespaces.
We decided to disallow changing canceled orders in a way that would
affect stock or totals.
We have a PR already that removes the UI for this when the order is
canceled. Implementing it on controller-side makes sure that it doesn't
happen accidentally if the user has multiple tabs open.
@mkllnk mkllnk merged commit b83340a into openfoodfoundation:master Apr 8, 2021
@mkllnk mkllnk deleted the 7166-cancelled-stock branch April 8, 2021 01:28
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.

Changing line item quantities on cancelled orders leads to stock management discrepancies
5 participants