diff --git a/packages/types/src/documents/app/automation/automation.ts b/packages/types/src/documents/app/automation/automation.ts index cfe2ba5147..dd85e1f3aa 100644 --- a/packages/types/src/documents/app/automation/automation.ts +++ b/packages/types/src/documents/app/automation/automation.ts @@ -99,7 +99,7 @@ export interface SendEmailOpts { // workspaceId If finer grain controls being used then this will lookup config for workspace. workspaceId?: string // user If sending to an existing user the object can be provided, this is used in the context. - user: User + user?: User // from If sending from an address that is not what is configured in the SMTP config. from?: string // contents If sending a custom email then can supply contents which will be added to it. diff --git a/packages/worker/src/api/controllers/global/email.ts b/packages/worker/src/api/controllers/global/email.ts index ed2d9b5125..674fe5955f 100644 --- a/packages/worker/src/api/controllers/global/email.ts +++ b/packages/worker/src/api/controllers/global/email.ts @@ -28,9 +28,9 @@ export async function sendEmail( if (userId) { const db = tenancy.getGlobalDB() user = await db.tryGet(userId) - } - if (!user) { - ctx.throw(404, "User not found.") + if (!user) { + ctx.throw(404, "User not found.") + } } const response = await sendEmailFn(email, purpose, { workspaceId, diff --git a/packages/worker/src/utilities/email.ts b/packages/worker/src/utilities/email.ts index a2b9c3bfc2..d3fac15860 100644 --- a/packages/worker/src/utilities/email.ts +++ b/packages/worker/src/utilities/email.ts @@ -60,22 +60,6 @@ function createSMTPTransport(config?: SMTPInnerConfig) { return nodemailer.createTransport(options) } -async function getLinkCode( - purpose: EmailTemplatePurpose, - email: string, - user: User, - info: any = null -) { - switch (purpose) { - case EmailTemplatePurpose.PASSWORD_RECOVERY: - return cache.passwordReset.createCode(user._id!, info) - case EmailTemplatePurpose.INVITATION: - return cache.invite.createCode(email, info) - default: - return null - } -} - /** * Builds an email using handlebars and the templates found in the system (default or otherwise). * @param purpose the purpose of the email being built, e.g. invitation, password reset. @@ -159,7 +143,18 @@ export async function sendEmail( } const transport = createSMTPTransport(config) // if there is a link code needed this will retrieve it - const code = await getLinkCode(purpose, email, opts.user, opts?.info) + let code: string | null = null + switch (purpose) { + case EmailTemplatePurpose.PASSWORD_RECOVERY: + if (!opts.user || !opts.user._id) { + throw "User must be provided for password recovery." + } + code = await cache.passwordReset.createCode(opts.user._id, opts.info) + break + case EmailTemplatePurpose.INVITATION: + code = await cache.invite.createCode(email, opts.info) + break + } let context = await getSettingsTemplateContext(purpose, code) let message: Parameters[0] = {