-
Notifications
You must be signed in to change notification settings - Fork 0
/
approve_service.rb
executable file
·51 lines (40 loc) · 1.11 KB
/
approve_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# app/services/affiliates/approve_service.rb
module Affiliates
class ApproveService
#gem interactor
include Interactor
def call
check_already_approved
ActiveRecord::Base.transaction do
approve_affiliate
synchronize
log_activity
end
rescue ActiveRecord::RecordInvalid
context.fail!(errors: affiliate.errors.full_messages)
end
private
def check_already_approved
context.fail!(errors: 'Already approved') if affiliate.approved?
end
def approve_affiliate
affiliate.update!(state: Affiliate.states[:approved], approved_at: Time.current)
end
def log_activity
Activity::AffiliateApproved.create!(subject: affiliate, payload: { user_id: current_user.id })
end
def synchronize
client = SpecialClient.new(name: ssp_account.name, apikey: ssp_account.apikey)
client.approve_affiliate(affiliate, current_user.fullname)
end
def affiliate
context.object
end
def current_user
context.user
end
def ssp_account
affiliate.ssp_account
end
end
end