Class: BulkMessageWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/bulk_message_worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_recipients(recipient_types) ⇒ Object



18
19
20
21
22
23
24
# File 'app/workers/bulk_message_worker.rb', line 18

def self.build_recipients(recipient_types)
  recipients = Set.new
  recipient_types.each do |type|
    recipients += user_ids(type)
  end
  recipients
end

.user_ids(type) ⇒ Object

rubocop:disable CyclomaticComplexity



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
# File 'app/workers/bulk_message_worker.rb', line 27

def self.user_ids(type)
  case type
  when "all"
    User.non_admins.pluck(:id)
  when "incomplete"
    User.non_admins.pluck(:id) - Questionnaire.pluck(:user_id)
  when "complete"
    Questionnaire.pluck(:user_id)
  when "accepted"
    Questionnaire.where(acc_status: "accepted").pluck(:user_id)
  when "denied"
    Questionnaire.where(acc_status: "denied").pluck(:user_id)
  when "waitlisted"
    Questionnaire.where(acc_status: "waitlist").pluck(:user_id)
  when "late-waitlisted"
    Questionnaire.where(acc_status: "late_waitlist").pluck(:user_id)
  when "rsvp-confirmed"
    Questionnaire.where(acc_status: "rsvp_confirmed").pluck(:user_id)
  when "rsvp-denied"
    Questionnaire.where(acc_status: "rsvp_denied").pluck(:user_id)
  when "checked-in"
    Questionnaire.where("checked_in_at IS NOT NULL").pluck(:user_id)
  when "non-checked-in"
    Questionnaire.where("(acc_status = 'accepted' OR acc_status = 'rsvp_confirmed' OR acc_status = 'rsvp_denied') AND checked_in_at IS NULL").pluck(:user_id)
  when "non-checked-in-excluding"
    Questionnaire.where("acc_status != 'accepted' AND acc_status != 'rsvp_confirmed' AND acc_status != 'rsvp_denied' AND checked_in_at IS NULL").pluck(:user_id)
  when /(.*)::(\d*)/
    user_ids_from_query(type)
  else
    raise "Unknown recipient type: #{type.inspect}"
  end
end

.user_ids_from_query(type) ⇒ Object

rubocop:enable CyclomaticComplexity



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/workers/bulk_message_worker.rb', line 61

def self.user_ids_from_query(type)
  # Parse the query
  # See app/models/message_recipient_query.rb for how this works
  recipient_query = MessageRecipientQuery.parse(type)
  model = recipient_query.model

  # Build the recipients query
  case recipient_query.type
  when "bus-list"
    model.passengers.pluck(:user_id)
  when "school"
    Questionnaire.where("school_id = ? AND (acc_status = 'rsvp_confirmed' OR acc_status = 'accepted')", model.id).pluck(:user_id)
  when "blazer"
    result = Blazer::RunStatement.new.perform(Blazer.data_sources[model.data_source], model.statement)
    user_id_column = result.columns.index("user_id")
    raise "Blazer query is missing required \"user_id\" column" unless user_id_column.present?
    result.rows.map { |row| row[user_id_column] }
  else
    raise "Unknown recipient query type: #{recipient_query.type.inspect} (in message recipient query: #{type.inspect}"
  end
end

Instance Method Details

#perform(message_id) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/workers/bulk_message_worker.rb', line 4

def perform(message_id)
  message = Message.find(message_id)
  return unless message.present? && message.status == "queued"
  message.update_attribute(:started_at, Time.now)

  recipients = self.class.build_recipients(message.recipients)

  recipients.each do |recipient|
    Mailer.delay.bulk_message_email(message.id, recipient)
  end

  message.update_attribute(:delivered_at, Time.now)
end