diff --git a/app.json b/app.json index ab2aaba5..01476560 100644 --- a/app.json +++ b/app.json @@ -4,12 +4,12 @@ "cron": [ { "command": "rake top_up_accounts", - "schedule": "0 14 * * *" + "schedule": "0 14 * * 5" } ], "scripts": { "dokku": { - "postdeploy": "rake db:migrate" - } + "postdeploy": "rake db:migrate" + } } } diff --git a/app/actions/automatic_topup_action.rb b/app/actions/automatic_topup_action.rb index d77959d3..220267bc 100644 --- a/app/actions/automatic_topup_action.rb +++ b/app/actions/automatic_topup_action.rb @@ -16,5 +16,6 @@ def perform_implementation ) TransactionsMailer.automatic_top_up_done(transaction).deliver_now + ObjectiveNotificationService.new(to).perform end end diff --git a/app/controllers/topups_controller.rb b/app/controllers/topups_controller.rb index a2b83263..fd51242e 100644 --- a/app/controllers/topups_controller.rb +++ b/app/controllers/topups_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class TopupsController < ApplicationController def create Transaction.create!( @@ -9,6 +11,7 @@ def create access_token: Devise.friendly_token ) SendNotification.call(account) + ObjectiveNotificationService.new(account).perform redirect_to request.referrer end diff --git a/app/models/objective.rb b/app/models/objective.rb index 9f77d4ca..157ecf54 100644 --- a/app/models/objective.rb +++ b/app/models/objective.rb @@ -9,6 +9,7 @@ class Objective < ApplicationRecord scope :not_archived, -> { joins(:account).where(account: { archived_at: nil }) } scope :accomplished, -> { where.not(accomplished_at: nil) } scope :not_accomplished, -> { where(accomplished_at: nil) } + scope :for, ->(account) { where(account: account) } def weeks_to_achieve return -1 if account.balance >= amount diff --git a/app/services/objective_notification_service.rb b/app/services/objective_notification_service.rb index f808c667..f492d622 100644 --- a/app/services/objective_notification_service.rb +++ b/app/services/objective_notification_service.rb @@ -1,8 +1,12 @@ # frozen_string_literal: true class ObjectiveNotificationService + def initialize(account) + @account = account + end + def perform - Objective.not_archived.each do |objective| + Objective.not_archived.not_accomplished.for(@account).each do |objective| send_almost_achieved_notification(objective) send_achieved_notification(objective) end @@ -13,19 +17,19 @@ def perform def send_almost_achieved_notification(objective) return if !goal_almost_achieved?(objective) || objective.goal_almost_achieved_notified_at - ObjectivesMailer.goal_almost_achieved(objective, recipients(objective)).deliver + ObjectivesMailer.goal_almost_achieved(objective, recipients).deliver objective.update!(goal_almost_achieved_notified_at: Time.current) end def send_achieved_notification(objective) return if !goal_achieved?(objective) || objective.goal_achieved_notified_at - ObjectivesMailer.goal_achieved(objective, recipients(objective)).deliver + ObjectivesMailer.goal_achieved(objective, recipients).deliver objective.update!(goal_achieved_notified_at: Time.current) end - def recipients(objective) - [*objective.account.account_shares.accepted.pluck(:email), objective.account.parent.user.email] + def recipients + [*@account.account_shares.accepted.pluck(:email), @account.parent.user.email] end def goal_almost_achieved?(objective) diff --git a/lib/tasks/scheduler.rake b/lib/tasks/scheduler.rake index b6290acc..97ba18d9 100644 --- a/lib/tasks/scheduler.rake +++ b/lib/tasks/scheduler.rake @@ -2,8 +2,3 @@ desc 'Top up accounts automatically' task top_up_accounts: :environment do AutomaticTopupsService.new.perform if Date.current.friday? end - -desc 'Check and send notifications for achieved objectives' -task check_objectives: :environment do - ObjectiveNotificationService.new.perform -end diff --git a/spec/services/objective_notification_service_spec.rb b/spec/services/objective_notification_service_spec.rb index 2f01b307..afb4a7ea 100644 --- a/spec/services/objective_notification_service_spec.rb +++ b/spec/services/objective_notification_service_spec.rb @@ -13,7 +13,7 @@ let(:params) { { from_account: parent, to_account: account, amount: 10 } } let(:amount) { 10 } - subject(:service) { described_class.new.perform } + subject(:service) { described_class.new(account).perform } describe '#perform' do context 'when goal is achieved' do