oneuptime/App/FeatureSet/Workers/Jobs/StatusPageOwners/SendCreatedResourceNotification.ts

123 lines
3.9 KiB
TypeScript
Raw Normal View History

import RunCron from "../../Utils/Cron";
import { CallRequestMessage } from "Common/Types/Call/CallRequest";
import LIMIT_MAX from "Common/Types/Database/LimitMax";
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 { SMSMessage } from "Common/Types/SMS/SMS";
import { EVERY_MINUTE } from "Common/Utils/CronTime";
import ProjectService from "CommonServer/Services/ProjectService";
import StatusPageService from "CommonServer/Services/StatusPageService";
import UserNotificationSettingService from "CommonServer/Services/UserNotificationSettingService";
import Markdown, { MarkdownContentType } from "CommonServer/Types/Markdown";
import StatusPage from "Common/Models/DatabaseModels/StatusPage";
import User from "Common/Models/DatabaseModels/User";
2024-01-11 10:49:55 +00:00
RunCron(
"StatusPageOwner:SendCreatedResourceEmail",
{ schedule: EVERY_MINUTE, runOnStartup: false },
async () => {
// get all scheduled events of all the projects.
const statusPages: Array<StatusPage> = await StatusPageService.findBy({
query: {
isOwnerNotifiedOfResourceCreation: false,
},
props: {
isRoot: true,
},
limit: LIMIT_MAX,
skip: 0,
select: {
_id: true,
name: true,
description: true,
projectId: true,
project: {
name: true,
},
},
});
2024-01-11 10:49:55 +00:00
for (const statusPage of statusPages) {
await StatusPageService.updateOneById({
id: statusPage.id!,
data: {
isOwnerNotifiedOfResourceCreation: true,
},
props: {
isRoot: true,
},
});
2024-01-11 10:49:55 +00:00
// now find owners.
2024-01-11 10:49:55 +00:00
let doesResourceHasOwners: boolean = true;
2024-01-11 10:49:55 +00:00
let owners: Array<User> = await StatusPageService.findOwners(
statusPage.id!,
);
2024-01-11 10:49:55 +00:00
if (owners.length === 0) {
doesResourceHasOwners = false;
2024-01-11 10:49:55 +00:00
// find project owners.
owners = await ProjectService.getOwners(statusPage.projectId!);
}
2024-01-11 10:49:55 +00:00
if (owners.length === 0) {
continue;
}
2024-01-11 10:49:55 +00:00
const vars: Dictionary<string> = {
statusPageName: statusPage.name!,
projectName: statusPage.project!.name!,
statusPageDescription: await Markdown.convertToHTML(
statusPage.description! || "",
MarkdownContentType.Email,
),
statusPageViewLink: (
await StatusPageService.getStatusPageLinkInDashboard(
statusPage.projectId!,
statusPage.id!,
)
).toString(),
};
2024-01-11 10:49:55 +00:00
if (doesResourceHasOwners === true) {
vars["isOwner"] = "true";
}
2024-01-11 10:49:55 +00:00
for (const user of owners) {
const emailMessage: EmailEnvelope = {
templateType: EmailTemplateType.StatusPageOwnerResourceCreated,
vars: vars,
subject: "[Status Page Created]" + statusPage.name!,
};
2024-01-11 10:49:55 +00:00
const sms: SMSMessage = {
message: `This is a message from OneUptime. New status page created - ${statusPage.name}. To unsubscribe from this notification go to User Settings in OneUptime Dashboard.`,
};
2024-01-11 10:49:55 +00:00
const callMessage: CallRequestMessage = {
data: [
{
sayMessage: `This is a message from OneUptime. New status page created ${statusPage.name}. To unsubscribe from this notification go to User Settings in OneUptime Dashboard. Good bye.`,
},
],
};
2024-01-11 10:49:55 +00:00
await UserNotificationSettingService.sendUserNotification({
userId: user.id!,
projectId: statusPage.projectId!,
emailEnvelope: emailMessage,
smsMessage: sms,
callRequestMessage: callMessage,
eventType:
NotificationSettingEventType.SEND_STATUS_PAGE_CREATED_OWNER_NOTIFICATION,
});
}
2024-01-11 10:49:55 +00:00
}
},
2024-01-11 10:49:55 +00:00
);