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) { if (data.data.subscriberPhone) {
// check if this project has SMS enabled. // check if this project has SMS enabled.
const isSMSEnabled: boolean = await ProjectService.isSMSNotificationsEnabled( const isSMSEnabled: boolean =
projectId await ProjectService.isSMSNotificationsEnabled(projectId);
);
if (!isSMSEnabled) { if (!isSMSEnabled) {
throw new BadDataException( throw new BadDataException(

View File

@ -7,6 +7,7 @@ import DataMigrationBase from './DataMigrationBase';
import MigrateDefaultUserNotificationRule from './MigrateDefaultUserNotificationRule'; import MigrateDefaultUserNotificationRule from './MigrateDefaultUserNotificationRule';
import MigrateDefaultUserNotificationSetting from './MigrateDefaultUserSettingNotification'; import MigrateDefaultUserNotificationSetting from './MigrateDefaultUserSettingNotification';
import MigrateToMeteredSubscription from './MigrateToMeteredSubscription'; import MigrateToMeteredSubscription from './MigrateToMeteredSubscription';
import MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage from './MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage';
import UpdateActiveMonitorCountToBillingProvider from './UpdateActiveMonitorCountToBillingProvider'; import UpdateActiveMonitorCountToBillingProvider from './UpdateActiveMonitorCountToBillingProvider';
import UpdateGlobalConfigFromEnv from './UpdateGlobalCongfigFromEnv'; import UpdateGlobalConfigFromEnv from './UpdateGlobalCongfigFromEnv';
@ -23,6 +24,7 @@ const DataMigrations: Array<DataMigrationBase> = [
new AddDefaultGlobalConfig(), new AddDefaultGlobalConfig(),
new UpdateGlobalConfigFromEnv(), new UpdateGlobalConfigFromEnv(),
new AddPostedAtToPublicNotes(), new AddPostedAtToPublicNotes(),
new MoveEnableSubscribersToEnableEmailSubscribersOnStatusPage(),
]; ];
export default DataMigrations; 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'; import './Jobs/Announcement/SendEmailToSubscribers';
// Incidents // Incidents
import './Jobs/Incident/SendEmailToSubscribers'; import './Jobs/Incident/SendNotificationToSubscribers';
import './Jobs/IncidentStateTimeline/SendEmailToSubscribers'; import './Jobs/IncidentStateTimeline/SendEmailToSubscribers';
// Incident Notes // Incident Notes

View File

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