mirror of
https://github.com/Ravinou/borgwarehouse
synced 2024-06-10 01:42:17 +02:00
feat: send email alert with cronjob API
This commit is contained in:
parent
31ec7bae8f
commit
ff82d87aee
|
@ -6,6 +6,8 @@ import { promises as fs } from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
const util = require('node:util');
|
const util = require('node:util');
|
||||||
const exec = util.promisify(require('node:child_process').exec);
|
const exec = util.promisify(require('node:child_process').exec);
|
||||||
|
import nodemailerSMTP from '../../../helpers/functions/nodemailerSMTP';
|
||||||
|
import emailTest from '../../../helpers/templates/emailTest';
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
if (req.headers.authorization == null) {
|
if (req.headers.authorization == null) {
|
||||||
|
@ -19,9 +21,17 @@ export default async function handler(req, res) {
|
||||||
const CRONJOB_KEY = process.env.CRONJOB_KEY;
|
const CRONJOB_KEY = process.env.CRONJOB_KEY;
|
||||||
const ACTION_KEY = req.headers.authorization.split(' ')[1];
|
const ACTION_KEY = req.headers.authorization.split(' ')[1];
|
||||||
|
|
||||||
try {
|
if (req.method == 'POST' && ACTION_KEY === CRONJOB_KEY) {
|
||||||
if (req.method == 'POST' && ACTION_KEY === CRONJOB_KEY) {
|
//Var
|
||||||
////Call the shell : getLastSave.sh
|
let newRepoList;
|
||||||
|
let repoListToSendAlert = [];
|
||||||
|
let usersList;
|
||||||
|
const date = Math.round(Date.now() / 1000);
|
||||||
|
const jsonDirectory = path.join(process.cwd(), '/config');
|
||||||
|
|
||||||
|
////PART 1 : Status
|
||||||
|
try {
|
||||||
|
//Call the shell : getLastSave.sh
|
||||||
//Find the absolute path of the shells directory
|
//Find the absolute path of the shells directory
|
||||||
const shellsDirectory = path.join(process.cwd(), '/helpers');
|
const shellsDirectory = path.join(process.cwd(), '/helpers');
|
||||||
//Exec the shell
|
//Exec the shell
|
||||||
|
@ -41,7 +51,6 @@ export default async function handler(req, res) {
|
||||||
const lastSave = JSON.parse(stdout);
|
const lastSave = JSON.parse(stdout);
|
||||||
|
|
||||||
//Find the absolute path of the json directory
|
//Find the absolute path of the json directory
|
||||||
const jsonDirectory = path.join(process.cwd(), '/config');
|
|
||||||
let repoList = await fs.readFile(
|
let repoList = await fs.readFile(
|
||||||
jsonDirectory + '/repo.json',
|
jsonDirectory + '/repo.json',
|
||||||
'utf8'
|
'utf8'
|
||||||
|
@ -49,10 +58,8 @@ export default async function handler(req, res) {
|
||||||
//Parse the repoList
|
//Parse the repoList
|
||||||
repoList = JSON.parse(repoList);
|
repoList = JSON.parse(repoList);
|
||||||
|
|
||||||
//Rebuild a newRepoList with the lasSave timestamp updated
|
//Rebuild a newRepoList with the lastSave timestamp updated and the status updated.
|
||||||
let repoToSendAlert = [];
|
newRepoList = repoList;
|
||||||
const date = Math.round(Date.now() / 1000);
|
|
||||||
let newRepoList = repoList;
|
|
||||||
for (let index in newRepoList) {
|
for (let index in newRepoList) {
|
||||||
const repoFiltered = lastSave.filter(
|
const repoFiltered = lastSave.filter(
|
||||||
(x) => x.user === newRepoList[index].unixUser
|
(x) => x.user === newRepoList[index].unixUser
|
||||||
|
@ -72,50 +79,108 @@ export default async function handler(req, res) {
|
||||||
) {
|
) {
|
||||||
newRepoList[index].status = true;
|
newRepoList[index].status = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//// TESTING START ////
|
|
||||||
|
|
||||||
//Trigger lastStatusAlertSend
|
|
||||||
//Here, a mail is sent every 24H (90000) if a repo has down status
|
|
||||||
if (
|
|
||||||
!newRepoList[index].status &&
|
|
||||||
(!newRepoList[index].lastStatusAlertSend ||
|
|
||||||
date - newRepoList[index].lastStatusAlertSend >
|
|
||||||
90000)
|
|
||||||
) {
|
|
||||||
repoToSendAlert.push(newRepoList[index].alias);
|
|
||||||
newRepoList[index].lastStatusAlertSend = date;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Send email alert
|
} catch (err) {
|
||||||
console.log(repoToSendAlert);
|
res.status(500).json({
|
||||||
if (repoToSendAlert.length > 0) {
|
status: 500,
|
||||||
console.log('ENVOI EMAIL');
|
message: "API error : can't update the status.",
|
||||||
}
|
});
|
||||||
//// TESTING END ////
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//// PART 2 : check if there is a repo that need an email alert
|
||||||
|
try {
|
||||||
|
//Here, a mail is sent every 24H (90000) if a repo has down status
|
||||||
|
for (let index in newRepoList) {
|
||||||
|
if (
|
||||||
|
!newRepoList[index].status &&
|
||||||
|
(!newRepoList[index].lastStatusAlertSend ||
|
||||||
|
date - newRepoList[index].lastStatusAlertSend > 90000)
|
||||||
|
) {
|
||||||
|
repoListToSendAlert.push(newRepoList[index].alias);
|
||||||
|
newRepoList[index].lastStatusAlertSend = date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({
|
||||||
|
status: 500,
|
||||||
|
message:
|
||||||
|
"API error : can't check if a repo needs an email alert.",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//PART 3 : Save the new repoList
|
||||||
|
try {
|
||||||
//Stringify the repoList to write it into the json file.
|
//Stringify the repoList to write it into the json file.
|
||||||
newRepoList = JSON.stringify(newRepoList);
|
newRepoList = JSON.stringify(newRepoList);
|
||||||
//Write the new json
|
//Write the new json
|
||||||
fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => {
|
fs.writeFile(jsonDirectory + '/repo.json', newRepoList, (err) => {
|
||||||
if (err) console.log(err);
|
if (err) console.log(err);
|
||||||
});
|
});
|
||||||
|
} catch (err) {
|
||||||
res.status(200).json({
|
res.status(500).json({
|
||||||
success: 'Status cron has been executed.',
|
status: 500,
|
||||||
|
message: "API error : can't write the new repoList.",
|
||||||
});
|
});
|
||||||
} else {
|
return;
|
||||||
res.status(401).json({
|
}
|
||||||
status: 401,
|
console.log(repoListToSendAlert);
|
||||||
message: 'Unauthorized',
|
//PART 4 : Send the mail alert
|
||||||
|
if (repoListToSendAlert.length > 0) {
|
||||||
|
// Read user informations
|
||||||
|
console.log(repoListToSendAlert);
|
||||||
|
try {
|
||||||
|
//Read the email of the user
|
||||||
|
usersList = await fs.readFile(
|
||||||
|
jsonDirectory + '/users.json',
|
||||||
|
'utf8'
|
||||||
|
);
|
||||||
|
//Parse the usersList
|
||||||
|
usersList = JSON.parse(usersList);
|
||||||
|
|
||||||
|
console.log('test');
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({
|
||||||
|
status: 500,
|
||||||
|
message: "API error : can't read user information.",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send mail
|
||||||
|
//Create the SMTP Transporter
|
||||||
|
const transporter = nodemailerSMTP();
|
||||||
|
//Mail options
|
||||||
|
const mailData = emailTest(
|
||||||
|
usersList[0].email,
|
||||||
|
usersList[0].username
|
||||||
|
);
|
||||||
|
transporter.sendMail(mailData, function (err, info) {
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
res.status(400).json({
|
||||||
|
message:
|
||||||
|
'An error occured while sending the email : ' + err,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
console.log(info);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.log(err);
|
//PART 5 : Sucess
|
||||||
res.status(500).json({
|
res.status(200).json({
|
||||||
status: 500,
|
success: 'Status cron has been executed.',
|
||||||
message: 'API error, contact the administrator.',
|
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
res.status(401).json({
|
||||||
|
status: 401,
|
||||||
|
message: 'Unauthorized',
|
||||||
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue