-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathu-case.rb
59 lines (45 loc) · 1.29 KB
/
u-case.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
52
53
54
55
56
57
58
59
class CreateResponse < Micro::Case
attributes :responder, :answers, :survey
def call!
survey_response = responder.survey_responses.build(
response_text: answers[:text],
rating: answers[:rating],
survey: survey
)
return Success result: attributes(:responder, :survey) if survey_response.save
Failure :survey_response_errors, result: survey_response.errors
end
end
class AddRewardPoints < Micro::Case
attributes :responder, :survey
def call!
reward_account = responder.reward_account
reward_account.balance += survey.reward_points
return Success, result: attributes if reward_account.save
Failure :reward_account_errors, result: reward_account.errors
end
end
class SendNotifications < Micro::Case
attributes :responder, :survey
def call!
sender = survey.sender
SurveyMailer.delay.notify_responder(responder.id)
SurveyMailer.delay.notify_sender(sender.id)
if sender.add_survey_response_notification
Success, result: attributes(:survey)
else
Failure :sender_errors, result: sender.errors
end
end
end
class ReplyToSurvey < Micro::Case
flow CreateResponse,
AddRewardPoints,
SendNotifications
end
# or
ReplyToSurvey = Micro::Cases.flow([
CreateResponse,
AddRewardPoints,
SendNotifications
])