Skip to content

Instantly share code, notes, and snippets.

@Haegin
Created September 17, 2019 19:13
Show Gist options
  • Save Haegin/78e1fc0349bd09fb655e09ed280cbdb8 to your computer and use it in GitHub Desktop.
Save Haegin/78e1fc0349bd09fb655e09ed280cbdb8 to your computer and use it in GitHub Desktop.
```
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
helper :app_url
helper :formatting
default from: "Relay <app@#{Rails.configuration.incoming_mail_domain}>"
layout "mailer"
MAX_ATTACHMENT_SIZE = 20 * 1024 * 1024
def reply_address(discussion)
"Relay Platform <reply-#{discussion.id}@#{Rails.configuration.incoming_mail_domain}>"
end
def quote_address(quote)
"Relay Platform <quote-#{quote.id}@#{Rails.configuration.incoming_mail_domain}>"
end
end
# frozen_string_literal: true
class QuoteMailer < ApplicationMailer
def invite_reinsurer
@quote = params[:quote]
@message = params[:message]
@recipient = @quote.reinsurer
attach(@quote.worksheet.documents)
mail(
to: @recipient.email,
from: quote_address(@quote),
subject: "#{@quote.worksheet.cedent.full_name} requested a quote from you",
)
end
def quote_uploaded
@quote = params[:quote]
@message = params[:message]
@recipient = @quote.worksheet.cedent
attach(@quote.revisions.last&.documents)
mail(
to: @recipient.email,
from: reply_address(@quote.discussion),
subject: "#{@quote.reinsurer.full_name} uploaded a quote",
)
end
def quote_confirmation
@quote = params[:quote]
@recipient = @quote.reinsurer
mail(
to: @recipient.email,
from: reply_address(@quote.discussion),
subject: "Your quote has been sent to #{@quote.worksheet.cedent.full_name}",
)
end
def quote_accepted
@quote = params[:quote]
@recipient = @quote.reinsurer
@message = params[:message]
mail(
to: @recipient.email,
from: reply_address(@quote.discussion),
subject: "#{@quote.worksheet.cedent.full_name} accepted your quote",
)
end
def quote_rejected
@quote = params[:quote]
@recipient = @quote.reinsurer
@message = params[:message]
mail(
to: @recipient.email,
from: reply_address(@quote.discussion),
subject: "#{@quote.worksheet.cedent.full_name} requested a new quote",
)
end
private
def attach(file_or_files)
attachment_limit = MAX_ATTACHMENT_SIZE
[file_or_files].flatten.compact.each do |attachment|
if attachment.byte_size < attachment_limit
attachment_limit -= attachment.byte_size
attachments[attachment.blob.filename.to_s] = attachment.download
else
@attachments_excluded = true
end
end
end
end
# frozen_string_literal: true
class Revision
class CreationService
MESSAGE_SUBJECT = "Quote Submitted"
include ActiveModel::Validations
attr_reader :quote, :params, :message, :files, :files_to_attach,
:key_perils
delegate :reinsurer, :worksheet, to: :quote
validates :quote, presence: true
validate :quote_can_transition
validate :revision_is_valid
def initialize(quote:, files:, files_to_attach: [], message: nil, **params)
@quote = quote
@files = files
@params = params
@files_to_attach = files_to_attach
@key_perils = params[:key_perils]
if message
@message = quote.discussion.messages.build(
author: reinsurer,
subject: MESSAGE_SUBJECT,
body: message,
)
end
end
def revision
return if quote.blank?
@revision ||= quote.revisions.build(documents: documents, **params)
end
def save
if valid?
ActiveRecord::Base.transaction do
revision.save
message&.save
quote.receive_quote
ZipRevisionDocumentsJob.perform_later(revision.id)
QuoteMailer.with(quote: quote, message: message).quote_uploaded.deliver_later
QuoteMailer.with(quote: quote).quote_confirmation.deliver_later
worksheet.cedent.notification_events.create(
event: quote.revisions.count == 1 ? "Quote.Created" : "Quote.Modified",
triggered_by: reinsurer,
relates_to: quote,
)
end
end
end
private
def documents
files + documents_to_copy
end
def documents_to_copy
existing_attachments = ActiveStorage::Attachment
.includes(:blob)
.where(id: files_to_attach.map { |f| f[:id] })
existing_attachments.map(&:blob)
end
def revision_is_valid
return if revision.blank?
unless revision.valid?
revision.errors.each do |key, error|
errors.add(key, error)
end
end
end
def quote_can_transition
return if quote.blank?
errors.add(:quote, "must be ready to receive quote") unless quote.can_receive_quote?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment