2024-07-23 23:56:45 +00:00
|
|
|
import RunCron from "../../Utils/Cron";
|
|
|
|
import LIMIT_MAX from "Common/Types/Database/LimitMax";
|
|
|
|
import OneUptimeDate from "Common/Types/Date";
|
|
|
|
import Recurring from "Common/Types/Events/Recurring";
|
|
|
|
import { EVERY_MINUTE } from "Common/Utils/CronTime";
|
|
|
|
import StatusPageService from "CommonServer/Services/StatusPageService";
|
|
|
|
import QueryHelper from "CommonServer/Types/Database/QueryHelper";
|
2024-07-25 21:23:56 +00:00
|
|
|
import logger from "CommonServer/Utils/Logger";
|
2024-08-05 19:00:31 +00:00
|
|
|
import StatusPage from "Common/Models/DatabaseModels/StatusPage";
|
2024-07-23 23:56:45 +00:00
|
|
|
|
|
|
|
RunCron(
|
|
|
|
"StatusPage:SendReportToSubscribers",
|
|
|
|
{ schedule: EVERY_MINUTE, runOnStartup: false },
|
|
|
|
async () => {
|
|
|
|
// get all scheduled events of all the projects.
|
|
|
|
const statusPageToSendReports: Array<StatusPage> =
|
|
|
|
await StatusPageService.findBy({
|
|
|
|
query: {
|
|
|
|
isReportEnabled: true,
|
2024-07-25 21:23:56 +00:00
|
|
|
sendNextReportBy: QueryHelper.lessThan(
|
2024-07-23 23:56:45 +00:00
|
|
|
OneUptimeDate.getCurrentDate(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
sendNextReportBy: true,
|
|
|
|
reportRecurringInterval: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const statusPageToSendReport of statusPageToSendReports) {
|
|
|
|
const nextReportBy: Date = Recurring.getNextDate(
|
|
|
|
statusPageToSendReport.sendNextReportBy!,
|
|
|
|
statusPageToSendReport.reportRecurringInterval!,
|
|
|
|
);
|
|
|
|
|
|
|
|
// updte this date in the status page
|
|
|
|
|
|
|
|
await StatusPageService.updateOneById({
|
|
|
|
id: statusPageToSendReport.id!,
|
|
|
|
data: {
|
|
|
|
sendNextReportBy: nextReportBy,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-07-25 21:23:56 +00:00
|
|
|
try {
|
|
|
|
await StatusPageService.sendEmailReport({
|
|
|
|
statusPageId: statusPageToSendReport.id!,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
logger.error("Error sending report to subscribers");
|
|
|
|
logger.error(err);
|
|
|
|
}
|
2024-07-23 23:56:45 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|