From 119409276c2d02ed67b968cb19df81863feda60c Mon Sep 17 00:00:00 2001 From: Orlando Del Aguila Date: Thu, 10 Sep 2020 16:55:12 -0500 Subject: [PATCH 1/8] dev: fix libre franklin font weights --- app/assets/stylesheets/_main-nav.scss | 9 ++++----- app/views/layouts/admin.html.erb | 2 +- app/views/layouts/application.html.erb | 10 ++-------- app/views/layouts/backoffice.html.erb | 2 +- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/app/assets/stylesheets/_main-nav.scss b/app/assets/stylesheets/_main-nav.scss index fba77fc3..62deb412 100644 --- a/app/assets/stylesheets/_main-nav.scss +++ b/app/assets/stylesheets/_main-nav.scss @@ -5,7 +5,7 @@ align-items: center; padding: 1rem 5rem; - @media(min-width: 80rem) { + @media (min-width: 80rem) { justify-content: space-between; } @@ -17,7 +17,7 @@ margin-right: auto; } - @media(min-width: 80rem) { + @media (min-width: 80rem) { display: flex; } } @@ -27,10 +27,9 @@ li { margin: 0 2rem; - font-weight: bold; } - @media(min-width: 80rem) { + @media (min-width: 80rem) { display: flex; } } @@ -49,7 +48,7 @@ display: none; padding: 1rem 3rem; - @media(min-width: 80rem) { + @media (min-width: 80rem) { display: flex; } } diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index d6cf527d..c327e787 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -8,7 +8,7 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - + <%= render 'layouts/sentry' %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5b872f77..a49eeaa0 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,14 +7,8 @@ <%= csp_meta_tag %> <%= content_for?(:head) ? yield(:head) : '' %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - - + + diff --git a/app/views/layouts/backoffice.html.erb b/app/views/layouts/backoffice.html.erb index 8e16f4b4..1a5ec35c 100644 --- a/app/views/layouts/backoffice.html.erb +++ b/app/views/layouts/backoffice.html.erb @@ -9,7 +9,7 @@ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> - + From 4d53050de255534dff2642536b969786eb4854e6 Mon Sep 17 00:00:00 2001 From: Orlando Del Aguila Date: Thu, 10 Sep 2020 16:55:34 -0500 Subject: [PATCH 2/8] dev: rename activePlan to currentPlan --- app/javascript/bundles/User/components/CurrentPlan.jsx | 9 ++++----- .../bundles/User/components/DonationsHistory.jsx | 2 +- .../bundles/User/components/SubscriptionCancel.jsx | 4 ++-- app/views/plan_changes/index.html.erb | 2 +- app/views/users/subscription.html.erb | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/javascript/bundles/User/components/CurrentPlan.jsx b/app/javascript/bundles/User/components/CurrentPlan.jsx index d11b8459..f923d4dd 100644 --- a/app/javascript/bundles/User/components/CurrentPlan.jsx +++ b/app/javascript/bundles/User/components/CurrentPlan.jsx @@ -18,7 +18,7 @@ const useStyles = makeStyles(theme => ({ const CHANGE_PLAN_ENDPOINT = id => `/users/${id}/plan_changes` -function CurrentPlanView ({ user, activePlan, plans }) { +function CurrentPlanView ({ user, currentPlan, plans }) { const classes = useStyles() const [changedPlan, setPlanChanged] = useState(false) @@ -27,7 +27,7 @@ function CurrentPlanView ({ user, activePlan, plans }) { await fetch(CHANGE_PLAN_ENDPOINT(user.id), { body: JSON.stringify({ plan_change: { - old_plan_id: activePlan.id, + old_plan_id: currentPlan.id, new_plan_id: selectedPlanId } }), @@ -49,8 +49,7 @@ function CurrentPlanView ({ user, activePlan, plans }) {

Change your susbcription

- The plan you're using to contribute is{' '} - {activePlan.name}. + Your membership tier is {currentPlan.name}.


@@ -78,7 +77,7 @@ function CurrentPlanView ({ user, activePlan, plans }) { ) diff --git a/app/jobs/find_overdue_subscriptions_job.rb b/app/jobs/find_overdue_subscriptions_job.rb index c308f367..e49f0e83 100644 --- a/app/jobs/find_overdue_subscriptions_job.rb +++ b/app/jobs/find_overdue_subscriptions_job.rb @@ -4,11 +4,11 @@ class FindOverdueSubscriptionsJob < ApplicationJob queue_as :default def perform - subscriptions_to_charge = Subscription.where(active: true).select do |subscription| - # create charge if there's no last_charge_at date, meaning it would be the - # first time we attempt to charge this customer this subscription. - subscription.last_charge_at ? subscription.last_charge_at <= 30.days.ago : true - end + subscriptions_to_charge = + Subscription.where(active: true).where( + 'last_charge_at <= ? OR last_charge_at = null', + 30.days.ago + ) subscriptions_to_charge.each do |subscription_to_charge| SubscriptionPaymentJob.perform_later(subscription_to_charge) diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 190fb0ce..83ad733b 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -5,12 +5,12 @@ - mailers :schedule: find_overdue_subscriptions: - every: '45m' + every: '1h' queue: default class: FindOverdueSubscriptionsJob description: 'This job finds all active overdue subscriptions and creates a sidekiq job to charge the subscriber' find_subscription_plan_changes: - every: '45m' + every: '15m' queue: default class: ChangeSubscriptionPlansJob description: 'This job finds all pending subscription changes and creates a sidekiq job to perform the plan change' diff --git a/spec/features/plan_change_spec.rb b/spec/features/plan_change_spec.rb index 6924f08c..98d542f2 100644 --- a/spec/features/plan_change_spec.rb +++ b/spec/features/plan_change_spec.rb @@ -23,7 +23,7 @@ click_link 'Change Subscription' - expect(page).to have_content('Available plans') + expect(page).to have_content('Available tiers') within "#plan-#{active_plan.id}" do expect(page).to have_content(active_plan.name) @@ -31,7 +31,7 @@ within "#plan-#{new_plan.id}" do expect(page).to have_content(new_plan.name) - click_button 'Pick plan' + click_button 'Select this tier' end expect(page).to have_content('We have changed your subscription successfully.') From 143543af00338cb5fdd2c4580ac4166bccb71242 Mon Sep 17 00:00:00 2001 From: Orlando Del Aguila Date: Mon, 14 Sep 2020 12:53:25 -0500 Subject: [PATCH 5/8] chore(renovate): run renovate the first day of the month --- renovate.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/renovate.json b/renovate.json index 0f49eb57..c36e8039 100644 --- a/renovate.json +++ b/renovate.json @@ -1,6 +1,6 @@ { "extends": ["config:base"], - "schedule": ["before 11pm on Monday"], + "schedule": ["before 4am on the first day of the month"], "bundler": { "enabled": true } From 97b996b6e0af929af0a837b45df6ea06dd0508c0 Mon Sep 17 00:00:00 2001 From: Orlando Del Aguila Date: Mon, 14 Sep 2020 18:43:06 -0500 Subject: [PATCH 6/8] fix: user should be able to change subscriptions more than once --- app/controllers/plan_changes_controller.rb | 25 +++++++----- .../bundles/User/components/CurrentPlan.jsx | 20 ++++++---- .../User/components/SubscriptionCancel.jsx | 9 +---- ...oggle_user_active_subscription_plan_job.rb | 23 +++++++---- app/models/subscription.rb | 14 ++++--- app/models/user.rb | 11 ++++- app/models/user_plan_change.rb | 12 +++++- app/views/plan_changes/index.html.erb | 2 +- config/routes.rb | 34 ++++++++-------- ...0200914231027_remove_subscription_index.rb | 5 +++ db/schema.rb | 3 +- spec/factories/subscriptions.rb | 5 +-- spec/features/plan_change_spec.rb | 2 +- ..._user_active_subscription_plan_job_spec.rb | 15 ++++--- spec/models/subscription_spec.rb | 40 ++++++++++++++++--- spec/models/user_plan_change_spec.rb | 20 ++++++++++ spec/rails_helper.rb | 4 ++ 17 files changed, 168 insertions(+), 76 deletions(-) create mode 100644 db/migrate/20200914231027_remove_subscription_index.rb diff --git a/app/controllers/plan_changes_controller.rb b/app/controllers/plan_changes_controller.rb index 1875b067..cdcc1d0e 100644 --- a/app/controllers/plan_changes_controller.rb +++ b/app/controllers/plan_changes_controller.rb @@ -9,25 +9,30 @@ def index redirect_to root_path unless current_user&.active_subscription @user = current_user + @pending_plan_change = @user.pending_plan_change @plans = Plan.all end - # GET /users/:user_id/plan_changes/new - # GET /users/:user_id/plan_changes/new.json - def new - @plan_change = UserPlanChange.new - end - # POST /users/:user_id/plan_changes # POST /users/:user_id/plan_changes.json def create - @plan = UserPlanChange.new(user_id: current_user.id, old_plan_id: plan_change_params[:old_plan_id], new_plan_id: plan_change_params[:new_plan_id], status: 'pending') + @plan_change = + current_user.user_plan_changes.new( + old_plan_id: plan_change_params[:old_plan_id], + new_plan_id: plan_change_params[:new_plan_id], + status: 'pending' + ) respond_to do |format| - if @plan.save - format.json { render json: @plan, status: :ok } + if @plan_change.save + format.html { render :index, notice: 'Plan changed successfully' } + format.json { render json: @plan_change, status: :ok } else - format.html { render :index } + format.html { render :index, notice: 'Error while changing plans' } + format.json do + render json: { errors: @plan_change.errors.full_messages }, + status: :ok + end end end end diff --git a/app/javascript/bundles/User/components/CurrentPlan.jsx b/app/javascript/bundles/User/components/CurrentPlan.jsx index fa5b485c..892abd8f 100644 --- a/app/javascript/bundles/User/components/CurrentPlan.jsx +++ b/app/javascript/bundles/User/components/CurrentPlan.jsx @@ -18,13 +18,15 @@ const useStyles = makeStyles(theme => ({ const CHANGE_PLAN_ENDPOINT = id => `/users/${id}/plan_changes` -function CurrentPlanView ({ user, currentPlan, plans }) { +function CurrentPlanView ({ user, currentPlan, pendingPlanChange, plans }) { const classes = useStyles() - const [hasPlanChange, setPlanChanged] = useState(false) + const [planChange, setPlanChange] = useState(pendingPlanChange) + const pendingPlanChangePlan = + planChange && plans.find(plan => plan.id == planChange.new_plan_id) const changePlan = async selectedPlanId => { try { - await fetch(CHANGE_PLAN_ENDPOINT(user.id), { + const response = await fetch(CHANGE_PLAN_ENDPOINT(user.id), { body: JSON.stringify({ plan_change: { old_plan_id: currentPlan.id, @@ -38,7 +40,9 @@ function CurrentPlanView ({ user, currentPlan, plans }) { }, method: 'post' }) - setPlanChanged(true) + + const json = await response.json() + setPlanChange(json) } catch (error) { console.error(error) Sentry.captureException(error) @@ -56,9 +60,11 @@ function CurrentPlanView ({ user, currentPlan, plans }) {

Available tiers

You can change your membership tier to another one at anytime.

- {hasPlanChange && ( + {planChange && (

- Your plan is scheduled to change on the next billing cycle + Your plan is scheduled to change to{' '} + {pendingPlanChangePlan.name}. The new amount will be + charged on your next billing cycle.

)} {plans.map(plan => { @@ -78,7 +84,7 @@ function CurrentPlanView ({ user, currentPlan, plans }) {