From 33495e55ac8db9c765273fc0198b1ab60c3d4e6e Mon Sep 17 00:00:00 2001 From: drusepth Date: Thu, 10 Mar 2022 12:51:57 -0800 Subject: [PATCH] queue a downgrade job several months in the future to reset prepay storage, needs testing --- app/jobs/pay_pal_prepay_processing_job.rb | 5 +---- app/jobs/premium_downgrade_job.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 app/jobs/premium_downgrade_job.rb diff --git a/app/jobs/pay_pal_prepay_processing_job.rb b/app/jobs/pay_pal_prepay_processing_job.rb index cf88cd75..5b025b37 100644 --- a/app/jobs/pay_pal_prepay_processing_job.rb +++ b/app/jobs/pay_pal_prepay_processing_job.rb @@ -30,10 +30,7 @@ class PayPalPrepayProcessingJob < ApplicationJob # Add the extra Premium space SubscriptionService.add_any_referral_bonuses(invoice.user, 'premium') - - # Rather than queueing up a job to run N months from now to reset space, - # we'll probably go with a worker that ensures everyone's space is correct - # every day. Worker TBD. + PremiumDowngradeJob.set(wait: (invoice.months).months).perform_later(invoice.user_id) end else diff --git a/app/jobs/premium_downgrade_job.rb b/app/jobs/premium_downgrade_job.rb new file mode 100644 index 00000000..c4ad07e2 --- /dev/null +++ b/app/jobs/premium_downgrade_job.rb @@ -0,0 +1,15 @@ +class PremiumDowngradeJob < ApplicationJob + queue_as :low_priority + + def perform(*args) + user_id = args.shift + + user = User.find_by(id: user_id) + raise "No user to downgrade; id=#{user_id}" if user.nil? + + # Remove any bonus bandwidth granted by the plan + premium_bandwidth_bonus = BillingPlan.find(4).bonus_bandwidth_kb + user.update!(upload_bandwidth_kb: user.upload_bandwidth_kb - premium_bandwidth_bonus) + end + +end \ No newline at end of file