Fixed API issues and added a test file

This commit is contained in:
2024-03-11 12:19:19 +01:00
parent 6d941b7bf1
commit 6452ec2dba
10 changed files with 71 additions and 31 deletions

View File

@@ -1,44 +1,45 @@
import nodemailer from 'nodemailer';
import { random } from './random';
import { log } from './logManager';
const transporter = nodemailer.createTransport({
host: process.env.SMTP,
port: 465,
secure: true,
auth: {
user: process.env.MAIL,
pass: process.env.MAIL_PASS,
user: `${process.env.MAIL}`,
pass: `${process.env.PASS}`,
},
tls: { rejectUnauthorized: false },
});
function sendMail(email, head, body) {
try {
// setup email data
const mailOptions = {
from: `"AirJet" <${process.env.MAIL}>`,
from: `"HSP-GDH" <${process.env.MAIL}>`,
to: email,
subject: head,
text: body,
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
log('Retrying connection to SMTP');
if (sendMail(email, head, body)) return true;
}
else {
console.log('Email sent: ' + info.response);
log('Email sent: ' + info.response);
return true;
}
});
return true;
}
catch (err) {
return false;
}
}
function sendVerification(email, userId, type = 'register', code = null) {
async function sendVerification(email, userId, type = 'register', code = null) {
try {
code ? code : random(100000, 999999);
code ? code : code = random(100000, 999999);
let title, body;
switch (type) {
case 'email':
@@ -56,8 +57,9 @@ function sendVerification(email, userId, type = 'register', code = null) {
default:
return false;
}
if (sendMail(email, title, body)) return code;
return false;
await sendMail(email, title, body);
return code;
}
catch (err) {
return false;