GWM : corn for reminder email
This commit is contained in:
parent
2e00784921
commit
03dfdd39cd
@ -2,6 +2,7 @@ const cron = require('node-cron');
|
||||
const mysql = require('mysql2/promise');
|
||||
const SubmissionAutoFillService = require('./auto_fill_missing_quarterly_submissions_service'); // Adjust path
|
||||
const IPICalculationService = require('./ipi_calculation_service');
|
||||
const { sendEmailService } = require("./email.service");
|
||||
|
||||
// Database configuration
|
||||
const dbConfig = {
|
||||
@ -36,6 +37,7 @@ class AutomatedSchedulerService {
|
||||
try {
|
||||
const query = `
|
||||
SELECT
|
||||
survey_name,
|
||||
year,
|
||||
quarter,
|
||||
end_date,
|
||||
@ -265,150 +267,169 @@ class AutomatedSchedulerService {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||
const connection = await this.pool.getConnection();
|
||||
|
||||
try {
|
||||
const query = `
|
||||
SELECT
|
||||
e.id AS establishment_id,
|
||||
e.factory_name,
|
||||
eu.id AS user_id,
|
||||
eu.name AS user_name,
|
||||
eu.email
|
||||
FROM establishments e
|
||||
LEFT JOIN establishment_users eu
|
||||
ON eu.establishment_id = e.id AND eu.is_active = 1
|
||||
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM submissions s
|
||||
WHERE s.establishment_id = e.id
|
||||
AND s.year = ?
|
||||
AND s.quarter = ?
|
||||
AND s.status != 'Draft'
|
||||
)
|
||||
`;
|
||||
|
||||
const [rows] = await connection.query(query, [year, quarter]);
|
||||
|
||||
return rows;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error fetching pending establishments:", error);
|
||||
return [];
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async sendReminderEmails(rows, latestWindow) {
|
||||
try {
|
||||
if (!rows.length) {
|
||||
console.log("No pending establishments.");
|
||||
return;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||
|
||||
const connection = await this.pool.getConnection();
|
||||
|
||||
try {
|
||||
const query = `
|
||||
SELECT
|
||||
e.id AS establishment_id,
|
||||
e.factory_name,
|
||||
eu.id AS user_id,
|
||||
eu.name AS user_name,
|
||||
eu.email
|
||||
FROM establishments e
|
||||
LEFT JOIN establishment_users eu
|
||||
ON eu.establishment_id = e.id AND eu.is_active = 1
|
||||
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM submission s
|
||||
WHERE s.establishment_id = e.id
|
||||
AND s.year = ?
|
||||
AND s.quarter = ? ) AND e.is_active = 1
|
||||
`;
|
||||
|
||||
const [rows] = await connection.query(query, [year, quarter]);
|
||||
|
||||
return rows;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error fetching pending establishments:", error);
|
||||
return [];
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
const surveyData = await this.getSurveyData(
|
||||
async sendReminderEmails(rows, latestWindow) {
|
||||
try {
|
||||
if (!rows.length) {
|
||||
console.log("No pending establishments.");
|
||||
return;
|
||||
}
|
||||
|
||||
const basePlaceholder = {
|
||||
survey_name: latestWindow.survey_name,
|
||||
quarter: latestWindow.quarter,
|
||||
year: latestWindow.year,
|
||||
opens_on: latestWindow.start_date,
|
||||
closes_on: latestWindow.end_date,
|
||||
portal_url: process.env.FE_BASE_URL,
|
||||
support_email: process.env.SUPPORT_EMAIL,
|
||||
support_phone: process.env.SUPPORT_PHONE,
|
||||
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`
|
||||
};
|
||||
|
||||
|
||||
|
||||
const promises = rows.map((r) =>
|
||||
sendEmailService(r.email, "submission_last_day_reminder_mail", {
|
||||
...basePlaceholder,
|
||||
user_name: r.user_name,
|
||||
establishment_name: r.factory_name,
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
console.log(`Sent ${promises.length} reminder emails.`);
|
||||
|
||||
} catch (err) {
|
||||
console.error("Reminder email error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
async sendEmailReminders(latestWindow) {
|
||||
console.log(
|
||||
`[${new Date().toISOString()}] Sending reminders for Year=${latestWindow.year}, Quarter=${latestWindow.quarter}`
|
||||
);
|
||||
|
||||
const establishments = await this.getEstablishmentsWithoutSubmission(
|
||||
latestWindow.year,
|
||||
latestWindow.quarter
|
||||
);
|
||||
|
||||
const basePlaceholder = {
|
||||
quarter: latestWindow.quarter,
|
||||
year: latestWindow.year,
|
||||
survey_name: surveyData?.survey_name,
|
||||
support_email: process.env.SUPPORT_EMAIL,
|
||||
support_phone: process.env.SUPPORT_PHONE,
|
||||
portal_url: process.env.FE_BASE_URL,
|
||||
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`,
|
||||
};
|
||||
if (!establishments.length) {
|
||||
console.log("No pending establishments found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const promises = rows.map((r) =>
|
||||
sendEmailService(r.email, "submission_reminder_mail", {
|
||||
...basePlaceholder,
|
||||
user_name: r.user_name,
|
||||
establishment_name: r.factory_name,
|
||||
})
|
||||
);
|
||||
await this.sendReminderEmails(establishments, latestWindow);
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
console.log(`Sent ${promises.length} reminder emails.`);
|
||||
|
||||
} catch (err) {
|
||||
console.error("Reminder email error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async sendEmailReminders(latestWindow) {
|
||||
console.log(
|
||||
`[${new Date().toISOString()}] Sending reminders for Year=${latestWindow.year}, Quarter=${latestWindow.quarter}`
|
||||
);
|
||||
|
||||
const establishments = await getEstablishmentsWithoutSubmission(
|
||||
latestWindow.year,
|
||||
latestWindow.quarter
|
||||
);
|
||||
|
||||
if (!establishments.length) {
|
||||
console.log("No pending establishments found.");
|
||||
return;
|
||||
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
|
||||
}
|
||||
|
||||
await sendReminderEmails(establishments, latestWindow);
|
||||
async runScheduledEmails() {
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
|
||||
}
|
||||
//find latest quarterly window configuration
|
||||
const latestWindow = await this.getLatestQuarterlyWindow();
|
||||
|
||||
if (!latestWindow) {
|
||||
console.log(`[${new Date().toISOString()}] No quarterly window configuration found for email reminders`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Latest window for email reminders: Year=${latestWindow.year}, Quarter=${latestWindow.quarter}, End Date=${latestWindow.end_date}`);
|
||||
|
||||
|
||||
async runScheduledEmails() {
|
||||
// Check if current date is greater than end_date
|
||||
const currentDate = new Date();
|
||||
const endDate = new Date(latestWindow.end_date);
|
||||
|
||||
const currentDateStr = new Date().toLocaleDateString("en-CA");
|
||||
const endDateStr = new Date(latestWindow.end_date).toLocaleDateString("en-CA");
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Current Date: ${currentDateStr}`);
|
||||
console.log(`[${new Date().toISOString()}] End Date: ${endDateStr}`);
|
||||
|
||||
if (currentDateStr === endDateStr) {
|
||||
console.log(`[${new Date().toISOString()}] Today is the last day. Sending reminder emails.`);
|
||||
await this.sendEmailReminders(latestWindow);
|
||||
} else {
|
||||
console.log(`[${new Date().toISOString()}] Not the last day. Skipping reminder emails.`);
|
||||
}
|
||||
|
||||
|
||||
//find latest quarterly window configuration
|
||||
const latestWindow = await this.getLatestQuarterlyWindow();
|
||||
|
||||
if (!latestWindow) {
|
||||
console.log(`[${new Date().toISOString()}] No quarterly window configuration found for email reminders`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Latest window for email reminders: Year=${latestWindow.year}, Quarter=${latestWindow.quarter}, End Date=${latestWindow.end_date}`);
|
||||
|
||||
// Check if current date is greater than end_date
|
||||
const currentDate = new Date();
|
||||
const endDate = new Date(latestWindow.end_date);
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Current Date: ${currentDate.toISOString()}`);
|
||||
console.log(`[${new Date().toISOString()}] End Date: ${endDate.toISOString()}`);
|
||||
|
||||
if (currentDate < endDate) {
|
||||
console.log(`[${new Date().toISOString()}] Current date is not greater than end_date. Skipping email reminders.`);
|
||||
} else {
|
||||
console.log(`[${new Date().toISOString()}] Current date is greater than end_date. Sending email reminders.`);
|
||||
// Call your email reminder service here, passing the latestWindow details if needed
|
||||
// Send email remainders for All esatblishmets which have not submitted their survey for the quarter which has just opened and also for thouse who have not submitted till end date (This is a last day reminder)
|
||||
await this.sendEmailReminders(latestWindow);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Start the scheduler
|
||||
* Runs every day at 6:00 AM (you can adjust the time)
|
||||
*/
|
||||
startRemainderEmail() {
|
||||
console.log(`[${new Date().toISOString()}] Starting automated scheduler...`);
|
||||
|
||||
console.log(`[${new Date().toISOString()}] Starting automated email reminder scheduler...`);
|
||||
// Run every day at 6:00 AM
|
||||
// Format: minute hour day month weekday
|
||||
// '0 6 * * *' = At 6:00 AM every day
|
||||
|
||||
@ -307,6 +307,7 @@ app.post("/deploy", deploymentController.deployment);
|
||||
*/
|
||||
const scheduler = new AutomatedSchedulerService();
|
||||
scheduler.start();
|
||||
scheduler.startRemainderEmail();
|
||||
// manual trigger endpoint
|
||||
app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
||||
try {
|
||||
@ -328,5 +329,6 @@ app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
||||
const PORT = process.env.PORT || 5000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 Server running on port ${PORT}`);
|
||||
console.log(`Automated scheduler is active and will run daily at 2:00 AM`);
|
||||
console.log(`Automated scheduler (Index Calculation) is active and will run daily at 2:00 AM`);
|
||||
console.log(`Automated scheduler (Reminder Emails) is active and will run daily at 6:00 AM`);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user