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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions sample/db/samples/reimbursements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,46 @@

Spree::Sample.load_sample("orders")

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

admin_user = if defined?(Spree::Auth)
Spree.user_class.admin.take!
else
Spree.user_class.find_or_create_by!(email: '[email protected]')
end
Comment on lines +10 to +14
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.


return_item = Spree::ReturnItem.create(inventory_unit: inventory_unit)
# Mark the order paid and shipped
order.payments.pending.each(&:complete)
order.shipments.each do |shipment|
shipment.suppress_mailer = false
shipment.ship!
end

return_item.exchange_variant = return_item.eligible_exchange_variants.last
return_item.build_exchange_inventory_unit
return_item.accept!
# Create a return authorization
return_item = Spree::ReturnItem.new(
inventory_unit: inventory_unit,
preferred_reimbursement_type: preferred_reimbursement_type
)

customer_return = Spree::CustomerReturn.create(
stock_location: stock_location,
return_items: [return_item]
order.return_authorizations.create!(
reason: return_reason,
return_items: [return_item],
stock_location: stock_location
)

order.reimbursements.create(
customer_return: customer_return,
return_items: [return_item]
# Create a customer return and mark it as received
customer_return = Spree::CustomerReturn.create!(
return_items: [return_item],
stock_location: stock_location
)
return_item.reload
return_item.skip_customer_return_processing = true
return_item.receive!
customer_return.process_return!

# Accept the customer return and reimburse it
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

reimbursement.return_all(created_by: admin_user)