Class: ApiEngineBase::InboxService::Blast::Upsert

Inherits:
ServiceBase
  • Object
show all
Defined in:
app/services/api_engine_base/inbox_service/blast/upsert.rb

Constant Summary

Constants inherited from ServiceBase

ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from ServiceBase

inherited, #internal_validate, #service_base_logging, #validate!

Methods included from ArgumentValidation

included

Methods included from ServiceLogging

#aletered_message, #class_name, #log, #log_error, #log_info, #log_prefix, #log_warn, #logger, #service_id

Instance Method Details

#blast_messages!(message_blast:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/api_engine_base/inbox_service/blast/upsert.rb', line 40

def blast_messages!(message_blast:)
  # Only blast messages to existing users that are brand new
  return if @existing_record
  return unless existing_users

  User.all.each do |u|
    InboxService::Message::Send.(
      user: u,
      text: text,
      title: title,
      message_blast: message_blast,
    )
  end
end

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/api_engine_base/inbox_service/blast/upsert.rb', line 16

def call
  ar = record

  ar.existing_users = existing_users
  ar.new_users = new_users
  ar.text = text
  ar.title = title
  ar.user = user

  # Probably should wrap this in a transaction block
  ar.save!
  blast_messages!(message_blast: ar)

  context.message_blast = ar
  context.blast = ApiEngineBase::Schema::Inbox::BlastResponse.new(
    existing_users:,
    new_users:,
    text:,
    title:,
    id: ar.id,
    created_by: ApiEngineBase::Schema::User.convert_user_object(user:)
  )
end

#recordObject



55
56
57
58
59
60
61
62
63
# File 'app/services/api_engine_base/inbox_service/blast/upsert.rb', line 55

def record
  return MessageBlast.new if id.nil?

  record = MessageBlast.where(id:).first
  inline_argument_failure!(errors: { id: "Message Blast ID does not exist" }) if record.nil?

  @existing_record = true
  record
end