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";
|
2024-08-07 21:50:32 +00:00
|
|
|
import StatusPageService from "Common/Server/Services/StatusPageService";
|
|
|
|
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
|
|
|
|
import logger from "Common/Server/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",
|
2024-09-22 13:15:09 +00:00
|
|
|
{
|
|
|
|
schedule: EVERY_MINUTE,
|
2024-09-22 13:57:49 +00:00
|
|
|
runOnStartup: false,
|
2024-09-22 13:15:09 +00:00
|
|
|
},
|
2024-07-23 23:56:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|