Attempt to fix the user check problem.

This commit is contained in:
Sam Rose 2025-02-26 14:56:11 +00:00
parent 33f61a8da7
commit 8dc635b894
No known key found for this signature in database
3 changed files with 16 additions and 21 deletions

View File

@ -99,7 +99,7 @@ export interface SendEmailOpts {
// workspaceId If finer grain controls being used then this will lookup config for workspace. // workspaceId If finer grain controls being used then this will lookup config for workspace.
workspaceId?: string workspaceId?: string
// user If sending to an existing user the object can be provided, this is used in the context. // 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 If sending from an address that is not what is configured in the SMTP config.
from?: string from?: string
// contents If sending a custom email then can supply contents which will be added to it. // contents If sending a custom email then can supply contents which will be added to it.

View File

@ -28,9 +28,9 @@ export async function sendEmail(
if (userId) { if (userId) {
const db = tenancy.getGlobalDB() const db = tenancy.getGlobalDB()
user = await db.tryGet<User>(userId) user = await db.tryGet<User>(userId)
} if (!user) {
if (!user) { ctx.throw(404, "User not found.")
ctx.throw(404, "User not found.") }
} }
const response = await sendEmailFn(email, purpose, { const response = await sendEmailFn(email, purpose, {
workspaceId, workspaceId,

View File

@ -60,22 +60,6 @@ function createSMTPTransport(config?: SMTPInnerConfig) {
return nodemailer.createTransport(options) 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). * 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. * @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) const transport = createSMTPTransport(config)
// if there is a link code needed this will retrieve it // 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 context = await getSettingsTemplateContext(purpose, code)
let message: Parameters<typeof transport.sendMail>[0] = { let message: Parameters<typeof transport.sendMail>[0] = {