Skip to content

Commit

Permalink
fix(cron): add check objectives task (#342)
Browse files Browse the repository at this point in the history
* fix(cron): add check objectives task

* fix(goal notifications) fix goal notifications
  • Loading branch information
VladislavSokov committed Jan 6, 2024
1 parent 1a04ee2 commit d0dd761
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
1 change: 1 addition & 0 deletions app/actions/automatic_topup_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ def perform_implementation
)

TransactionsMailer.automatic_top_up_done(transaction).deliver_now
ObjectiveNotificationService.new(to).perform
end
end
3 changes: 3 additions & 0 deletions app/controllers/topups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class TopupsController < ApplicationController
def create
Transaction.create!(
Expand All @@ -9,6 +11,7 @@ def create
access_token: Devise.friendly_token
)
SendNotification.call(account)
ObjectiveNotificationService.new(account).perform

redirect_to request.referrer
end
Expand Down
1 change: 1 addition & 0 deletions app/models/objective.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions app/services/objective_notification_service.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions lib/tasks/scheduler.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/services/objective_notification_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0dd761

Please sign in to comment.