Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions apps/backend/src/eventHandlers/email/stfcEmailHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { container } from 'tsyringe';

import { stfcEmailHandler } from './stfcEmailHandler';
import { Tokens } from '../../config/Tokens';
import { EmailTemplateDataSource } from '../../datasources/EmailTemplateDataSource';
import { ApplicationEvent } from '../../events/applicationEvents';
import { Event } from '../../events/event.enum';

const ORIGINAL_ENV = process.env;
const emailTemplateDataSource = container.resolve<EmailTemplateDataSource>(
Tokens.EmailTemplateDataSource
);
const spyLogError = jest
.spyOn(Logger.logger, 'logError')
.mockImplementation(() => {});
Expand Down Expand Up @@ -66,10 +70,16 @@ describe('stfcEmailHandler', () => {
mockMailService.sendMail.mockResolvedValue({ success: true });

await stfcEmailHandler(mockEvent);
const emailTemplate =
await emailTemplateDataSource.getEmailTemplateByName(
'call-created-email'
);

expect(process.env.FBS_EMAIL).toBe(inviteEmail);
expect(mockMailService.sendMail).toHaveBeenCalledWith({
content: { template: 'call-created-email' },
content: {
template: emailTemplate?.id.toString(),
},
substitution_data: {
shortCode: 'string',
startCall: new Date(2000, 1, 1),
Expand Down Expand Up @@ -99,6 +109,10 @@ describe('stfcEmailHandler', () => {
isRejection: false,
} as ApplicationEvent;
const forcedError = new Error('SMTP down');
const emailTemplate =
await emailTemplateDataSource.getEmailTemplateByName(
'call-created-email'
);

mockMailService.sendMail.mockRejectedValueOnce(forcedError);
container.registerInstance(Tokens.MailService, mockMailService);
Expand All @@ -109,7 +123,9 @@ describe('stfcEmailHandler', () => {
await new Promise(setImmediate);

expect(mockMailService.sendMail).toHaveBeenCalledWith({
content: { template: 'call-created-email' },
content: {
template: emailTemplate?.id.toString(),
},
substitution_data: {
shortCode: 'error',
},
Expand Down
20 changes: 19 additions & 1 deletion apps/backend/src/eventHandlers/email/stfcEmailHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { container } from 'tsyringe';

import { EmailTemplateId } from './emailTemplateId';
import { Tokens } from '../../config/Tokens';
import { EmailTemplateDataSource } from '../../datasources/EmailTemplateDataSource';
import { ApplicationEvent } from '../../events/applicationEvents';
import { Event } from '../../events/event.enum';
import { MailService } from '../MailService/MailService';
Expand All @@ -15,6 +16,10 @@ export async function stfcEmailHandler(event: ApplicationEvent) {

const mailService = container.resolve<MailService>(Tokens.MailService);

const emailTemplateDataSource = container.resolve<EmailTemplateDataSource>(
Tokens.EmailTemplateDataSource
);

switch (event.type) {
case Event.CALL_CREATED: {
if (event?.call) {
Expand All @@ -33,9 +38,22 @@ export async function stfcEmailHandler(event: ApplicationEvent) {
startCall,
endCall,
}))(event.call);

const template = EmailTemplateId.CALL_CREATED_EMAIL;
const emailTemplate =
await emailTemplateDataSource.getEmailTemplateByName(template);

if (!emailTemplate) {
logger.logError('Email template not found', {
template,
});

return;
}

const sendMailOptions = callCreationEmail(
eventCallPartial,
EmailTemplateId.CALL_CREATED_EMAIL,
emailTemplate.id.toString(),
notificationEmailAddress
);

Expand Down
Loading