-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add an EmailFactory to build emails
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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
63
api/tests/shared/unit/mail/domain/models/EmailFactory_test.js
This file contains 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
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.'); | ||
}); | ||
}); | ||
}); |