Skip to content

Commit

Permalink
revert: changes to email-transactional.middleware.ts (deal in separat…
Browse files Browse the repository at this point in the history
…e PR)
  • Loading branch information
zxt-tzx committed Jun 24, 2023
1 parent a1e4c2a commit b2ece95
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions backend/src/email/middlewares/email-transactional.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import {
} from '@core/constants'
import {
ApiInvalidTemplateError,
ApiNotFoundError,
ApiRateLimitError,
ApiValidationError,
} from '@core/errors/rest-api.errors'
import { UploadedFile } from 'express-fileupload'

Expand Down Expand Up @@ -58,7 +56,6 @@ export const InitEmailTransactionalMiddleware = (
classification: TransactionalEmailClassification
tag: string
}
type ReqBodyWithId = ReqBody & { emailMessageTransactionalId: string }

function convertMessageModelToResponse(message: EmailMessageTransactional) {
return {
Expand Down Expand Up @@ -97,7 +94,7 @@ export const InitEmailTransactionalMiddleware = (
attachments,
classification,
tag,
} = req.body as ReqBody
}: ReqBody = req.body

const attachmentsMetadata = attachments
? attachments.map((a) => ({
Expand Down Expand Up @@ -125,13 +122,12 @@ export const InitEmailTransactionalMiddleware = (
tag,
// not sure why unknown is needed to silence TS (yet other parts of the code base can just use `as Model` directly hmm)
} as unknown as EmailMessageTransactional)
// insert id into req.body so that subsequent middlewares can use it
req.body.emailMessageTransactionalId = emailMessageTransactional.id
req.body.emailMessageTransactionalId = emailMessageTransactional.id // for subsequent middlewares to distinguish whether this is a transactional email
next()
}

async function sendMessage(
req: Request<unknown, unknown, ReqBodyWithId>,
req: Request,
res: Response,
next: NextFunction
): Promise<void> {
Expand All @@ -145,23 +141,17 @@ export const InitEmailTransactionalMiddleware = (
reply_to: replyTo,
attachments,
emailMessageTransactionalId, // added by saveMessage
}: ReqBody & {
emailMessageTransactionalId: number
} = req.body

if (typeof emailMessageTransactionalId !== 'string') {
throw new ApiValidationError(
`emailMessageTransactionalId ${emailMessageTransactionalId} is not a string`
)
}

try {
const emailMessageTransactional =
await EmailMessageTransactional.findByPk(emailMessageTransactionalId)
if (!emailMessageTransactional) {
// practically this will never happen but adding to fulfill TypeScript
// type-safety requirement
throw new ApiNotFoundError(
'Unable to find entry in email_messages_transactional'
)
throw new Error('Unable to find entry in email_messages_transactional')
}
await EmailTransactionalService.sendMessage({
subject,
Expand All @@ -171,7 +161,7 @@ export const InitEmailTransactionalMiddleware = (
replyTo:
replyTo ?? (await authService.findUser(req.session?.user?.id))?.email,
attachments,
emailMessageTransactionalId: +emailMessageTransactionalId,
emailMessageTransactionalId,
})
emailMessageTransactional.set(
'status',
Expand Down

0 comments on commit b2ece95

Please sign in to comment.