dont wait for emails

This commit is contained in:
Ulf Gebhardt 2023-04-05 02:24:51 +02:00
parent dd86e51121
commit c6369d1e0b
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
4 changed files with 19 additions and 42 deletions

View File

@ -16,13 +16,16 @@ export const sendEmailTranslated = async (params: {
}
template: string
locals: Record<string, unknown>
}): Promise<Record<string, unknown> | null> => {
let resultSend: Record<string, unknown> | null = null
}): Promise<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'
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<string, unknown>) => {
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;
}

View File

@ -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,

View File

@ -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,

View File

@ -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)