oneuptime/Workers/Jobs/StatusPageOwners/SendCreatedResourceNotification.ts
Simon Larsen a31d1880ee
fix fmt
2023-09-07 15:57:55 +05:30

123 lines
4.5 KiB
TypeScript

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';
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';
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! || ''
),
statusPageViewLink: (
await StatusPageService.getStatusPageLinkInDashboard(
statusPage.projectId!,
statusPage.id!
)
).toString(),
};
if (doesResourceHasOwners === true) {
vars['isOwner'] = 'true';
}
for (const user of owners) {
const emailMessage: EmailEnvelope = {
templateType:
EmailTemplateType.StatusPageOwnerResourceCreated,
vars: vars,
subject: 'New status page created - ' + statusPage.name!,
};
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:
NotificationSettingEventType.SEND_STATUS_PAGE_CREATED_OWNER_NOTIFICATION,
});
}
}
}
);