diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 81132b9..004f94f 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -17,4 +17,10 @@ class NotificationMailer < ApplicationMailer @subject = "New app connected to your storage" mail to: @user.email, subject: @subject end + + def new_invitations_available + @user = params[:user] + @subject = "New invitations added to your account" + mail to: @user.email, subject: @subject + end end diff --git a/app/services/create_invitations.rb b/app/services/create_invitations.rb new file mode 100644 index 0000000..3003b1a --- /dev/null +++ b/app/services/create_invitations.rb @@ -0,0 +1,17 @@ +class CreateInvitations < ApplicationService + def initialize(user:, amount:, notify: true) + @user = user + @amount = amount + @notify = notify + end + + def call + @amount.times do + Invitation.create(user: @user) + end + + if @notify + NotificationMailer.with(user: @user).new_invitations_available.deliver_later + end + end +end diff --git a/app/views/notification_mailer/new_invitations_available.text.erb b/app/views/notification_mailer/new_invitations_available.text.erb new file mode 100644 index 0000000..9cf8adc --- /dev/null +++ b/app/views/notification_mailer/new_invitations_available.text.erb @@ -0,0 +1,11 @@ +Hi <%= @user.display_name.presence || @user.cn %>, + +New invitations have just been added to your Kosmos account, so you can invite more people to our cooperative services: + +<%= invitations_url %> + +Have a nice day! + +--- + +Tip: if you want to invite someone you're meeting in person, log into your account panel on a mobile device and let people scan the invitation QR code from theirs. diff --git a/spec/services/create_invitations_spec.rb b/spec/services/create_invitations_spec.rb new file mode 100644 index 0000000..4e5de23 --- /dev/null +++ b/spec/services/create_invitations_spec.rb @@ -0,0 +1,40 @@ +require 'rails_helper' + +RSpec.describe CreateInvitations, type: :model do + include ActiveJob::TestHelper + + let(:user) { create :user } + + describe "#call" do + before do + CreateInvitations.call(user: user, amount: 5) + end + + after(:each) { clear_enqueued_jobs } + + it "creates the right amount of invitations for the given user" do + expect(user.invitations.count).to eq(5) + end + + it "sends an email notification to the user" do + expect(enqueued_jobs.size).to eq(1) + expect(enqueued_jobs.first["job_class"]).to eq("ActionMailer::MailDeliveryJob") + args = enqueued_jobs.first['arguments'] + expect(args[0]).to eq("NotificationMailer") + expect(args[1]).to eq("new_invitations_available") + expect(args[3]["params"]["user"]["_aj_globalid"]).to eq("gid://akkounts/User/1") + end + end + + describe "#call with notification disabled" do + before do + CreateInvitations.call(user: user, amount: 3, notify: false) + end + + after(:each) { clear_enqueued_jobs } + + it "does not send an email notification to the user" do + expect(enqueued_jobs.size).to eq(0) + end + end +end