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 mysql = require('mysql2/promise');
|
||||||
const SubmissionAutoFillService = require('./auto_fill_missing_quarterly_submissions_service'); // Adjust path
|
const SubmissionAutoFillService = require('./auto_fill_missing_quarterly_submissions_service'); // Adjust path
|
||||||
const IPICalculationService = require('./ipi_calculation_service');
|
const IPICalculationService = require('./ipi_calculation_service');
|
||||||
|
const { sendEmailService } = require("./email.service");
|
||||||
|
|
||||||
// Database configuration
|
// Database configuration
|
||||||
const dbConfig = {
|
const dbConfig = {
|
||||||
@ -36,6 +37,7 @@ class AutomatedSchedulerService {
|
|||||||
try {
|
try {
|
||||||
const query = `
|
const query = `
|
||||||
SELECT
|
SELECT
|
||||||
|
survey_name,
|
||||||
year,
|
year,
|
||||||
quarter,
|
quarter,
|
||||||
end_date,
|
end_date,
|
||||||
@ -265,12 +267,39 @@ class AutomatedSchedulerService {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async getEstablishmentsWithoutSubmission(year, quarter) {
|
async getEstablishmentsWithoutSubmission(year, quarter) {
|
||||||
|
|
||||||
const connection = await this.pool.getConnection();
|
const connection = await this.pool.getConnection();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -287,12 +316,10 @@ async getEstablishmentsWithoutSubmission(year, quarter) {
|
|||||||
|
|
||||||
WHERE NOT EXISTS (
|
WHERE NOT EXISTS (
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM submissions s
|
FROM submission s
|
||||||
WHERE s.establishment_id = e.id
|
WHERE s.establishment_id = e.id
|
||||||
AND s.year = ?
|
AND s.year = ?
|
||||||
AND s.quarter = ?
|
AND s.quarter = ? ) AND e.is_active = 1
|
||||||
AND s.status != 'Draft'
|
|
||||||
)
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const [rows] = await connection.query(query, [year, quarter]);
|
const [rows] = await connection.query(query, [year, quarter]);
|
||||||
@ -307,7 +334,6 @@ async getEstablishmentsWithoutSubmission(year, quarter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async sendReminderEmails(rows, latestWindow) {
|
async sendReminderEmails(rows, latestWindow) {
|
||||||
try {
|
try {
|
||||||
if (!rows.length) {
|
if (!rows.length) {
|
||||||
@ -315,23 +341,22 @@ async sendReminderEmails(rows, latestWindow) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const surveyData = await this.getSurveyData(
|
|
||||||
latestWindow.year,
|
|
||||||
latestWindow.quarter
|
|
||||||
);
|
|
||||||
|
|
||||||
const basePlaceholder = {
|
const basePlaceholder = {
|
||||||
|
survey_name: latestWindow.survey_name,
|
||||||
quarter: latestWindow.quarter,
|
quarter: latestWindow.quarter,
|
||||||
year: latestWindow.year,
|
year: latestWindow.year,
|
||||||
survey_name: surveyData?.survey_name,
|
opens_on: latestWindow.start_date,
|
||||||
|
closes_on: latestWindow.end_date,
|
||||||
|
portal_url: process.env.FE_BASE_URL,
|
||||||
support_email: process.env.SUPPORT_EMAIL,
|
support_email: process.env.SUPPORT_EMAIL,
|
||||||
support_phone: process.env.SUPPORT_PHONE,
|
support_phone: process.env.SUPPORT_PHONE,
|
||||||
portal_url: process.env.FE_BASE_URL,
|
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`
|
||||||
logo_url: `${process.env.APP_BASE_URL}/assets/FCSCLogo.png`,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const promises = rows.map((r) =>
|
const promises = rows.map((r) =>
|
||||||
sendEmailService(r.email, "submission_reminder_mail", {
|
sendEmailService(r.email, "submission_last_day_reminder_mail", {
|
||||||
...basePlaceholder,
|
...basePlaceholder,
|
||||||
user_name: r.user_name,
|
user_name: r.user_name,
|
||||||
establishment_name: r.factory_name,
|
establishment_name: r.factory_name,
|
||||||
@ -347,14 +372,12 @@ async sendReminderEmails(rows, latestWindow) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async sendEmailReminders(latestWindow) {
|
async sendEmailReminders(latestWindow) {
|
||||||
console.log(
|
console.log(
|
||||||
`[${new Date().toISOString()}] Sending reminders for Year=${latestWindow.year}, Quarter=${latestWindow.quarter}`
|
`[${new Date().toISOString()}] Sending reminders for Year=${latestWindow.year}, Quarter=${latestWindow.quarter}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const establishments = await getEstablishmentsWithoutSubmission(
|
const establishments = await this.getEstablishmentsWithoutSubmission(
|
||||||
latestWindow.year,
|
latestWindow.year,
|
||||||
latestWindow.quarter
|
latestWindow.quarter
|
||||||
);
|
);
|
||||||
@ -364,12 +387,11 @@ async sendEmailReminders(latestWindow) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await sendReminderEmails(establishments, latestWindow);
|
await this.sendReminderEmails(establishments, latestWindow);
|
||||||
|
|
||||||
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
|
console.log(`[${new Date().toISOString()}] Email reminders completed.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async runScheduledEmails() {
|
async runScheduledEmails() {
|
||||||
|
|
||||||
//find latest quarterly window configuration
|
//find latest quarterly window configuration
|
||||||
@ -382,33 +404,32 @@ async sendEmailReminders(latestWindow) {
|
|||||||
|
|
||||||
console.log(`[${new Date().toISOString()}] Latest window for email reminders: Year=${latestWindow.year}, Quarter=${latestWindow.quarter}, End Date=${latestWindow.end_date}`);
|
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
|
// Check if current date is greater than end_date
|
||||||
const currentDate = new Date();
|
const currentDate = new Date();
|
||||||
const endDate = new Date(latestWindow.end_date);
|
const endDate = new Date(latestWindow.end_date);
|
||||||
|
|
||||||
console.log(`[${new Date().toISOString()}] Current Date: ${currentDate.toISOString()}`);
|
const currentDateStr = new Date().toLocaleDateString("en-CA");
|
||||||
console.log(`[${new Date().toISOString()}] End Date: ${endDate.toISOString()}`);
|
const endDateStr = new Date(latestWindow.end_date).toLocaleDateString("en-CA");
|
||||||
|
|
||||||
if (currentDate < endDate) {
|
console.log(`[${new Date().toISOString()}] Current Date: ${currentDateStr}`);
|
||||||
console.log(`[${new Date().toISOString()}] Current date is not greater than end_date. Skipping email reminders.`);
|
console.log(`[${new Date().toISOString()}] End Date: ${endDateStr}`);
|
||||||
} else {
|
|
||||||
console.log(`[${new Date().toISOString()}] Current date is greater than end_date. Sending email reminders.`);
|
if (currentDateStr === endDateStr) {
|
||||||
// Call your email reminder service here, passing the latestWindow details if needed
|
console.log(`[${new Date().toISOString()}] Today is the last day. Sending reminder emails.`);
|
||||||
// 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);
|
await this.sendEmailReminders(latestWindow);
|
||||||
}
|
} else {
|
||||||
|
console.log(`[${new Date().toISOString()}] Not the last day. Skipping reminder emails.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Start the scheduler
|
* Start the scheduler
|
||||||
* Runs every day at 6:00 AM (you can adjust the time)
|
* Runs every day at 6:00 AM (you can adjust the time)
|
||||||
*/
|
*/
|
||||||
startRemainderEmail() {
|
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
|
// Run every day at 6:00 AM
|
||||||
// Format: minute hour day month weekday
|
// Format: minute hour day month weekday
|
||||||
// '0 6 * * *' = At 6:00 AM every day
|
// '0 6 * * *' = At 6:00 AM every day
|
||||||
|
|||||||
@ -307,6 +307,7 @@ app.post("/deploy", deploymentController.deployment);
|
|||||||
*/
|
*/
|
||||||
const scheduler = new AutomatedSchedulerService();
|
const scheduler = new AutomatedSchedulerService();
|
||||||
scheduler.start();
|
scheduler.start();
|
||||||
|
scheduler.startRemainderEmail();
|
||||||
// manual trigger endpoint
|
// manual trigger endpoint
|
||||||
app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -328,5 +329,6 @@ app.post('/api/admin/trigger-scheduler', async (req, res) => {
|
|||||||
const PORT = process.env.PORT || 5000;
|
const PORT = process.env.PORT || 5000;
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`🚀 Server running on port ${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