properly destructure params in sendEMailTranslated

This commit is contained in:
Ulf Gebhardt 2023-04-05 02:44:41 +02:00
parent c6369d1e0b
commit 136f282e6c
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 26 additions and 24 deletions

View File

@ -31,11 +31,11 @@ jest.mock('nodemailer', () => {
}) })
describe('sendEmailTranslated', () => { describe('sendEmailTranslated', () => {
let result: Record<string, unknown> | null let result: boolean | null
describe('config email is false', () => { describe('config email is false', () => {
beforeEach(async () => { beforeEach(() => {
result = await sendEmailTranslated({ result = sendEmailTranslated({
receiver: { receiver: {
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net', cc: 'support@gradido.net',
@ -57,9 +57,9 @@ describe('sendEmailTranslated', () => {
}) })
describe('config email is true', () => { describe('config email is true', () => {
beforeEach(async () => { beforeEach(() => {
CONFIG.EMAIL = true CONFIG.EMAIL = true
result = await sendEmailTranslated({ result = sendEmailTranslated({
receiver: { receiver: {
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net', cc: 'support@gradido.net',
@ -117,11 +117,11 @@ describe('sendEmailTranslated', () => {
}) })
describe('with email EMAIL_TEST_MODUS true', () => { describe('with email EMAIL_TEST_MODUS true', () => {
beforeEach(async () => { beforeEach(() => {
jest.clearAllMocks() jest.clearAllMocks()
CONFIG.EMAIL = true CONFIG.EMAIL = true
CONFIG.EMAIL_TEST_MODUS = true CONFIG.EMAIL_TEST_MODUS = true
result = await sendEmailTranslated({ result = sendEmailTranslated({
receiver: { receiver: {
to: 'receiver@mail.org', to: 'receiver@mail.org',
cc: 'support@gradido.net', cc: 'support@gradido.net',

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import path from 'path' import path from 'path'
import Email from 'email-templates' import Email from 'email-templates'
@ -6,17 +5,20 @@ import i18n from 'i18n'
import { createTransport } from 'nodemailer' import { createTransport } from 'nodemailer'
import CONFIG from '@/config' import CONFIG from '@/config'
import LogError from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
export const sendEmailTranslated = async (params: { export const sendEmailTranslated = ({
receiver,
template,
locals,
}: {
receiver: { receiver: {
to: string to: string
cc?: string cc?: string
} }
template: string template: string
locals: Record<string, unknown> locals: Record<string, unknown>
}): Promise<Boolean | null> => { }): boolean | null => {
// TODO: test the calling order of 'i18n.setLocale' for example: language of logging 'en', language of email receiver 'es', reset language of current user 'de' // TODO: test the calling order of 'i18n.setLocale' for example: language of logging 'en', language of email receiver 'es', reset language of current user 'de'
if (!CONFIG.EMAIL) { if (!CONFIG.EMAIL) {
@ -29,16 +31,16 @@ export const sendEmailTranslated = async (params: {
i18n.setLocale('en') // for logging i18n.setLocale('en') // for logging
logger.info( logger.info(
`send Email: language=${params.locals.locale} to=${params.receiver.to}` + `send Email: language=${locals.locale as string} to=${receiver.to}` +
(params.receiver.cc ? `, cc=${params.receiver.cc}` : '') + (receiver.cc ? `, cc=${receiver.cc}` : '') +
`, subject=${i18n.__('emails.' + params.template + '.subject')}`, `, subject=${i18n.__('emails.' + template + '.subject')}`,
) )
if (CONFIG.EMAIL_TEST_MODUS) { if (CONFIG.EMAIL_TEST_MODUS) {
logger.info( logger.info(
`Testmodus=ON: change receiver from ${params.receiver.to} to ${CONFIG.EMAIL_TEST_RECEIVER}`, `Testmodus=ON: change receiver from ${receiver.to} to ${CONFIG.EMAIL_TEST_RECEIVER}`,
) )
params.receiver.to = CONFIG.EMAIL_TEST_RECEIVER receiver.to = CONFIG.EMAIL_TEST_RECEIVER
} }
const transport = createTransport({ const transport = createTransport({
host: CONFIG.EMAIL_SMTP_URL, host: CONFIG.EMAIL_SMTP_URL,
@ -51,7 +53,7 @@ export const sendEmailTranslated = async (params: {
}, },
}) })
i18n.setLocale(params.locals.locale as string) // for email i18n.setLocale(locals.locale as string) // for email
// TESTING: see 'README.md' // TESTING: see 'README.md'
const email = new Email({ const email = new Email({
@ -65,14 +67,14 @@ export const sendEmailTranslated = async (params: {
void email void email
.send({ .send({
template: path.join(__dirname, 'templates', params.template), template: path.join(__dirname, 'templates', template),
message: params.receiver, message: receiver,
locals: params.locals, // the 'locale' in here seems not to be used by 'email-template', because it doesn't work if the language isn't set before by 'i18n.setLocale' locals, // the 'locale' in here seems not to be used by 'email-template', because it doesn't work if the language isn't set before by 'i18n.setLocale'
}) })
.catch((error: unknown) => { .catch((error: unknown) => {
new LogError('Error sending notification email', error) logger.error('Error sending notification email', error)
return false; return false
}) })
return true; return true
} }