Skip to content

Commit

Permalink
feat(api): add an EmailFactory to build emails
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 7, 2024
1 parent fe7f44b commit 196e6aa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
27 changes: 27 additions & 0 deletions api/src/shared/mail/domain/models/EmailFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getI18nForLocale } from '../../../infrastructure/i18n/i18n.js';
import { getEmailDefaultVariables } from '../emails-default-variables.js';
import { Email } from './email.js';

export class EmailFactory {
constructor({ app, locale }) {
this.app = app;
this.i18n = getI18nForLocale(locale);
this.defaultVariables = getEmailDefaultVariables();
}

buildEmail(emailParameters) {
if (!emailParameters) {
throw new Error('Email parameters are required.');
}

return new Email({
from: emailParameters.from || '[email protected]',
fromName: emailParameters.fromName || this.i18n.__(`email-sender-name.${this.app}`),
subject: emailParameters.subject,
to: emailParameters.to,
template: emailParameters.template,
variables: emailParameters.variables,
tags: emailParameters.tags,
});
}
}
63 changes: 63 additions & 0 deletions api/tests/shared/unit/mail/domain/models/EmailFactory_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Email } from '../../../../../../src/shared/mail/domain/models/email.js';
import { expect, sinon } from '../../../../../test-helper.js';
import { EmailFactory } from '.././../../../../../src/shared/mail/domain/models/EmailFactory.js';

describe('Unit | Email | Domain | Models | EmailFactory', function () {
let emailFactory;

beforeEach(function () {
emailFactory = new EmailFactory({ app: 'pix-app', locale: 'fr' });
});

afterEach(function () {
sinon.restore();
});

describe('buildEmail', function () {
it('builds an email with provided parameters', function () {
const emailParameters = {
from: '[email protected]',
fromName: 'Test Sender',
subject: 'Test Subject',
to: '[email protected]',
template: 'test-template',
variables: { key: 'value' },
tags: ['tag1', 'tag2'],
};

const email = emailFactory.buildEmail(emailParameters);

expect(email).to.be.instanceOf(Email);
expect(email.from).to.equal(emailParameters.from);
expect(email.fromName).to.equal(emailParameters.fromName);
expect(email.subject).to.equal(emailParameters.subject);
expect(email.to).to.equal(emailParameters.to);
expect(email.template).to.equal(emailParameters.template);
expect(email.variables).to.deep.equal(emailParameters.variables);
expect(email.tags).to.deep.equal(emailParameters.tags);
});

it('builds an email with default values for missing parameters', function () {
const emailParameters = {
subject: 'Test Subject',
to: '[email protected]',
template: 'test-template',
};

const email = emailFactory.buildEmail(emailParameters);

expect(email).to.be.instanceOf(Email);
expect(email.from).to.equal('[email protected]');
expect(email.fromName).to.equal('PIX - Ne pas répondre');
expect(email.subject).to.equal(emailParameters.subject);
expect(email.to).to.equal(emailParameters.to);
expect(email.template).to.equal(emailParameters.template);
expect(email.variables).to.deep.equal({});
expect(email.tags).to.be.undefined;
});

it('throws an error if emailParameters is not provided', function () {
expect(() => emailFactory.buildEmail()).to.throw('Email parameters are required.');
});
});
});

0 comments on commit 196e6aa

Please sign in to comment.