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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/tolliver/services/notification_service.rb', line 19
def notify(options)
options = options.deep_symbolize_keys
notification = Tolliver.notification_model.new
Tolliver.notification_model.transaction do
template = options[:template]
raise Tolliver::Errors::BadRequest.new('Missing template.') if template.blank?
notification_template = Tolliver.notification_template_model.where(ref: template).first
notification_template = Tolliver.notification_template_model.where(id: template).first if notification_template.nil?
raise Tolliver::Errors::NotFound.new("Template #{template.to_s} not found.") if notification_template.nil?
if !notification_template.is_disabled && !notification_template.message.blank?
params = map_params(options[:params] || {})
notification.message = eval_expressions(interpret_named_params(notification_template.message, params))
notification.subject = eval_expressions(interpret_named_params(notification_template.subject, params))
notification.notification_template = notification_template
unless options[:signature].blank?
notification.message += options[:signature].to_s
end
notification.save
unless options[:attachments].blank?
attachments = options[:attachments]
attachments = [attachments] unless attachments.is_a?(Array)
attachments.each do |attachment|
raise Tolliver::Errors::BadRequest.new('Missing attachment name.') if attachment[:name].blank?
raise Tolliver::Errors::BadRequest.new('Missing attachment data.') if attachment[:attachment].blank?
notification.notification_attachments.create(name: attachment[:name], attachment: attachment[:attachment])
end
end
unless notification_template.is_dry
if options[:methods].blank?
methods = Tolliver.delivery_methods
else
methods = options[:methods]
methods = [methods] unless methods.is_a?(Array)
methods = methods.delete_if { |method| !Tolliver.delivery_methods.include?(method.to_sym) }
end
methods.each do |method|
notification_delivery = notification.notification_deliveries.create(method: method)
if options[:is_postponed]
notification_delivery.is_postponed = options[:is_postponed]
end
unless options[:sender].blank?
raise Tolliver::Errors::BadRequest.new('Missing sender ref.') if options[:sender][:ref].blank?
raise Tolliver::Errors::BadRequest.new('Missing sender contact.') if options[:sender][:contact].blank?
notification_delivery.sender_ref = options[:sender][:ref]
notification_delivery.sender_contact = options[:sender][:contact]
end
receivers = options[:receivers] || []
raise Tolliver::Errors::BadRequest.new('Missing receivers.') if receivers.blank? || receivers.empty?
receivers = [receivers] unless receivers.is_a?(Array)
filtered_receivers = receivers.dup
filtered_receivers.each do |receiver|
raise Tolliver::Errors::BadRequest.new('Missing receiver ref.') if receiver[:ref].blank?
raise Tolliver::Errors::BadRequest.new('Missing receiver contact.') if receiver[:contact].blank?
notification_delivery.notification_receivers.create(receiver_ref: receiver[:ref], receiver_contact: receiver[:contact])
end
notification_delivery.sent_count = 0
notification_delivery.receivers_count = filtered_receivers.size
notification_delivery.save
end
end
else
notification = nil end
end
if !notification.nil? && !options[:is_postponed]
notification.enqueue_for_delivery
end
notification
end
|