oneuptime/Workers/Jobs/StatusPageOwners/SendCreatedResourceNotification.ts

123 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-05-18 16:13:22 +00:00
import { EVERY_MINUTE } from 'Common/Utils/CronTime';
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
import RunCron from '../../Utils/Cron';
import EmailTemplateType from 'Common/Types/Email/EmailTemplateType';
import Dictionary from 'Common/Types/Dictionary';
import StatusPage from 'Model/Models/StatusPage';
import StatusPageService from 'CommonServer/Services/StatusPageService';
import User from 'Model/Models/User';
import ProjectService from 'CommonServer/Services/ProjectService';
import Markdown from 'CommonServer/Types/Markdown';
2023-07-22 12:36:02 +00:00
import { EmailEnvelope } from 'Common/Types/Email/EmailMessage';
import { SMSMessage } from 'Common/Types/SMS/SMS';
import { CallRequestMessage } from 'Common/Types/Call/CallRequest';
import UserNotificationSettingService from 'CommonServer/Services/UserNotificationSettingService';
import NotificationSettingEventType from 'Common/Types/NotificationSetting/NotificationSettingEventType';
2023-05-18 16:13:22 +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,
},
},
});
for (const statusPage of statusPages) {
await StatusPageService.updateOneById({
id: statusPage.id!,
data: {
isOwnerNotifiedOfResourceCreation: true,
},
props: {
isRoot: true,
},
});
// now find owners.
let doesResourceHasOwners: boolean = true;
let owners: Array<User> = await StatusPageService.findOwners(
statusPage.id!
);
if (owners.length === 0) {
doesResourceHasOwners = false;
// find project owners.
owners = await ProjectService.getOwners(statusPage.projectId!);
}
if (owners.length === 0) {
continue;
}
const vars: Dictionary<string> = {
statusPageName: statusPage.name!,
projectName: statusPage.project!.name!,
statusPageDescription: Markdown.convertToHTML(
statusPage.description! || ''
),
2023-09-07 10:27:55 +00:00
statusPageViewLink: (
await StatusPageService.getStatusPageLinkInDashboard(
2023-05-18 17:13:27 +00:00
statusPage.projectId!,
statusPage.id!
2023-09-07 10:27:55 +00:00
)
).toString(),
2023-05-18 16:13:22 +00:00
};
if (doesResourceHasOwners === true) {
vars['isOwner'] = 'true';
}
for (const user of owners) {
2023-07-22 12:36:02 +00:00
const emailMessage: EmailEnvelope = {
2023-05-18 17:13:27 +00:00
templateType:
EmailTemplateType.StatusPageOwnerResourceCreated,
2023-05-18 16:13:22 +00:00
vars: vars,
2023-05-18 16:22:44 +00:00
subject: 'New status page created - ' + statusPage.name!,
2023-07-22 12:36:02 +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.`,
};
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.`,
},
],
};
await UserNotificationSettingService.sendUserNotification({
userId: user.id!,
projectId: statusPage.projectId!,
emailEnvelope: emailMessage,
smsMessage: sms,
callRequestMessage: callMessage,
eventType:
2023-07-22 15:13:32 +00:00
NotificationSettingEventType.SEND_STATUS_PAGE_CREATED_OWNER_NOTIFICATION,
2023-05-18 16:13:22 +00:00
});
}
}
}
);