Build out and push automatic report generation, remove emailing of every report indiidially
This commit is contained in:
1
auto-report-mail/.dockerignore
Normal file
1
auto-report-mail/.dockerignore
Normal file
@@ -0,0 +1 @@
|
||||
node_modules
|
||||
10
auto-report-mail/Dockerfile
Normal file
10
auto-report-mail/Dockerfile
Normal file
@@ -0,0 +1,10 @@
|
||||
FROM node:18-alpine
|
||||
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ=Europe/London
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
COPY package*.json ./
|
||||
RUN npm install --production
|
||||
COPY . .
|
||||
CMD ["node", "index.js"]
|
||||
81
auto-report-mail/index.js
Normal file
81
auto-report-mail/index.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import cron from 'node-cron';
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
port: parseInt(process.env.SMTP_PORT || '587'),
|
||||
secure: false,
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
}
|
||||
});
|
||||
|
||||
export async function sendReportEmail(start, end, subject = "TrACreport Summary") {
|
||||
const url = new URL(process.env.WEBURL);
|
||||
url.searchParams.set('start', start.toISOString().slice(0, 10));
|
||||
url.searchParams.set('end', end.toISOString().slice(0, 10));
|
||||
url.searchParams.set('password', process.env.PASSWORD);
|
||||
|
||||
const maxAttempts = 3;
|
||||
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
console.log(`📡 Attempt ${attempt}: fetching ${url}`);
|
||||
|
||||
const res = await fetch(url.toString());
|
||||
if (!res.ok) throw new Error(`Fetch failed: ${res.status} ${res.statusText}`);
|
||||
|
||||
const html = await res.text();
|
||||
|
||||
await transporter.sendMail({
|
||||
from: `"TrACreport Bot" <${process.env.SMTP_USER}>`,
|
||||
to: process.env.MAIL_RECIPIENTS,
|
||||
subject,
|
||||
html,
|
||||
});
|
||||
|
||||
console.log(`✅ Report emailed: ${subject} (${start.toISOString().slice(0, 10)})`);
|
||||
return; // Success, exit function
|
||||
|
||||
} catch (err) {
|
||||
console.error(`❌ Attempt ${attempt} failed:`, err);
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
console.log(`⏳ Retrying in 5s...`);
|
||||
await delay(5000); // Wait before retry
|
||||
} else {
|
||||
console.error(`🚫 All ${maxAttempts} attempts failed. Giving up.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Daily Report
|
||||
cron.schedule('5 1 * * *', () => {
|
||||
const today = new Date();
|
||||
const yesterday = new Date(today);
|
||||
yesterday.setUTCDate(today.getUTCDate() - 1);
|
||||
sendReportEmail(yesterday, yesterday, "TrACreport Daily Report");
|
||||
});
|
||||
|
||||
// Weekly Report
|
||||
cron.schedule('10 1 * * 1', () => {
|
||||
const today = new Date();
|
||||
const end = new Date(today);
|
||||
end.setUTCDate(roday.getUTCDate() - 1);
|
||||
const start = new Date(end);
|
||||
start.setUTCDate(end.getUTCDate() - 6);
|
||||
sendReportEmail(start, end, "TrACreport Weekly Report");
|
||||
})
|
||||
|
||||
// Monthly Report
|
||||
cron.schedule('15 1 1 * *', () => {
|
||||
const today = new Date();
|
||||
const end = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), 0)); // Last day of previous month
|
||||
const start = new Date(Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), 1)); // First day of previous month
|
||||
sendReportEmail(start, end, "TrACreport Monthly Report");
|
||||
});
|
||||
|
||||
console.log('📅 TrACreport schedulers are active');
|
||||
35
auto-report-mail/package-lock.json
generated
Normal file
35
auto-report-mail/package-lock.json
generated
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "auto-report-mail",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "auto-report-mail",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"node-cron": "^4.2.0",
|
||||
"nodemailer": "^7.0.5"
|
||||
}
|
||||
},
|
||||
"node_modules/node-cron": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-4.2.0.tgz",
|
||||
"integrity": "sha512-nOdP7uH7u55w7ybQq9fusXtsResok+ErzvOBydJUPBBaQ9W+EfBaBWFPgJ8sOB7FWQednDvVBJtgP5xA0bME7Q==",
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/nodemailer": {
|
||||
"version": "7.0.5",
|
||||
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-7.0.5.tgz",
|
||||
"integrity": "sha512-nsrh2lO3j4GkLLXoeEksAMgAOqxOv6QumNRVQTJwKH4nuiww6iC2y7GyANs9kRAxCexg3+lTWM3PZ91iLlVjfg==",
|
||||
"license": "MIT-0",
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
16
auto-report-mail/package.json
Normal file
16
auto-report-mail/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "auto-report-mail",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"node-cron": "^4.2.0",
|
||||
"nodemailer": "^7.0.5"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user