From c6369d1e0b46c2fe8c34faf76c1f4682fd41a633 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 5 Apr 2023 02:24:51 +0200 Subject: [PATCH] dont wait for emails --- backend/src/emails/sendEmailTranslated.ts | 32 +++++++------------ .../resolver/ContributionMessageResolver.ts | 2 +- .../graphql/resolver/TransactionResolver.ts | 4 +-- backend/src/graphql/resolver/UserResolver.ts | 23 +++---------- 4 files changed, 19 insertions(+), 42 deletions(-) diff --git a/backend/src/emails/sendEmailTranslated.ts b/backend/src/emails/sendEmailTranslated.ts index 6d89cc257..f04fd377f 100644 --- a/backend/src/emails/sendEmailTranslated.ts +++ b/backend/src/emails/sendEmailTranslated.ts @@ -16,13 +16,16 @@ export const sendEmailTranslated = async (params: { } template: string locals: Record -}): Promise | null> => { - let resultSend: Record | null = null - +}): Promise => { // 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) { + logger.info(`Emails are disabled via config...`) + return null + } + // because language of receiver can differ from language of current user who triggers the sending - const rememberLocaleToRestore = i18n.getLocale() + // const rememberLocaleToRestore = i18n.getLocale() i18n.setLocale('en') // for logging logger.info( @@ -31,10 +34,6 @@ export const sendEmailTranslated = async (params: { `, subject=${i18n.__('emails.' + params.template + '.subject')}`, ) - if (!CONFIG.EMAIL) { - logger.info(`Emails are disabled via config...`) - return null - } if (CONFIG.EMAIL_TEST_MODUS) { logger.info( `Testmodus=ON: change receiver from ${params.receiver.to} to ${CONFIG.EMAIL_TEST_RECEIVER}`, @@ -43,7 +42,7 @@ export const sendEmailTranslated = async (params: { } const transport = createTransport({ host: CONFIG.EMAIL_SMTP_URL, - port: Number(CONFIG.EMAIL_SMTP_PORT), + port: CONFIG.EMAIL_SMTP_PORT, secure: false, // true for 465, false for other ports requireTLS: CONFIG.EMAIL_TLS, auth: { @@ -64,23 +63,16 @@ export const sendEmailTranslated = async (params: { // i18n, // is only needed if you don't install i18n }) - // ATTENTION: await is needed, because otherwise on send the email gets send in the language of the current user, because below the language gets reset - await email + void email .send({ template: path.join(__dirname, 'templates', params.template), message: params.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' }) - .then((result: Record) => { - resultSend = result - logger.info('Send email successfully !!!') - logger.info('Result: ', result) - }) .catch((error: unknown) => { - throw new LogError('Error sending notification email', error) + new LogError('Error sending notification email', error) + return false; }) - i18n.setLocale(rememberLocaleToRestore) - - return resultSend + return true; } diff --git a/backend/src/graphql/resolver/ContributionMessageResolver.ts b/backend/src/graphql/resolver/ContributionMessageResolver.ts index e0b4e7b96..a59023036 100644 --- a/backend/src/graphql/resolver/ContributionMessageResolver.ts +++ b/backend/src/graphql/resolver/ContributionMessageResolver.ts @@ -146,7 +146,7 @@ export class ContributionMessageResolver { await queryRunner.manager.update(DbContribution, { id: contributionId }, contribution) } - await sendAddedContributionMessageEmail({ + void sendAddedContributionMessageEmail({ firstName: contribution.user.firstName, lastName: contribution.user.lastName, email: contribution.user.emailContact.email, diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 4c8ab0061..0d7d7abf5 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -149,7 +149,7 @@ export const executeTransaction = async ( } finally { await queryRunner.release() } - await sendTransactionReceivedEmail({ + void sendTransactionReceivedEmail({ firstName: recipient.firstName, lastName: recipient.lastName, email: recipient.emailContact.email, @@ -160,7 +160,7 @@ export const executeTransaction = async ( transactionAmount: amount, }) if (transactionLink) { - await sendTransactionLinkRedeemedEmail({ + void sendTransactionLinkRedeemedEmail({ firstName: sender.firstName, lastName: sender.lastName, email: sender.emailContact.email, diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index d6b1f22bc..d4aa6c8b3 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -245,7 +245,7 @@ export class UserResolver { user.publisherId = publisherId logger.debug('partly faked user', user) - const emailSent = await sendAccountMultiRegistrationEmail({ + void sendAccountMultiRegistrationEmail({ firstName: foundUser.firstName, // this is the real name of the email owner, but just "firstName" would be the name of the new registrant which shall not be passed to the outside lastName: foundUser.lastName, // this is the real name of the email owner, but just "lastName" would be the name of the new registrant which shall not be passed to the outside email, @@ -258,9 +258,6 @@ export class UserResolver { ) /* uncomment this, when you need the activation link on the console */ // In case EMails are disabled log the activation link for the user - if (!emailSent) { - logger.debug(`Email not send!`) - } logger.info('createUser() faked and send multi registration mail...') return user @@ -325,8 +322,7 @@ export class UserResolver { emailContact.emailVerificationCode.toString(), ).replace(/{code}/g, redeemCode ? '/' + redeemCode : '') - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const emailSent = await sendAccountActivationEmail({ + void sendAccountActivationEmail({ firstName, lastName, email, @@ -338,10 +334,6 @@ export class UserResolver { await EVENT_EMAIL_CONFIRMATION(dbUser) - if (!emailSent) { - logger.debug(`Account confirmation link: ${activationLink}`) - } - await queryRunner.commitTransaction() logger.addContext('user', dbUser.id) } catch (e) { @@ -392,8 +384,8 @@ export class UserResolver { }) logger.info(`optInCode for ${email}=${user.emailContact}`) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const emailSent = await sendResetPasswordEmail({ + + void sendResetPasswordEmail({ firstName: user.firstName, lastName: user.lastName, email, @@ -402,13 +394,6 @@ export class UserResolver { timeDurationObject: getTimeDurationObject(CONFIG.EMAIL_CODE_VALID_TIME), }) - /* uncomment this, when you need the activation link on the console */ - // In case EMails are disabled log the activation link for the user - if (!emailSent) { - logger.debug( - `Reset password link: ${activationLink(user.emailContact.emailVerificationCode)}`, - ) - } logger.info(`forgotPassword(${email}) successful...`) await EVENT_EMAIL_FORGOT_PASSWORD(user)