2024-06-14 11:09:53 +00:00
|
|
|
import RunCron from "../../Utils/Cron";
|
|
|
|
import { CallRequestMessage } from "Common/Types/Call/CallRequest";
|
|
|
|
import LIMIT_MAX from "Common/Types/Database/LimitMax";
|
|
|
|
import OneUptimeDate from "Common/Types/Date";
|
|
|
|
import Dictionary from "Common/Types/Dictionary";
|
|
|
|
import { EmailEnvelope } from "Common/Types/Email/EmailMessage";
|
|
|
|
import EmailTemplateType from "Common/Types/Email/EmailTemplateType";
|
|
|
|
import NotificationSettingEventType from "Common/Types/NotificationSetting/NotificationSettingEventType";
|
|
|
|
import ObjectID from "Common/Types/ObjectID";
|
|
|
|
import { SMSMessage } from "Common/Types/SMS/SMS";
|
|
|
|
import { EVERY_MINUTE } from "Common/Utils/CronTime";
|
2024-08-07 21:50:32 +00:00
|
|
|
import IncidentService from "Common/Server/Services/IncidentService";
|
|
|
|
import IncidentStateTimelineService from "Common/Server/Services/IncidentStateTimelineService";
|
|
|
|
import ProjectService from "Common/Server/Services/ProjectService";
|
|
|
|
import UserNotificationSettingService from "Common/Server/Services/UserNotificationSettingService";
|
|
|
|
import Markdown, { MarkdownContentType } from "Common/Server/Types/Markdown";
|
2024-08-05 19:00:31 +00:00
|
|
|
import Incident from "Common/Models/DatabaseModels/Incident";
|
|
|
|
import IncidentState from "Common/Models/DatabaseModels/IncidentState";
|
|
|
|
import IncidentStateTimeline from "Common/Models/DatabaseModels/IncidentStateTimeline";
|
|
|
|
import Monitor from "Common/Models/DatabaseModels/Monitor";
|
|
|
|
import User from "Common/Models/DatabaseModels/User";
|
2024-01-11 10:49:55 +00:00
|
|
|
|
|
|
|
RunCron(
|
2024-06-14 11:09:53 +00:00
|
|
|
"IncidentOwner:SendStateChangeEmail",
|
|
|
|
{ schedule: EVERY_MINUTE, runOnStartup: false },
|
|
|
|
async () => {
|
|
|
|
// get all scheduled events of all the projects.
|
|
|
|
|
|
|
|
const incidentStateTimelines: Array<IncidentStateTimeline> =
|
|
|
|
await IncidentStateTimelineService.findBy({
|
|
|
|
query: {
|
|
|
|
isOwnerNotified: false,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
createdAt: true,
|
|
|
|
projectId: true,
|
|
|
|
project: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
incidentId: true,
|
|
|
|
incidentState: {
|
|
|
|
name: true,
|
2024-11-06 12:20:37 +00:00
|
|
|
isResolvedState: true,
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const incidentStateTimeline of incidentStateTimelines) {
|
|
|
|
const incidentId: ObjectID = incidentStateTimeline.incidentId!;
|
|
|
|
|
|
|
|
if (!incidentId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get incident
|
|
|
|
|
|
|
|
const incident: Incident | null = await IncidentService.findOneById({
|
|
|
|
id: incidentId,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
title: true,
|
|
|
|
description: true,
|
|
|
|
monitors: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!incident) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const incidentState: IncidentState = incidentStateTimeline.incidentState!;
|
|
|
|
|
|
|
|
// get incident severity
|
|
|
|
const incidentWithSeverity: Incident | null =
|
|
|
|
await IncidentService.findOneById({
|
|
|
|
id: incident.id!,
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
incidentSeverity: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!incidentWithSeverity) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
await IncidentStateTimelineService.updateOneById({
|
|
|
|
id: incidentStateTimeline.id!,
|
|
|
|
data: {
|
|
|
|
isOwnerNotified: true,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// now find owners.
|
|
|
|
|
|
|
|
let doesResourceHasOwners: boolean = true;
|
|
|
|
|
|
|
|
let owners: Array<User> = await IncidentService.findOwners(incident.id!);
|
|
|
|
|
|
|
|
if (owners.length === 0) {
|
|
|
|
doesResourceHasOwners = false;
|
|
|
|
|
|
|
|
// find project owners.
|
|
|
|
owners = await ProjectService.getOwners(
|
|
|
|
incidentStateTimeline.projectId!,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owners.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-06 12:20:37 +00:00
|
|
|
const resourcesAffected: string =
|
|
|
|
incident
|
|
|
|
.monitors!.map((monitor: Monitor) => {
|
|
|
|
return monitor.name!;
|
|
|
|
})
|
|
|
|
.join(", ") || "";
|
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const user of owners) {
|
2024-06-17 14:57:32 +00:00
|
|
|
const vars: Dictionary<string> = {
|
|
|
|
incidentTitle: incident.title!,
|
|
|
|
projectName: incidentStateTimeline.project!.name!,
|
|
|
|
currentState: incidentState!.name!,
|
|
|
|
incidentDescription: await Markdown.convertToHTML(
|
|
|
|
incident.description! || "",
|
|
|
|
MarkdownContentType.Email,
|
|
|
|
),
|
2024-11-06 12:20:37 +00:00
|
|
|
resourcesAffected: resourcesAffected || "None",
|
2024-06-17 14:58:30 +00:00
|
|
|
stateChangedAt:
|
|
|
|
OneUptimeDate.getDateAsFormattedHTMLInMultipleTimezones({
|
2024-06-17 14:57:32 +00:00
|
|
|
date: incidentStateTimeline.createdAt!,
|
2024-06-17 14:58:30 +00:00
|
|
|
timezones: user.timezone ? [user.timezone] : [],
|
|
|
|
}),
|
2024-06-17 14:57:32 +00:00
|
|
|
incidentSeverity: incidentWithSeverity.incidentSeverity!.name!,
|
|
|
|
incidentViewLink: (
|
|
|
|
await IncidentService.getIncidentLinkInDashboard(
|
|
|
|
incidentStateTimeline.projectId!,
|
|
|
|
incident.id!,
|
|
|
|
)
|
|
|
|
).toString(),
|
|
|
|
};
|
2024-06-17 14:58:30 +00:00
|
|
|
|
2024-06-17 14:57:32 +00:00
|
|
|
if (doesResourceHasOwners === true) {
|
|
|
|
vars["isOwner"] = "true";
|
|
|
|
}
|
|
|
|
|
2024-11-06 12:20:37 +00:00
|
|
|
let subjectLine: string = `[Incident] ${incident.title!}`;
|
|
|
|
|
|
|
|
if (incidentState.isResolvedState) {
|
|
|
|
if (resourcesAffected) {
|
|
|
|
subjectLine = `[Incident] Incident on ${resourcesAffected} is resolved`;
|
|
|
|
} else {
|
|
|
|
subjectLine = `[Incident] Incident is resolved`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
const emailMessage: EmailEnvelope = {
|
|
|
|
templateType: EmailTemplateType.IncidentOwnerStateChanged,
|
|
|
|
vars: vars,
|
2024-11-06 12:20:37 +00:00
|
|
|
subject: subjectLine,
|
2024-06-14 11:09:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const sms: SMSMessage = {
|
|
|
|
message: `This is a message from OneUptime. Incident: ${
|
|
|
|
incident.title
|
|
|
|
} - state changed to ${incidentState!
|
|
|
|
.name!}. To unsubscribe from this notification go to User Settings in OneUptime Dashboard.`,
|
|
|
|
};
|
|
|
|
|
|
|
|
const callMessage: CallRequestMessage = {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
sayMessage: `This is a message from OneUptime. Incident ${
|
|
|
|
incident.title
|
|
|
|
} state changed to ${incidentState!
|
|
|
|
.name!}. To unsubscribe from this notification go to User Settings in OneUptime Dashboard. Good bye.`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
await UserNotificationSettingService.sendUserNotification({
|
|
|
|
userId: user.id!,
|
|
|
|
projectId: incidentStateTimeline.projectId!,
|
|
|
|
emailEnvelope: emailMessage,
|
|
|
|
smsMessage: sms,
|
|
|
|
callRequestMessage: callMessage,
|
|
|
|
eventType:
|
|
|
|
NotificationSettingEventType.SEND_INCIDENT_STATE_CHANGED_OWNER_NOTIFICATION,
|
|
|
|
});
|
|
|
|
}
|
2024-01-11 10:49:55 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
2024-01-11 10:49:55 +00:00
|
|
|
);
|