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

Improve sample data for the returned/reimbursed order #3495

Merged

Conversation

kennyadsl
Copy link
Member

@kennyadsl kennyadsl commented Jan 29, 2020

Description

In our sample data script, we are creating two orders. One of them is also returned and refunded in order to be able to see the reimbursement mailer preview, which requires an actual reimbursement in the database to work.

The order is set in a way that allows the email preview to work but it's actually a bit messed up. In a real UX flow, it's not possible to have the order in that state, so I tried to adjust it to be as similar as possible with how a real refunded order looks like.

This also improves the documentation since the sample code flow used can be a guideline to understand how the complex returns flow works.

Ref: #1146

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@kennyadsl kennyadsl added type:enhancement Proposed or newly added feature Documentation labels Jan 29, 2020
@kennyadsl kennyadsl self-assigned this Jan 29, 2020
@kennyadsl kennyadsl force-pushed the kennyadsl/improve-sample-returns branch from 1399e21 to 8127dc3 Compare January 29, 2020 16:23
Copy link
Member

@spaghetticode spaghetticode left a comment

Choose a reason for hiding this comment

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

@kennyadsl 👍 thanks for the improvement, I left a non-blocking suggestion

customer_return = Spree::CustomerReturn.create(
stock_location: stock_location,
return_items: [return_item]
order.return_authorizations.create(
Copy link
Member

Choose a reason for hiding this comment

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

If a new validation is added to the model then #create may fail silently, so what about using #create!?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, and added the ! to some other methods as well, thanks!

The current code is not creating a valid reimbursement since it's
created when the associated order is still not in the riht state
(paid and shipped).

The amount of the reimbursement was also wrong, since the return
item created had an exchange variant associated, making the total
of the calculated reimboursement 0.

This was mainly used to allow seeing the reimbursement email in the
preview (with solidusio#1146), and it was working fine for that purpose but it
is creating an inconsistent order in the backend running samples.

Retruns are already complex and messed up, we need to avoid showing
wrong examples of code and order states to users.
The sample order's reimbursement is created correctly but
`perform!` is never called on it. This means that the instance
of Spree::Reimbursement is persisted, but no action is taken
on the reimbursment for this return (no payment refund is made).

This commit change how we create the reimbursement, the provided
method #build_from_customer_return already accept return items,
create the reimbursement taking what is needed by the customer
return instance and also perform the acutal refund.

To perform a reimbursement we need to pass the user that is taking
this action. When solidus_auth_devise is there we can assume we
have an admin user, at least this is what happens in the sandbox
envirnoment. When it's not used we can just try to fetch/create a
dummy user.
@kennyadsl kennyadsl force-pushed the kennyadsl/improve-sample-returns branch from 8127dc3 to 7686078 Compare February 1, 2020 12:52
@spaghetticode spaghetticode merged commit 3ceec32 into solidusio:master Feb 7, 2020
@spaghetticode spaghetticode deleted the kennyadsl/improve-sample-returns branch February 7, 2020 08:46
Copy link
Contributor

@vzqzac vzqzac left a comment

Choose a reason for hiding this comment

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

Kudos for the initiative! The changes look good overall, I just had some questions after syncing @softr8, do you mind to resolve them?

order = Spree::Order.last
inventory_unit = order.inventory_units.first
order = Spree::Order.last
inventory_unit = order.inventory_units.take!
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey @kennyadsl I think we're not handling exceptions when this takes! method fails, should we still use the bang method?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah nevermind, I just saw spaghetticode's comment and that solved my doubt here

stock_location = inventory_unit.find_stock_item.stock_location
return_reason = Spree::ReturnReason.active.take!
preferred_reimbursement_type = Spree::ReimbursementType.where(name: 'Original').take!
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I remember the take method expects the collection to contain at least one element, this will probably break if we don't have any Spree::ReimbursementType in the database

customer_return: customer_return,
return_items: [return_item]
)
reimbursement = Spree::Reimbursement.build_from_customer_return(customer_return)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the commit description! That made me know what the build_from_customer_return method does without having to do a lot of research

Comment on lines +10 to +14
admin_user = if defined?(Spree::Auth)
Spree.user_class.admin.take!
else
Spree.user_class.find_or_create_by!(email: '[email protected]')
end
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a clever way to set an user, but if we need an actual admin user, shouldn't we create it with more information than just the email?

Copy link
Member Author

Choose a reason for hiding this comment

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

This will be executed when Auth::Devise is not present, so it will create a LegacyUser which does not need any more information to be valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants