Class: Quotes::QuoteExpirationMessage

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/quotes/quote_expiration_message.rb

Constant Summary collapse

EMAIL_TEMPLATE_QUOTE_EXPIRATION =
'QUOTEEXPIRATION'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ QuoteExpirationMessage

Returns a new instance of QuoteExpirationMessage.



6
7
8
9
10
# File 'app/services/quotes/quote_expiration_message.rb', line 6

def initialize(options = {})
  @email_template = EmailTemplate.find_by(system_code: EMAIL_TEMPLATE_QUOTE_EXPIRATION)
  @email_category = @email_template.category
  super
end

Instance Attribute Details

#email_categoryObject (readonly)

Returns the value of attribute email_category.



4
5
6
# File 'app/services/quotes/quote_expiration_message.rb', line 4

def email_category
  @email_category
end

#email_templateObject (readonly)

Returns the value of attribute email_template.



4
5
6
# File 'app/services/quotes/quote_expiration_message.rb', line 4

def email_template
  @email_template
end

Class Method Details

.load_expiring_quote_records(limit: nil, expiration_range: nil) ⇒ Object



50
51
52
53
54
55
# File 'app/services/quotes/quote_expiration_message.rb', line 50

def self.load_expiring_quote_records(limit: nil, expiration_range: nil)
  expiration_range ||= Date.current...7.days.from_now.to_date
  records = Quote.sales_quotes.last_revisions.completed_quotes.with_email.joins(:opportunity).merge(Opportunity.open_opportunities).where(expiration_date: expiration_range).where(expiration_notice_sent: false)
  records = records.limit(limit) if limit.present?
  records
end

.mark_old_quotes_sentObject



61
62
63
# File 'app/services/quotes/quote_expiration_message.rb', line 61

def self.mark_old_quotes_sent
  Quote.last_revisions.completed_quotes.where(Quote[:expiration_date].lt(Date.current)).update_all(expiration_notice_sent: true)
end

.populate_quote_expirationObject



57
58
59
# File 'app/services/quotes/quote_expiration_message.rb', line 57

def self.populate_quote_expiration
  Quote.where(expiration_date: nil).where(state: 'complete').update_all("expiration_date = COALESCE((select max(c.expiration_date) from coupons c inner join discounts d on d.coupon_id = c.id and d.itemizable_id = quotes.id and d.itemizable_type = 'Quote'),quotes.created_at) + interval '60 day'")
end

Instance Method Details

#create_expiration_communication(quote) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/quotes/quote_expiration_message.rb', line 27

def create_expiration_communication(quote)
  logger.info "Creating quote expiration communication for #{quote.reference_number}"
  sender_party = quote.primary_sales_rep
  emails = quote.contact_points.emails.to_a.select { |email| EmailPreference.can_receive_email_of_category(email, email_category) }
  if emails.present?
    CommunicationBuilder.new(
      sender_party:,
      logger:,
      recipient_party: quote.primary_party,
      template: email_template,
      use_best_email: true,
      skip_resource_init: true,
      resource: quote,
      recipient_contact_points: emails,
      transmit_at: 1.hour.from_now
    ).create

  else
    logger.info "All quote contact emails: #{emails.join(',')} are suppressed for email category #{email_category}"
    nil
  end
end

#process(limit: nil, expiration_range: nil, do_not_track_notice_sent: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/quotes/quote_expiration_message.rb', line 12

def process(limit: nil, expiration_range: nil, do_not_track_notice_sent: false)
  # Find all expirations and schedule a message
  communications = []
  quotes = self.class.load_expiring_quote_records(limit:, expiration_range:)
  logger.info "Loaded #{quotes.size} quotes from db"
  count = 0
  Communication.transaction do
    quotes.find_each do |quote|
      communications << create_expiration_communication(quote)
      quote.update_column(:expiration_notice_sent, true) unless do_not_track_notice_sent
    end
  end
  { communications: communications.compact }
end