2024-06-14 11:09:53 +00:00
|
|
|
import RunCron from "../../Utils/Cron";
|
|
|
|
import { FileRoute } from "Common/ServiceRoute";
|
|
|
|
import Hostname from "Common/Types/API/Hostname";
|
|
|
|
import Protocol from "Common/Types/API/Protocol";
|
|
|
|
import URL from "Common/Types/API/URL";
|
|
|
|
import LIMIT_MAX, { LIMIT_PER_PROJECT } from "Common/Types/Database/LimitMax";
|
|
|
|
import OneUptimeDate from "Common/Types/Date";
|
|
|
|
import Dictionary from "Common/Types/Dictionary";
|
|
|
|
import EmailTemplateType from "Common/Types/Email/EmailTemplateType";
|
|
|
|
import ObjectID from "Common/Types/ObjectID";
|
|
|
|
import SMS from "Common/Types/SMS/SMS";
|
|
|
|
import Text from "Common/Types/Text";
|
|
|
|
import { EVERY_MINUTE } from "Common/Utils/CronTime";
|
2024-08-07 21:50:32 +00:00
|
|
|
import DatabaseConfig from "Common/Server/DatabaseConfig";
|
|
|
|
import MailService from "Common/Server/Services/MailService";
|
|
|
|
import ProjectCallSMSConfigService from "Common/Server/Services/ProjectCallSMSConfigService";
|
|
|
|
import ProjectSmtpConfigService from "Common/Server/Services/ProjectSmtpConfigService";
|
|
|
|
import ScheduledMaintenanceService from "Common/Server/Services/ScheduledMaintenanceService";
|
|
|
|
import ScheduledMaintenanceStateTimelineService from "Common/Server/Services/ScheduledMaintenanceStateTimelineService";
|
|
|
|
import SmsService from "Common/Server/Services/SmsService";
|
|
|
|
import StatusPageResourceService from "Common/Server/Services/StatusPageResourceService";
|
|
|
|
import StatusPageService from "Common/Server/Services/StatusPageService";
|
|
|
|
import StatusPageSubscriberService from "Common/Server/Services/StatusPageSubscriberService";
|
|
|
|
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
|
|
|
|
import logger from "Common/Server/Utils/Logger";
|
2024-08-05 19:00:31 +00:00
|
|
|
import Monitor from "Common/Models/DatabaseModels/Monitor";
|
|
|
|
import ScheduledMaintenance from "Common/Models/DatabaseModels/ScheduledMaintenance";
|
|
|
|
import ScheduledMaintenanceStateTimeline from "Common/Models/DatabaseModels/ScheduledMaintenanceStateTimeline";
|
|
|
|
import StatusPage from "Common/Models/DatabaseModels/StatusPage";
|
|
|
|
import StatusPageResource from "Common/Models/DatabaseModels/StatusPageResource";
|
|
|
|
import StatusPageSubscriber from "Common/Models/DatabaseModels/StatusPageSubscriber";
|
2024-10-28 13:09:26 +00:00
|
|
|
import StatusPageEventType from "Common/Types/StatusPage/StatusPageEventType";
|
2024-01-11 10:49:55 +00:00
|
|
|
|
|
|
|
RunCron(
|
2024-06-14 11:09:53 +00:00
|
|
|
"ScheduledMaintenanceStateTimeline:SendNotificationToSubscribers",
|
|
|
|
{ schedule: EVERY_MINUTE, runOnStartup: false },
|
|
|
|
async () => {
|
|
|
|
const host: Hostname = await DatabaseConfig.getHost();
|
|
|
|
const httpProtocol: Protocol = await DatabaseConfig.getHttpProtocol();
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const scheduledEventStateTimelines: Array<ScheduledMaintenanceStateTimeline> =
|
|
|
|
await ScheduledMaintenanceStateTimelineService.findBy({
|
|
|
|
query: {
|
|
|
|
isStatusPageSubscribersNotified: false,
|
|
|
|
shouldStatusPageSubscribersBeNotified: true,
|
|
|
|
createdAt: QueryHelper.lessThan(OneUptimeDate.getCurrentDate()),
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
scheduledMaintenanceId: true,
|
|
|
|
scheduledMaintenanceStateId: true,
|
|
|
|
scheduledMaintenanceState: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const scheduledEventStateTimeline of scheduledEventStateTimelines) {
|
|
|
|
await ScheduledMaintenanceStateTimelineService.updateOneById({
|
|
|
|
id: scheduledEventStateTimeline.id!,
|
|
|
|
data: {
|
|
|
|
isStatusPageSubscribersNotified: true,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
ignoreHooks: true,
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (
|
|
|
|
!scheduledEventStateTimeline.scheduledMaintenanceId ||
|
|
|
|
!scheduledEventStateTimeline.scheduledMaintenanceStateId
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!scheduledEventStateTimeline.scheduledMaintenanceState?.name) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// get all scheduled events of all the projects.
|
|
|
|
const event: ScheduledMaintenance | null =
|
|
|
|
await ScheduledMaintenanceService.findOneById({
|
|
|
|
id: scheduledEventStateTimeline.scheduledMaintenanceId!,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
startsAt: true,
|
|
|
|
monitors: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
statusPages: {
|
|
|
|
_id: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!event) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// get status page resources from monitors.
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
let statusPageResources: Array<StatusPageResource> = [];
|
2024-05-28 16:07:23 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (event.monitors && event.monitors.length > 0) {
|
|
|
|
statusPageResources = await StatusPageResourceService.findBy({
|
|
|
|
query: {
|
|
|
|
monitorId: QueryHelper.any(
|
|
|
|
event.monitors
|
|
|
|
.filter((m: Monitor) => {
|
|
|
|
return m._id;
|
|
|
|
})
|
|
|
|
.map((m: Monitor) => {
|
|
|
|
return new ObjectID(m._id!);
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
ignoreHooks: true,
|
|
|
|
},
|
|
|
|
skip: 0,
|
|
|
|
limit: LIMIT_PER_PROJECT,
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
displayName: true,
|
|
|
|
statusPageId: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const statusPageToResources: Dictionary<Array<StatusPageResource>> = {};
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const resource of statusPageResources) {
|
|
|
|
if (!resource.statusPageId) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!statusPageToResources[resource.statusPageId?.toString()]) {
|
|
|
|
statusPageToResources[resource.statusPageId?.toString()] = [];
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
statusPageToResources[resource.statusPageId?.toString()]?.push(
|
|
|
|
resource,
|
|
|
|
);
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const statusPages: Array<StatusPage> =
|
|
|
|
await StatusPageSubscriberService.getStatusPagesToSendNotification(
|
|
|
|
event.statusPages?.map((i: StatusPage) => {
|
|
|
|
return i.id!;
|
|
|
|
}) || [],
|
|
|
|
);
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const statuspage of statusPages) {
|
|
|
|
if (!statuspage.id) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const subscribers: Array<StatusPageSubscriber> =
|
|
|
|
await StatusPageSubscriberService.getSubscribersByStatusPage(
|
|
|
|
statuspage.id!,
|
|
|
|
{
|
|
|
|
isRoot: true,
|
|
|
|
ignoreHooks: true,
|
|
|
|
},
|
|
|
|
);
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const statusPageURL: string = await StatusPageService.getStatusPageURL(
|
|
|
|
statuspage.id,
|
|
|
|
);
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const statusPageName: string =
|
|
|
|
statuspage.pageTitle || statuspage.name || "Status Page";
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// Send email to Email subscribers.
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const subscriber of subscribers) {
|
|
|
|
if (!subscriber._id) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const shouldNotifySubscriber: boolean =
|
|
|
|
StatusPageSubscriberService.shouldSendNotification({
|
|
|
|
subscriber: subscriber,
|
|
|
|
statusPageResources: statusPageToResources[statuspage._id!] || [],
|
|
|
|
statusPage: statuspage,
|
2024-10-28 13:09:26 +00:00
|
|
|
eventType: StatusPageEventType.ScheduledEvent,
|
2024-06-14 11:09:53 +00:00
|
|
|
});
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (!shouldNotifySubscriber) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const unsubscribeUrl: string =
|
|
|
|
StatusPageSubscriberService.getUnsubscribeLink(
|
|
|
|
URL.fromString(statusPageURL),
|
|
|
|
subscriber.id!,
|
|
|
|
).toString();
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (subscriber.subscriberPhone) {
|
|
|
|
const sms: SMS = {
|
|
|
|
message: `
|
2024-01-11 10:49:55 +00:00
|
|
|
${statusPageName} - Scheduled maintenance event - ${
|
2024-06-14 11:09:53 +00:00
|
|
|
event.title || ""
|
|
|
|
} - state changed to ${
|
|
|
|
scheduledEventStateTimeline
|
|
|
|
.scheduledMaintenanceState?.name
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
|
|
|
To view this note, visit ${statusPageURL}
|
|
|
|
|
|
|
|
To update notification preferences or unsubscribe, visit ${unsubscribeUrl}
|
|
|
|
`,
|
2024-06-14 11:09:53 +00:00
|
|
|
to: subscriber.subscriberPhone,
|
|
|
|
};
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
// send sms here.
|
|
|
|
SmsService.sendSms(sms, {
|
|
|
|
projectId: statuspage.projectId,
|
|
|
|
customTwilioConfig: ProjectCallSMSConfigService.toTwilioConfig(
|
|
|
|
statuspage.callSmsConfig,
|
|
|
|
),
|
|
|
|
}).catch((err: Error) => {
|
|
|
|
logger.error(err);
|
|
|
|
});
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
if (subscriber.subscriberEmail) {
|
|
|
|
// send email here.
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
MailService.sendMail(
|
|
|
|
{
|
|
|
|
toEmail: subscriber.subscriberEmail,
|
|
|
|
templateType:
|
|
|
|
EmailTemplateType.SubscriberScheduledMaintenanceEventStateChanged,
|
|
|
|
vars: {
|
|
|
|
statusPageName: statusPageName,
|
|
|
|
statusPageUrl: statusPageURL,
|
|
|
|
logoUrl: statuspage.logoFileId
|
|
|
|
? new URL(httpProtocol, host)
|
|
|
|
.addRoute(FileRoute)
|
|
|
|
.addRoute("/image/" + statuspage.logoFileId)
|
|
|
|
.toString()
|
|
|
|
: "",
|
|
|
|
isPublicStatusPage: statuspage.isPublicStatusPage
|
|
|
|
? "true"
|
|
|
|
: "false",
|
|
|
|
resourcesAffected:
|
|
|
|
statusPageToResources[statuspage._id!]
|
|
|
|
?.map((r: StatusPageResource) => {
|
|
|
|
return r.displayName;
|
|
|
|
})
|
|
|
|
.join(", ") || "",
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
eventState:
|
|
|
|
scheduledEventStateTimeline.scheduledMaintenanceState
|
|
|
|
?.name || "",
|
2024-01-11 10:49:55 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
scheduledAt: OneUptimeDate.getDateAsFormattedString(
|
|
|
|
event.startsAt!,
|
|
|
|
),
|
|
|
|
eventTitle: event.title || "",
|
|
|
|
eventDescription: event.description || "",
|
|
|
|
unsubscribeUrl: unsubscribeUrl,
|
|
|
|
},
|
|
|
|
subject: `[Scheduled Maintenance ${Text.uppercaseFirstLetter(
|
|
|
|
scheduledEventStateTimeline.scheduledMaintenanceState?.name,
|
|
|
|
)}] ${statusPageName}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
mailServer: ProjectSmtpConfigService.toEmailServer(
|
|
|
|
statuspage.smtpConfig,
|
|
|
|
),
|
|
|
|
projectId: statuspage.projectId,
|
|
|
|
},
|
|
|
|
).catch((err: Error) => {
|
|
|
|
logger.error(err);
|
|
|
|
});
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
2024-01-11 10:49:55 +00:00
|
|
|
);
|