Updated the code generator to only generate after the e-mails are sent
This commit is contained in:
@@ -70,11 +70,10 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const codePromises = codes.map(async (code, index) => {
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type, code, creator: req.session.user!.id, expiryDate});
|
||||
const codeInformation = {type, code, creator: req.session.user!.id, expiryDate};
|
||||
|
||||
if (infos && infos.length > index) {
|
||||
const {email, name, passport_id} = infos[index];
|
||||
await setDoc(codeRef, {email: email.trim(), name: name.trim(), ...(passport_id ? {passport_id: passport_id.trim()} : {})}, {merge: true});
|
||||
|
||||
const transport = prepareMailer();
|
||||
const mailOptions = prepareMailOptions(
|
||||
@@ -87,11 +86,24 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
"main",
|
||||
);
|
||||
|
||||
await transport.sendMail(mailOptions);
|
||||
try {
|
||||
await transport.sendMail(mailOptions);
|
||||
await setDoc(
|
||||
codeRef,
|
||||
{...codeInformation, email: email.trim(), name: name.trim(), ...(passport_id ? {passport_id: passport_id.trim()} : {})},
|
||||
{merge: true},
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
await setDoc(codeRef, codeInformation);
|
||||
}
|
||||
});
|
||||
|
||||
Promise.all(codePromises).then(() => {
|
||||
res.status(200).json({ok: true});
|
||||
Promise.all(codePromises).then((results) => {
|
||||
res.status(200).json({ok: true, valid: results.filter((x) => x).length});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user