mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
User can switch between trialling a shopfront and not trialling a shopfront
This commit is contained in:
@@ -19,12 +19,19 @@ module Admin
|
||||
def set_sells
|
||||
enterprise = Enterprise.find(params[:id])
|
||||
attributes = { sells: params[:sells] }
|
||||
attributes[:producer_profile_only] = !!params[:producer_profile_only] if params[:sells] == 'none'
|
||||
attributes[:producer_profile_only] = params[:sells] == "none" && !!params[:producer_profile_only]
|
||||
attributes[:shop_trial_start_date] = Time.now if params[:sells] == "own"
|
||||
|
||||
if %w(none own).include?(params[:sells])
|
||||
if params[:sells] == 'own' && enterprise.shop_trial_start_date
|
||||
flash[:error] = "You've already started your trial!"
|
||||
expiry = enterprise.shop_trial_start_date + Enterprise::SHOP_TRIAL_LENGTH.days
|
||||
if Time.now > expiry
|
||||
flash[:error] = "Sorry, but you've already had a trial. Expired on: #{expiry.strftime('%Y-%m-%d')}"
|
||||
else
|
||||
attributes.delete :shop_trial_start_date
|
||||
enterprise.update_attributes(attributes)
|
||||
flash[:notice] = "Welcome back! Your trial expires on: #{expiry.strftime('%Y-%m-%d')}"
|
||||
end
|
||||
elsif enterprise.update_attributes(attributes)
|
||||
flash[:success] = "Congratulations! Registration for #{enterprise.name} is complete!"
|
||||
end
|
||||
|
||||
@@ -147,18 +147,44 @@ module Admin
|
||||
end
|
||||
|
||||
context "setting 'sells' to 'own'" do
|
||||
it "is disallowed if a trial already been started" do
|
||||
before do
|
||||
enterprise.sells = 'own'
|
||||
enterprise.shop_trial_start_date = Date.today.to_time
|
||||
enterprise.save!
|
||||
spree_post :set_sells, { id: enterprise.id, sells: 'own' }
|
||||
expect(response).to redirect_to spree.admin_path
|
||||
expect(flash[:error]).to eq "You've already started your trial!"
|
||||
expect(enterprise.reload.sells).to eq 'own'
|
||||
expect(enterprise.reload.shop_trial_start_date).to eq Date.today.to_time
|
||||
end
|
||||
|
||||
context "if a trial has not already been started" do
|
||||
context "if the trial has finished" do
|
||||
before do
|
||||
enterprise.shop_trial_start_date = (Date.today - 30.days).to_time
|
||||
enterprise.save!
|
||||
end
|
||||
|
||||
it "is disallowed" do
|
||||
spree_post :set_sells, { id: enterprise.id, sells: 'own' }
|
||||
expect(response).to redirect_to spree.admin_path
|
||||
trial_expiry = Date.today.strftime("%Y-%m-%d")
|
||||
expect(flash[:error]).to eq "Sorry, but you've already had a trial. Expired on: #{trial_expiry}"
|
||||
expect(enterprise.reload.sells).to eq 'own'
|
||||
expect(enterprise.reload.shop_trial_start_date).to eq (Date.today - 30.days).to_time
|
||||
end
|
||||
end
|
||||
|
||||
context "if the trial has not finished" do
|
||||
before do
|
||||
enterprise.shop_trial_start_date = Date.today.to_time
|
||||
enterprise.save!
|
||||
end
|
||||
|
||||
it "is allowed, but trial start date is not reset" do
|
||||
spree_post :set_sells, { id: enterprise.id, sells: 'own' }
|
||||
expect(response).to redirect_to spree.admin_path
|
||||
trial_expiry = (Date.today + 30.days).strftime("%Y-%m-%d")
|
||||
expect(flash[:notice]).to eq "Welcome back! Your trial expires on: #{trial_expiry}"
|
||||
expect(enterprise.reload.sells).to eq 'own'
|
||||
expect(enterprise.reload.shop_trial_start_date).to eq Date.today.to_time
|
||||
end
|
||||
end
|
||||
|
||||
context "if a trial has not started" do
|
||||
it "is allowed" do
|
||||
spree_post :set_sells, { id: enterprise.id, sells: 'own' }
|
||||
expect(response).to redirect_to spree.admin_path
|
||||
|
||||
Reference in New Issue
Block a user