Update import statements and add new data migration

This commit is contained in:
Simon Larsen 2023-12-14 16:23:49 +00:00
parent f62930abb1
commit 16a65fcb4c
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
5 changed files with 101 additions and 17 deletions

View File

@ -59,9 +59,8 @@ export class Service extends DatabaseService<Model> {
if (data.data.subscriberPhone) {
// check if this project has SMS enabled.
const isSMSEnabled: boolean = await ProjectService.isSMSNotificationsEnabled(
projectId
);
const isSMSEnabled: boolean =
await ProjectService.isSMSNotificationsEnabled(projectId);
if (!isSMSEnabled) {
throw new BadDataException(

View File

@ -7,6 +7,7 @@ import DataMigrationBase from './DataMigrationBase';
import MigrateDefaultUserNotificationRule from './MigrateDefaultUserNotificationRule';
import MigrateDefaultUserNotificationSetting from './MigrateDefaultUserSettingNotification';
import MigrateToMeteredSubscription from './MigrateToMeteredSubscription';
import MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage from './MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage';
import UpdateActiveMonitorCountToBillingProvider from './UpdateActiveMonitorCountToBillingProvider';
import UpdateGlobalConfigFromEnv from './UpdateGlobalCongfigFromEnv';
@ -23,6 +24,7 @@ const DataMigrations: Array<DataMigrationBase> = [
new AddDefaultGlobalConfig(),
new UpdateGlobalConfigFromEnv(),
new AddPostedAtToPublicNotes(),
new MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage(),
];
export default DataMigrations;

View File

@ -0,0 +1,52 @@
import DataMigrationBase from './DataMigrationBase';
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
import StatusPage from 'Model/Models/StatusPage';
import StatusPageService from 'CommonServer/Services/StatusPageService';
export default class MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage extends DataMigrationBase {
public constructor() {
super('AddPostedAtToPublicNotes');
}
public override async migrate(): Promise<void> {
// get all the users with email isVerified true.
const tempStatusPage = new StatusPage();
if(!tempStatusPage.getTableColumnMetadata("enableSubscribers")){
// this column does not exist, so we can skip this migration.
return;
}
const statusPages: Array<StatusPage> =
await StatusPageService.findBy({
query: {},
select: {
_id: true,
enableSubscribers: true,
},
skip: 0,
limit: LIMIT_MAX,
props: {
isRoot: true,
},
});
for (const statusPage of statusPages) {
await StatusPageService.updateOneById({
id: statusPage.id!,
data: {
enableEmailSubscribers: statusPage.enableSubscribers!,
},
props: {
isRoot: true,
},
});
}
}
public override async rollback(): Promise<void> {
return;
}
}

View File

@ -14,7 +14,7 @@ import './Jobs/PaymentProvider/PopulatePlanNameInProject';
import './Jobs/Announcement/SendEmailToSubscribers';
// Incidents
import './Jobs/Incident/SendEmailToSubscribers';
import './Jobs/Incident/SendNotificationToSubscribers';
import './Jobs/IncidentStateTimeline/SendEmailToSubscribers';
// Incident Notes

View File

@ -24,9 +24,11 @@ import Markdown from 'CommonServer/Types/Markdown';
import Hostname from 'Common/Types/API/Hostname';
import Protocol from 'Common/Types/API/Protocol';
import DatabaseConfig from 'CommonServer/DatabaseConfig';
import SmsService from 'CommonServer/Services/SmsService';
import SMS from 'Common/Types/SMS/SMS';
RunCron(
'Incident:SendEmailToSubscribers',
'Incident:SendNotificationToSubscribers',
{ schedule: EVERY_MINUTE, runOnStartup: false },
async () => {
// get all scheduled events of all the projects.
@ -176,6 +178,13 @@ RunCron(
// Send email to Email subscribers.
const resourcesAffectedString: string =
statusPageToResources[statuspage._id!]
?.map((r: StatusPageResource) => {
return r.displayName;
})
.join(', ') || 'None'
for (const subscriber of subscribers) {
if (!subscriber._id) {
continue;
@ -194,23 +203,19 @@ RunCron(
statusPageUrl: statusPageURL,
logoUrl: statuspage.logoFileId
? new URL(httpProtocol, host)
.addRoute(FileRoute)
.addRoute(
'/image/' +
statuspage.logoFileId
)
.toString()
.addRoute(FileRoute)
.addRoute(
'/image/' +
statuspage.logoFileId
)
.toString()
: '',
isPublicStatusPage:
statuspage.isPublicStatusPage
? 'true'
: 'false',
resourcesAffected:
statusPageToResources[statuspage._id!]
?.map((r: StatusPageResource) => {
return r.displayName;
})
.join(', ') || 'None',
resourcesAffectedString,
incidentSeverity:
incident.incidentSeverity?.name ||
' - ',
@ -221,7 +226,7 @@ RunCron(
unsubscribeUrl: new URL(httpProtocol, host)
.addRoute(
'/api/status-page-subscriber/unsubscribe/' +
subscriber._id.toString()
subscriber._id.toString()
)
.toString(),
},
@ -238,6 +243,32 @@ RunCron(
logger.error(err);
});
}
if (subscriber.subscriberPhone) {
const sms: SMS = {
message: `
${statusPageName} - New Incident
Title: ${incident.title || ''}
Severity: ${incident.incidentSeverity?.name || ' - '}
Resources Affected: ${resourcesAffectedString}
Click here for more info: ${statusPageURL}
To unsubscribe, please click here: ${new URL(httpProtocol, host)
.addRoute(
'/api/status-page-subscriber/unsubscribe/' +
subscriber._id.toString()
)
.toString()}
`,
to: subscriber.subscriberPhone,
}
// send sms here.
SmsService.sendSms(sms, {
projectId: statuspage.projectId,
});
}
}
}
}