Last active
November 6, 2024 12:04
-
-
Save Hasstrup/5a016c771335453b7bab1da3a05f58d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# The EmailDispatch class is responsible for dispatching emails containing generated PDFs. | |
# It uses a specified PDF generation strategy and the associated template to create the PDF, | |
# then sends it to the user via email. | |
class Templates::Templates::Contexts::EmailDispatch < BaseService | |
MailerInput = Struct.new(:user, :tmpfile, :template, keyword_init: true) | |
performs_checks | |
# Initializes a new instance of the EmailDispatch class. | |
# | |
# @param [Object] input The input data required for the email dispatch. | |
# @return [void] | |
def initialize(input:) | |
@input = input | |
end | |
# Executes the email dispatch process. | |
# | |
# @return [Integer, nil] The ID of the template if successful; nil otherwise. | |
def call | |
safely_execute do | |
pdf_generation_ctx = | |
::Templates::Templates::Contexts::PdfGeneration::Service.call( | |
input.template_id, input.strategy.to_sym | |
) | |
return fail!(pdf_generation_ctx.errors) unless pdf_generation_ctx.success? | |
# Dont do this in prod | |
PdfMailer.pdf_generated(mailer_input_from_ctx(pdf_generation_ctx)).deliver_now | |
succeed(::Templates::Template.find(input.template_id)) | |
end | |
end | |
private | |
# Constructs the input for the mailer from the PDF generation context. | |
# | |
# @param [Object] ctx The context object containing the PDF generation results. | |
# @return [Hash] A hash containing the user, temporary file, and template. | |
def mailer_input_from_ctx(ctx) | |
{ | |
tmpfile: ctx.payload, | |
template: ::Templates::Template.find(input.template_id), | |
user: User.find(input.user_id) | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment