ENCOA-271

This commit is contained in:
Tiago Ribeiro
2024-12-12 09:42:21 +00:00
parent 1a7d35317b
commit 578d29066f
6 changed files with 97 additions and 157 deletions

View File

@@ -40,6 +40,7 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
const generateAndSendCode = async (
code: string,
type: Type,
creator: string,
expiryDate: null | Date,
entity?: string,
info?: {
@@ -47,7 +48,7 @@ const generateAndSendCode = async (
}) => {
if (!info) {
await db.collection("codes").insertOne({
code, type, expiryDate, entity
code, type, creator, expiryDate, entity, creationDate: new Date().toISOString()
})
return true
}
@@ -70,8 +71,9 @@ const generateAndSendCode = async (
await transport.sendMail(mailOptions);
if (!previousCode) {
await db.collection("codes").insertOne({
code, type, expiryDate, entity, name: info.name.trim(), email: info.email.trim().toLowerCase(),
...(info.passport_id ? { passport_id: info.passport_id.trim() } : {})
code, type, creator, expiryDate, entity, name: info.name.trim(), email: info.email.trim().toLowerCase(),
...(info.passport_id ? { passport_id: info.passport_id.trim() } : {}),
creationDate: new Date().toISOString()
})
}
@@ -118,7 +120,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
const valid = []
for (const code of codes) {
const info = findBy(infos || [], 'code', code)
const isValid = await generateAndSendCode(code, type, expiryDate, entity, info)
const isValid = await generateAndSendCode(code, type, user.id, expiryDate, entity, info)
valid.push(isValid)
}