diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 3388a9b..5a1392a 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -26,6 +26,12 @@ class InvitationsController < ApplicationController end end + def email + AdminMailer.with(wedding_id: ActsAsTenant.current_tenant.id).invitations_pdf_email.deliver_later + + head :ok + end + def sheet; end def show diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb index af77c4f..c3c85d5 100644 --- a/app/mailers/admin_mailer.rb +++ b/app/mailers/admin_mailer.rb @@ -5,14 +5,41 @@ class AdminMailer < ApplicationMailer def attendance_change_email @guest = Guest.find(params[:guest_id]) - - mail( - to: @guest.wedding.users.pluck(:email), - subject: I18n.t( - 'admin_mailer.attendance_change_email.subject', - name: @guest.name, - status: I18n.t("active_record.attributes.guest/status.#{@guest.status}") + ActsAsTenant.with_tenant(@guest.wedding) do + mail( + to: recipients, + subject: I18n.t( + 'admin_mailer.attendance_change_email.subject', + name: @guest.name, + status: I18n.t("active_record.attributes.guest/status.#{@guest.status}") + ) ) - ) + end + end + + def invitations_pdf_email + ActsAsTenant.with_tenant(Wedding.find(params[:wedding_id])) do + invitations = Invitation.includes(:guests).all + + pdf_html = ActionController::Base.new.render_to_string( + template: 'invitations/sheet', + layout: 'pdf', + locals: { invitations: } + ) + pdf = WickedPdf.new.pdf_from_string(pdf_html) + + attachments["invitations_#{Time.current.strftime('%Y%m%d_%H%M%S')}.pdf"] = pdf + + mail( + to: recipients, + subject: I18n.t('admin_mailer.invitations_pdf_email.subject') + ) + end + end + + private + + def recipients + ActsAsTenant.current_tenant.users.pluck(:email) end end diff --git a/app/views/admin_mailer/invitations_pdf_email.html.erb b/app/views/admin_mailer/invitations_pdf_email.html.erb new file mode 100644 index 0000000..5976724 --- /dev/null +++ b/app/views/admin_mailer/invitations_pdf_email.html.erb @@ -0,0 +1,7 @@ +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> + +

<%= I18n.t('admin_mailer.greeting') %>,

+ +

+ <%= I18n.t('admin_mailer.invitations_pdf_email.paragraph_1') %> +

diff --git a/app/views/admin_mailer/invitations_pdf_email.txt.erb b/app/views/admin_mailer/invitations_pdf_email.txt.erb new file mode 100644 index 0000000..3b3de0f --- /dev/null +++ b/app/views/admin_mailer/invitations_pdf_email.txt.erb @@ -0,0 +1,5 @@ +<%# Copyright (C) 2024-2025 LibreWeddingPlanner contributors %> + +<%= I18n.t('admin_mailer.greeting') %>, + +<%= I18n.t('admin_mailer.invitations_pdf_email.paragraph_1') %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b85268e..b1dec72 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,3 +17,6 @@ en: subject: "%{name} has changed their attendance status: %{status}" paragraph_1: "The guest %{name} has changed their attendance for the wedding." notify_on_updates: "You will be notified of any further changes to their attendance status." + invitations_pdf_email: + subject: "Your wedding invitations are ready" + paragraph_1: "Your wedding invitations are ready. Please, find them attached to this email." diff --git a/config/routes.rb b/config/routes.rb index 4e36fa2..d9d4bf0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,9 @@ Rails.application.routes.draw do resources :tables_arrangements, only: %i[index show create] resources :summary, only: :index - resources :invitations, only: %i[show index create update destroy] + resources :invitations, only: %i[show index create update destroy] do + post :email, on: :collection + end root to: redirect("/%{slug}") end