Skip to content

Commit

Permalink
add annnouncement email
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed May 19, 2023
1 parent adea15b commit 7375e6e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CommonUI/src/Components/Detail/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const Detail: Function = (props: ComponentProps): ReactElement => {
let data: string | ReactElement = '';

if (_.get(props.item, fieldKey)) {
data = _.get(props.item, fieldKey, '') as any || '';
data = (_.get(props.item, fieldKey, '') as any) || '';
}

if (field.fieldType === FieldType.Date) {
Expand Down
1 change: 1 addition & 0 deletions Workers/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import './Jobs/ScheduledMaintenanceOwners/SendNotePostedEmail';
// Status Page Owners
import './Jobs/StatusPageOwners/SendCreatedResourceEmail';
import './Jobs/StatusPageOwners/SendOwnerAddedEmail';
import './Jobs/StatusPageOwners/SendAnnouncementCreatedEmail';

const APP_NAME: string = 'workers';

Expand Down
2 changes: 1 addition & 1 deletion Workers/Jobs/IncidentOwners/SendNotePostedEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ RunCron(
}

for (const note of privateNotes) {
await IncidentPublicNoteService.updateOneById({
await IncidentInternalNoteService.updateOneById({
id: note.id!,
data: {
isOwnerNotified: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ RunCron(
}

for (const note of privateNotes) {
await ScheduledMaintenancePublicNoteService.updateOneById({
await ScheduledMaintenanceInternalNoteService.updateOneById({
id: note.id!,
data: {
isOwnerNotified: true,
Expand Down
107 changes: 107 additions & 0 deletions Workers/Jobs/StatusPageOwners/SendAnnouncementCreatedEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { EVERY_MINUTE } from 'Common/Utils/CronTime';
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
import RunCron from '../../Utils/Cron';
import MailService from 'CommonServer/Services/MailService';
import EmailTemplateType from 'Common/Types/Email/EmailTemplateType';
import logger from 'CommonServer/Utils/Logger';
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 StatusPageAnnouncement from 'Model/Models/StatusPageAnnouncement';
import StatusPageAnnouncementService from 'CommonServer/Services/StatusPageAnnouncementService';

RunCron(
'StatusPageOwner:SendAnnouncementCreatedEmail',
{ schedule: EVERY_MINUTE, runOnStartup: false },
async () => {
const announcements: Array<StatusPageAnnouncement> =
await StatusPageAnnouncementService.findBy({
query: {
isOwnerNotified: false,
},
props: {
isRoot: true,
},
limit: LIMIT_MAX,
skip: 0,
select: {
_id: true,
title: true,
description: true,
statusPages: true,
},
populate: {
statusPages: {
_id: true,
name: true,
},
},
});

for (const announcement of announcements) {
await StatusPageAnnouncementService.updateOneById({
id: announcement.id!,
data: {
isOwnerNotified: true,
},
props: {
isRoot: true,
},
});

const statusPages: Array<StatusPage> =
announcement.statusPages || [];

for (const statusPage of statusPages) {
// 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!,
announcementTitle: announcement.title!,
announcementDescription: Markdown.convertToHTML(
announcement.description!
),
};

if (doesResourceHasOwners === true) {
vars['isOwner'] = 'true';
}

for (const user of owners) {
MailService.sendMail({
toEmail: user.email!,
templateType:
EmailTemplateType.StatusPageOwnerAnnouncementPosted,
vars: vars,
subject:
'New announcement posted - ' + announcement.title!,
}).catch((err: Error) => {
logger.error(err);
});
}
}
}
}
);

0 comments on commit 7375e6e

Please sign in to comment.