Class: Maintenance::CommunicationMaintenance
- Inherits:
-
BaseService
- Object
- BaseService
- Maintenance::CommunicationMaintenance
show all
- Defined in:
- app/services/maintenance/communication_maintenance.rb
Instance Method Summary
collapse
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#blank_out_old_marketing_email_based_on_templates(age_in_days: 365, batch_size: 500) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/services/maintenance/communication_maintenance.rb', line 34
def blank_out_old_marketing_email_based_on_templates(age_in_days: 365, batch_size: 500)
comms = Communication.where(archived: false).joins(:email_template).where(state: 'sent').where(created_at: ..age_in_days.days.ago).where.not(body: nil).where(EmailTemplate[:category].not_eq('transactional')).where(EmailTemplate[:resource_id].eq(nil))
total_count = comms.size
@logger.info "Purging communications body for email template based communications created more than #{age_in_days} ago. #{total_count} were found."
comms.in_batches(of: batch_size).each_with_index do |relation, batch_index|
current_position = (batch_index + 1) * batch_size
percentage = ((current_position.to_f / total_count) * 100).round(2)
@logger.info "[#{current_position} / #{total_count} - #{percentage}%] Purging batch #{batch_index}"
Communication.transaction do
relation.update_all(body: nil, archived: true)
end
end
@logger.info "Completed, body purged in #{total_count} records"
end
|
#deactivate_unused_templates ⇒ Object
54
55
56
57
58
59
|
# File 'app/services/maintenance/communication_maintenance.rb', line 54
def deactivate_unused_templates
EmailTemplate.transaction do
EmailTemplate.active.non_system.where('created_at <= ?',
6.months.ago).where.not(['exists(select 1 from communications where communications.email_template_id = email_templates.id and communications.created_at > ?)', 2.years.ago]).each(&:archived!)
end
end
|
#process ⇒ Object
2
3
4
5
6
7
8
9
10
11
|
# File 'app/services/maintenance/communication_maintenance.rb', line 2
def process
PaperTrail.request(whodunnit: 'Maintenance::CommunicationMaintenance') do
purge_old_drafts
purge_old_exceptions
sync_states
deactivate_unused_templates
blank_out_old_marketing_email_based_on_templates
end
end
|
#purge_old_drafts(age_in_days: 30) ⇒ Object
13
14
15
16
17
18
|
# File 'app/services/maintenance/communication_maintenance.rb', line 13
def purge_old_drafts(age_in_days: 30)
comm = Communication.where(state: 'draft', transmit_at: nil).where(created_at: ..age_in_days.days.ago)
@logger.info "Purging communications in draft created more than #{age_in_days} ago. #{comm.size} were found."
comm.each(&:destroy)
@logger.info 'Completed, draft communications destroyed'
end
|
#purge_old_exceptions(age_in_days: 2) ⇒ Object
20
21
22
23
24
25
|
# File 'app/services/maintenance/communication_maintenance.rb', line 20
def purge_old_exceptions(age_in_days: 2)
comm = Communication.where(state: 'exception', transmit_at: nil).where(created_at: ..age_in_days.days.ago)
@logger.info "Purging communications in exception created more than #{age_in_days} ago. #{comm.size} were found."
comm.each(&:destroy)
@logger.info 'Completed, exception communications destroyed'
end
|
#purge_old_suppressions(age_in_days: 90) ⇒ Object
27
28
29
30
31
32
|
# File 'app/services/maintenance/communication_maintenance.rb', line 27
def purge_old_suppressions(age_in_days: 90)
comm = Communication.where(state: 'suppressed', transmit_at: nil).where(created_at: ..age_in_days.days.ago)
@logger.info "Purging communications in suppression created more than #{age_in_days} ago. #{comm.size} were found."
comm.each(&:destroy)
@logger.info 'Completed, suppression communications destroyed'
end
|
#requested_counter_reconciliation ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'app/services/maintenance/communication_maintenance.rb', line 61
def requested_counter_reconciliation
sql = <<-SQL
select i.id as pub_id, count(*) as counter, max(i.name) as name, max(i.sku) as sku from
communications c
inner join communications_uploads cu on cu.communication_id = c.id
inner join uploads u on u.id = cu.upload_id
inner join items i on i.literature_id = u.id
where c.state = 'sent'
group by i.id
SQL
res = ActiveRecord::Base.connection.execute(sql)
msgs = []
Item.transaction do
res.each do |r|
pub_id = r['pub_id'].to_i
m = "Updating #{r['name']} (#{r['sku']}) with #{r['counter']} requests"
Item.where(id: pub_id).update_all(requested_counter: r['counter'])
msgs << m
end
end
msgs
end
|
#sync_states ⇒ Object
49
50
51
52
|
# File 'app/services/maintenance/communication_maintenance.rb', line 49
def sync_states
Communication.where(state: 'draft').where.not(transmit_at: nil).update_all(state: 'sent')
@logger.info 'Marking communications with a transmit date as sent from draft'
end
|