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

Add amount_remaining for Spree::StoreCreditEvent #1512

Merged

Conversation

mtylty
Copy link
Contributor

@mtylty mtylty commented Oct 10, 2016

This additional field will make sure that credit events will record the actual unused amount for a particular Spree::StoreCredit.

This also hides the user_total_amount in the backend because it makes no sense to display the total credit a user has at her/his disposal when visiting the store credit show route in the backend.

This should close #1473. It's a temporary fix waiting for a bigger refactor of Spree::StoreCreditEvent.

@mtylty
Copy link
Contributor Author

mtylty commented Oct 10, 2016

The new backend will look like something like this:

schermata 2016-10-10 alle 15 30 06

@gmacdougall
Copy link
Member

Thanks for taking this on Matteo. Is there anything that we could do to fill in the amount remaining on all of the existing store credits? We should have enough of a history to come up with something based upon the StoreCreditEvent.

If not, I would suggest we leave it NULL to show that we don't have that data.

@mtylty
Copy link
Contributor Author

mtylty commented Oct 17, 2016

I've changed the migration to add NULL default and migrate all the old data.

About the data migration: I've manually tested the various cases and it seems to be working for me (it correctly recalculated all the remaining amounts for all the store credit events I had in the sandbox). Can't say if there are some edge cases I'm not considering... Please take a look at the comments on the migration and tell me if you think it could be right.

I noticed there is a DEPRECATION WARNING on display_remaining_amount when amount_remaining is nil saying I should give "0.00" as an argument... Maybe it's better to leave the default of the new column to 0.0?

stewart added a commit to TommyJohnWear/solidus that referenced this pull request Oct 26, 2016
This commit backports work from an open [Solidus PR][1]

[1]: solidusio#1512

This migration adds the `amount_remaining` column to the
`spree_store_credit_events` table, and iterates over existing Store
Credit Events to calculate historical data for this column. This value
is also displayed in the admin, instead of the User's total remaining
store credit.
@bbuchalter
Copy link
Contributor

I've been using this change successfully in my store for more than six months. I believe it's time to get this one merged. 👍

cc: @gmacdougall

bbuchalter pushed a commit to TommyJohnWear/solidus that referenced this pull request May 26, 2017
This commit backports work from an open [Solidus PR][1]

[1]: solidusio#1512

This migration adds the `amount_remaining` column to the
`spree_store_credit_events` table, and iterates over existing Store
Credit Events to calculate historical data for this column. This value
is also displayed in the admin, instead of the User's total remaining
store credit.
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

LGTM, @gmacdougall could you please review as well?

@tvdeyen
Copy link
Member

tvdeyen commented Oct 4, 2017

@mtylty would you mind to rebase this with latest master? Thanks

@mtylty mtylty force-pushed the nebulab/fix-store-credit-balance-history branch from 3d8045e to 1f92c97 Compare November 17, 2017 15:30

credit.store_credit_events.chronological.each do |event|
case event.action
when Spree::StoreCredit::ALLOCATION_ACTION,

Choose a reason for hiding this comment

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

Avoid when branches without a body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure... is this a false positive?

Copy link
Member

Choose a reason for hiding this comment

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

@mtylty I think hound is right. This when is missing its content, maybe it should just be credit_amount? Or are you intentionally leaving it without content, just for documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kennyadsl you're right I didn't notice the when doesn't actually have code in it. Thanks!

@mtylty mtylty force-pushed the nebulab/fix-store-credit-balance-history branch 2 times, most recently from 11cce75 to ee1ee3c Compare November 28, 2017 17:36
def up
add_column :spree_store_credit_events, :amount_remaining, :decimal, precision: 8, scale: 2, default: nil, null: true

Spree::StoreCredit.all.each do |credit|
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to avoid referencing the Spree::StoreCredit model and the constants here.

Instead we can define our own "AddAmountRemainingToStoreCreditEvents::StoreCredit" model here and use that instead.

@kennyadsl kennyadsl assigned kennyadsl and unassigned gmacdougall Mar 7, 2018
@mtylty mtylty force-pushed the nebulab/fix-store-credit-balance-history branch from ee1ee3c to 994519a Compare March 9, 2018 09:41
@kennyadsl kennyadsl force-pushed the nebulab/fix-store-credit-balance-history branch from 994519a to ee8a77b Compare March 22, 2018 11:11
@@ -0,0 +1,38 @@
class AddAmountRemainingToStoreCreditEvents < ActiveRecord::Migration[5.0]

Choose a reason for hiding this comment

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

Missing magic comment # frozen_string_literal: true.

@mtylty mtylty force-pushed the nebulab/fix-store-credit-balance-history branch from ee8a77b to 41682aa Compare March 22, 2018 13:33
@mtylty
Copy link
Contributor Author

mtylty commented Mar 22, 2018

@kennyadsl this should be good to go, I made the requested changes.

I think it needs to be re-reviewed because I added a couple of changes that are probably worth talking about.

I talked this through with @kennyadsl and we decided to go for StoreCredit.includes(:store_credit_events).find_each to actually load the records to migrate so that, if you have a huge number of store credits, the server won't try to load all the records at once into memory.

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

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

Thanks @mtylty

@kennyadsl
Copy link
Member

@mtylty can you rebase now please?

This additional field will make sure that credit events will record the
actual unused amount for a particular Spree::StoreCredit.

This also hides the user_total_amount in the backend because it makes no
sense to display the total credit a user has at her/his disposal when
visiting the store credit show route in the backend.
This adds data migration capabilities to the migration that adds
the amount_remaining column to Spree::StoreCreditEvent.

The migration will re-calculate the amount_remaining based on previous
store credit events and the initial credit amount.
@mtylty mtylty force-pushed the nebulab/fix-store-credit-balance-history branch from 41682aa to 38a4a5f Compare March 28, 2018 15:38
@mtylty
Copy link
Contributor Author

mtylty commented Mar 28, 2018

@kennyadsl done!

Copy link
Contributor

@jhawthorn jhawthorn 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 to me.

My only concern is that the migration might be slow

@kennyadsl kennyadsl merged commit b504f7e into solidusio:master Apr 11, 2018
@kennyadsl kennyadsl deleted the nebulab/fix-store-credit-balance-history branch April 11, 2018 15:52
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.

Store Credit History Balance Not Treated Independently
7 participants