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

Fix fatal error in reports helper for orders without payments #5573

Merged
merged 3 commits into from
Jun 15, 2020

Conversation

Matt-Yorkley
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley commented Jun 9, 2020

What? Why?

Closes #5572

A form helper was trying to write create options for a dropdown of payment methods on orders, but the orders did not necessarily have payments (or payment methods), resulting in a slug.

What should we test?

The payment methods report page loads correctly if the hub has orders without payments

Release notes

Fixed an issue in payments report for orders without payments.

@Matt-Yorkley Matt-Yorkley self-assigned this Jun 9, 2020
Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

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

👍

@filipefurtad0 filipefurtad0 self-assigned this Jun 10, 2020
@filipefurtad0 filipefurtad0 added pr-staged-fr staging.coopcircuits.fr feedback-needed labels Jun 10, 2020
@filipefurtad0
Copy link
Contributor

Hey @Matt-Yorkley,

After a chat in Slack, @sauloperez made some changes in the DB creating a similar order to the one causing the issue originally. The idea was trying to reproduce this in staging, to make sure the PR solves it. This was order R805323111:
image

This indeed triggered a snail/bugsnag before:
https://openfoodnetwork.slack.com/archives/CEF14NU3V/p1591805246325400

but also after staging this PR:
https://openfoodnetwork.slack.com/archives/CEF14NU3V/p1591805913325700

Adding the feedback-needed label for further checks, and access whether further dev work is required.

@filipefurtad0 filipefurtad0 removed the pr-staged-fr staging.coopcircuits.fr label Jun 10, 2020
@sauloperez sauloperez self-assigned this Jun 11, 2020
@sauloperez
Copy link
Contributor

sauloperez commented Jun 11, 2020

I managed to reproduce it and fix it on my machine. I simply removed the payment for one of the orders that showed up in the report and my patched worked. Could you take another look @filipefurtad0 ?

@Matt-Yorkley
Copy link
Contributor Author

@sauloperez it looks like we jumped on this at the same time...

@sauloperez
Copy link
Contributor

sauloperez commented Jun 11, 2020

and my change is now gone 😅

I don't see what you changed with your force-push. Mine was

commit f322bac7668d91658c4a0c3c8c4a1f3be5428a2d
Author: Pau Perez <[email protected]>
Date:   Thu Jun 11 11:17:25 2020 +0200

    Defend from an order w/o payments building table

diff --git a/lib/open_food_network/order_cycle_management_report.rb b/lib/open_food_network/order_cycle_management_report.rb
index 49fb2e736..73a0ea34d 100644
--- a/lib/open_food_network/order_cycle_management_report.rb
+++ b/lib/open_food_network/order_cycle_management_report.rb
@@ -77,7 +77,7 @@ module OpenFoodNetwork
        ba.phone,
        order.shipping_method.andand.name,
        order.payments.first.andand.payment_method.andand.name,
-       order.payments.first.amount,
+       order.payments.first.andand.amount,
        OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance]
     end
 
@@ -92,7 +92,7 @@ module OpenFoodNetwork
        sa.phone,
        order.shipping_method.andand.name,
        order.payments.first.andand.payment_method.andand.name,
-       order.payments.first.amount,
+       order.payments.first.andand.amount,
        OpenFoodNetwork::UserBalanceCalculator.new(order.email, order.distributor).balance,
        has_temperature_controlled_items?(order),
        order.special_instructions]

Would you mind applying it?

@filipefurtad0 filipefurtad0 added the pr-staged-fr staging.coopcircuits.fr label Jun 11, 2020
@filipefurtad0
Copy link
Contributor

Hey @Matt-Yorkley and @sauloperez,

The report still is broken, after staging:
error_500

Moving back to dev.

@filipefurtad0 filipefurtad0 removed the pr-staged-fr staging.coopcircuits.fr label Jun 11, 2020
@sauloperez sauloperez added the pr-staged-fr staging.coopcircuits.fr label Jun 12, 2020
This a data integrity issue that needs deeper investigation but while
this happens, our users can still render their reports.
@sauloperez
Copy link
Contributor

sauloperez commented Jun 12, 2020

@filipefurtad0 this time we encountered another issue that is being discussed in #5561. Some orders didn't have billing address and that's something we assumed existed.

This was it: https://app.bugsnag.com/open-food-france/open-food-france-prod/errors/5ee107d3dd8f6c0018b1c294?filters[event.since][0]=1d&filters[error.status][0][type]=eq&filters[error.status][0][value]=open&filters[app.release_stage][0][value]=staging&filters[app.release_stage][0][type]=eq

It surprised me that I could do the following from the console without any failure:

irb(main):002:0> order.bill_address = nil       
=> nil                                          
irb(main):003:0> order.save!        
   (1.3ms)  COMMIT                              
=> true      

This makes me think we don't have any validation for this either at app or DB level. In any case, this needs a deeper look.

@sauloperez sauloperez removed the pr-staged-fr staging.coopcircuits.fr label Jun 12, 2020
@filipefurtad0 filipefurtad0 added pr-staged-es pr-staged-fr staging.coopcircuits.fr labels Jun 15, 2020
@sauloperez
Copy link
Contributor

Something else I just spot is that I can do a order.payments.first.destroy while order.state = 'complete'.

IMO, we shouldn't let this happen in that state. I believe that from an accounting perspective this is a red flag although it removes some flexibility that may come in handy when "fixing" some edge cases as a hub manager.

@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Jun 15, 2020

Hi @sauloperez and @Matt-Yorkley,

This PR is working as expected and preventing the report to break.

This was tested after manually corrupting orders both via psql (removing payments - thanks @sauloperez 👍 ), in FR (R805323111, now deleted) and ES-staging (R620504317, id=1979) - see below note empty shipment_state as well:

  id  |   number   | item_total | total |  state   | adjustment_total | user_id |         created_at         |         updated_at         |        completed_at        | bill_address_id | ship_address_id | payment_total | shipment_state | payment_state |                 email                  | special_instructions | distributor_id | order_cycle_id | currency | last_ip_address | customer_id | created_by_id 
------+------------+------------+-------+----------+------------------+---------+----------------------------+----------------------------+----------------------------+-----------------+-----------------+---------------+----------------+---------------+----------------------------------------+----------------------+----------------+----------------+----------+-----------------+-------------+---------------
 1979 | R620504317 |       0.00 |  0.00 | complete |             0.00 |      41 | 2020-06-11 15:55:52.315506 | 2020-06-11 16:07:31.662329 | 2020-06-11 16:07:31.662329 |            1413 |            1414 |         10.00 |                | balance_due   | [email protected] | 

image

Before the PR the Payment Methods Report was breaking - after staging it isn't. The page renders well and Order R620504317 now shows up:

image

As agreed in Slack, the remaining issues concerning this will be tackled in other PRs.
Ready to go.

@filipefurtad0 filipefurtad0 removed pr-staged-es pr-staged-fr staging.coopcircuits.fr labels Jun 15, 2020
@luisramos0 luisramos0 merged commit 538d886 into openfoodfoundation:master Jun 15, 2020
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.

500 Error on Payment Methods Report
4 participants