/
action_service.rb
91 lines (66 loc) 路 2.26 KB
/
action_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class ActionService
include EmailHelper
def initialize(conversation)
@conversation = conversation.reload
end
def mute_conversation(_params)
@conversation.mute!
end
def snooze_conversation(_params)
@conversation.snoozed!
end
def resolve_conversation(_params)
@conversation.resolved!
end
def change_status(status)
@conversation.update!(status: status[0])
end
def change_priority(priority)
@conversation.update!(priority: (priority[0] == 'nil' ? nil : priority[0]))
end
def add_label(labels)
return if labels.empty?
@conversation.reload.add_labels(labels)
end
def assign_agent(agent_ids = [])
return @conversation.update!(assignee_id: nil) if agent_ids[0] == 'nil'
return unless agent_belongs_to_inbox?(agent_ids)
@agent = @account.users.find_by(id: agent_ids)
@conversation.update!(assignee_id: @agent.id) if @agent.present?
end
def remove_label(labels)
return if labels.empty?
labels = @conversation.label_list - labels
@conversation.update(label_list: labels)
end
def assign_team(team_ids = [])
return unassign_team if team_ids[0]&.zero?
# check if team belongs to account only if team_id is present
# if team_id is nil, then it means that the team is being unassigned
return unless !team_ids[0].nil? && team_belongs_to_account?(team_ids)
@conversation.update!(team_id: team_ids[0])
end
def remove_assigned_team(_params)
@conversation.update!(team_id: nil)
end
def send_email_transcript(emails)
emails = emails[0].gsub(/\s+/, '').split(',')
emails.each do |email|
email = parse_email_variables(@conversation, email)
ConversationReplyMailer.with(account: @conversation.account).conversation_transcript(@conversation, email)&.deliver_later
end
end
private
def agent_belongs_to_inbox?(agent_ids)
member_ids = @conversation.inbox.members.pluck(:user_id)
assignable_agent_ids = member_ids + @account.administrators.ids
assignable_agent_ids.include?(agent_ids[0])
end
def team_belongs_to_account?(team_ids)
@account.team_ids.include?(team_ids[0])
end
def conversation_a_tweet?
return false if @conversation.additional_attributes.blank?
@conversation.additional_attributes['type'] == 'tweet'
end
end