Added 1s delay time
This commit is contained in:
parent
acddc25918
commit
8557ddb37a
@ -7,10 +7,22 @@ const EstablishmentUser = db.EstablishmentUser;
|
|||||||
const { sendEmailService } = require("../services/email.service");
|
const { sendEmailService } = require("../services/email.service");
|
||||||
|
|
||||||
// Create new configuration
|
// Create new configuration
|
||||||
|
|
||||||
exports.createConfig = async (req, res) => {
|
exports.createConfig = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const {survey_name, quarter, year, start_date, end_date } = req.body;
|
const {survey_name, quarter, year, start_date, end_date } = req.body;
|
||||||
|
|
||||||
|
const surveyNameExists = await QuarterlyWindowsConfiguration.findOne({
|
||||||
|
where: { survey_name, is_active: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (surveyNameExists) {
|
||||||
|
return res.status(400).json({
|
||||||
|
status: "failed",
|
||||||
|
message: `Survey Name '${survey_name}' already exists.`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// find config
|
// find config
|
||||||
const exists = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }});
|
const exists = await QuarterlyWindowsConfiguration.findOne({where: { quarter, year }});
|
||||||
@ -40,54 +52,39 @@ exports.createConfig = async (req, res) => {
|
|||||||
support_email: process.env.SUPPORT_EMAIL,
|
support_email: process.env.SUPPORT_EMAIL,
|
||||||
support_phone: process.env.SUPPORT_PHONE
|
support_phone: process.env.SUPPORT_PHONE
|
||||||
};
|
};
|
||||||
const EstablishmentUserData = await EstablishmentUser.findAll({where: { is_active: 1 }});
|
|
||||||
const establishmentEmailPromises = EstablishmentUserData.map(async (userObj) => {
|
|
||||||
try {
|
|
||||||
await sendEmailService(
|
|
||||||
userObj.email,
|
|
||||||
"quarterly_survey_created_to_establishment",
|
|
||||||
placeholder
|
|
||||||
);
|
|
||||||
return { email: userObj.email, status: "sent" };
|
|
||||||
} catch (error) {
|
|
||||||
return { email: userObj.email, status: "failed", error: error.message };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Wait for all emails to resolve
|
|
||||||
const emailResults = await Promise.all(establishmentEmailPromises);
|
|
||||||
|
|
||||||
// Create quarterly window configuration
|
|
||||||
const data = await QuarterlyWindowsConfiguration.create(req.body);
|
const data = await QuarterlyWindowsConfiguration.create(req.body);
|
||||||
|
|
||||||
res.status(201).send({
|
res.status(201).send({
|
||||||
status: "success",
|
status: "success",
|
||||||
message: "Quarterly window configuration created successfully",
|
message: "Quarterly window configuration created successfully",
|
||||||
data,
|
data,
|
||||||
emailResults,
|
|
||||||
});
|
});
|
||||||
|
sendEmailsToEstablishments(placeholder);
|
||||||
|
|
||||||
|
|
||||||
// const establishmentEmailPromises = EstablishmentUserData.map((userObj) => {
|
|
||||||
// return sendEmailService(userObj.email,"quarterly_survey_created_to_establishment", placeholder);
|
|
||||||
// });
|
|
||||||
|
|
||||||
// await Promise.all([
|
|
||||||
// ...establishmentEmailPromises
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// const data = await QuarterlyWindowsConfiguration.create(req.body);
|
|
||||||
// res.status(201).send({
|
|
||||||
// status: "success",
|
|
||||||
// message: "Quarterly window configuration created successfully",
|
|
||||||
// data,
|
|
||||||
// });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(500).send({ status: "failed", message: err.message });
|
res.status(500).send({ status: "failed", message: err.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function sendEmailsToEstablishments(placeholder) {
|
||||||
|
try {
|
||||||
|
const users = await EstablishmentUser.findAll({
|
||||||
|
where: { is_active: 1 }
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const user of users) {
|
||||||
|
try {
|
||||||
|
await sendEmailService( user.email, "quarterly_survey_created_to_establishment", placeholder );
|
||||||
|
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
} catch (emailErr) {
|
||||||
|
console.error("Email failed:", user.email, emailErr.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Email background job failed:", err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get all configurations
|
// Get all configurations
|
||||||
exports.getAllConfigs = async (req, res) => {
|
exports.getAllConfigs = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user