From 54da185280e02cfc2786646ea9d502e95f68432d Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Mon, 26 Jan 2026 22:45:49 +0000 Subject: [PATCH] feat: enhance on-call duty policy execution to support alert episode handling --- .../OnCallDutyPolicyExecutionLogService.ts | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts b/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts index c910747e1c..6cdf79eb62 100644 --- a/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts +++ b/Common/Server/Services/OnCallDutyPolicyExecutionLogService.ts @@ -17,9 +17,12 @@ import ObjectID from "../../Types/ObjectID"; import Color from "../../Types/Color"; import AlertFeedService from "./AlertFeedService"; import { AlertFeedEventType } from "../../Models/DatabaseModels/AlertFeed"; +import AlertEpisodeFeedService from "./AlertEpisodeFeedService"; +import { AlertEpisodeFeedEventType } from "../../Models/DatabaseModels/AlertEpisodeFeed"; import BadDataException from "../../Types/Exception/BadDataException"; import IncidentService from "./IncidentService"; import AlertService from "./AlertService"; +import AlertEpisodeService from "./AlertEpisodeService"; export class Service extends DatabaseService { public constructor() { @@ -55,7 +58,11 @@ export class Service extends DatabaseService { _onCreate: OnCreate, createdItem: Model, ): Promise { - if (createdItem.triggeredByIncidentId || createdItem.triggeredByAlertId) { + if ( + createdItem.triggeredByIncidentId || + createdItem.triggeredByAlertId || + createdItem.triggeredByAlertEpisodeId + ) { const onCallPolicy: OnCallDutyPolicy | null = await OnCallDutyPolicyService.findOneById({ id: createdItem.onCallDutyPolicyId!, @@ -90,6 +97,14 @@ export class Service extends DatabaseService { incidentOrAlertLink = `[Alert ${alertNumber}](${(await AlertService.getAlertLinkInDashboard(createdItem.projectId!, createdItem.triggeredByAlertId)).toString()})`; } + if (createdItem.triggeredByAlertEpisodeId) { + const episodeNumber: number | null = + await AlertEpisodeService.getEpisodeNumber({ + episodeId: createdItem.triggeredByAlertEpisodeId, + }); + incidentOrAlertLink = `[Alert Episode ${episodeNumber}](${(await AlertEpisodeService.getEpisodeLinkInDashboard(createdItem.projectId!, createdItem.triggeredByAlertEpisodeId)).toString()})`; + } + const feedInfoInMarkdown: string = `**📞 On Call Policy Started Executing:** On Call Policy **${onCallPolicy.name}** started executing for ${incidentOrAlertLink}. Users on call on this policy will now be notified.`; if ( @@ -118,6 +133,20 @@ export class Service extends DatabaseService { feedInfoInMarkdown: feedInfoInMarkdown, }); } + + if ( + onCallPolicy && + onCallPolicy.id && + createdItem.triggeredByAlertEpisodeId + ) { + await AlertEpisodeFeedService.createAlertEpisodeFeedItem({ + alertEpisodeId: createdItem.triggeredByAlertEpisodeId, + projectId: createdItem.projectId!, + alertEpisodeFeedEventType: AlertEpisodeFeedEventType.OnCallPolicy, + displayColor: Yellow500, + feedInfoInMarkdown: feedInfoInMarkdown, + }); + } } } @@ -159,6 +188,11 @@ export class Service extends DatabaseService { userNotificationEventType = UserNotificationEventType.AlertCreated; } + if (createdItem.triggeredByAlertEpisodeId) { + userNotificationEventType = + UserNotificationEventType.AlertEpisodeCreated; + } + if (!userNotificationEventType) { throw new BadDataException("Invalid userNotificationEventType"); } @@ -169,6 +203,7 @@ export class Service extends DatabaseService { projectId: createdItem.projectId!, triggeredByIncidentId: createdItem.triggeredByIncidentId, triggeredByAlertId: createdItem.triggeredByAlertId, + triggeredByAlertEpisodeId: createdItem.triggeredByAlertEpisodeId, userNotificationEventType: userNotificationEventType, onCallPolicyExecutionLogId: createdItem.id!, onCallPolicyId: createdItem.onCallDutyPolicyId!, @@ -256,6 +291,7 @@ export class Service extends DatabaseService { statusMessage: true, triggeredByIncidentId: true, triggeredByAlertId: true, + triggeredByAlertEpisodeId: true, }, props: { isRoot: true, @@ -266,7 +302,8 @@ export class Service extends DatabaseService { if ( onCalldutyPolicyExecutionLog && (onCalldutyPolicyExecutionLog.triggeredByIncidentId || - onCalldutyPolicyExecutionLog.triggeredByAlertId) + onCalldutyPolicyExecutionLog.triggeredByAlertId || + onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId) ) { const onCallPolicy: OnCallDutyPolicy | null = await OnCallDutyPolicyService.findOneById({ @@ -308,6 +345,14 @@ export class Service extends DatabaseService { incidentOrAlertLink = `[Alert ${alertNumber}](${(await AlertService.getAlertLinkInDashboard(onCalldutyPolicyExecutionLog.projectId!, onCalldutyPolicyExecutionLog.triggeredByAlertId)).toString()})`; } + if (onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId) { + const episodeNumber: number | null = + await AlertEpisodeService.getEpisodeNumber({ + episodeId: onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId, + }); + incidentOrAlertLink = `[Alert Episode ${episodeNumber}](${(await AlertEpisodeService.getEpisodeLinkInDashboard(onCalldutyPolicyExecutionLog.projectId!, onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId)).toString()})`; + } + const feedInfoInMarkdown: string = `**${this.getEmojiByStatus(onCalldutyPolicyExecutionLog.status)} On Call Policy Status Updated for ${incidentOrAlertLink}:** On-call policy **[${onCallPolicy.name?.toString()}](${(await OnCallDutyPolicyService.getOnCallDutyPolicyLinkInDashboard(onCallPolicy.projectId!, onCallPolicy.id!)).toString()})** status updated to **${onCalldutyPolicyExecutionLog.status}**`; @@ -344,6 +389,22 @@ export class Service extends DatabaseService { feedInfoInMarkdown: feedInfoInMarkdown, }); } + + if (onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId) { + await AlertEpisodeFeedService.createAlertEpisodeFeedItem({ + alertEpisodeId: + onCalldutyPolicyExecutionLog.triggeredByAlertEpisodeId, + projectId: onCalldutyPolicyExecutionLog.projectId!, + alertEpisodeFeedEventType: AlertEpisodeFeedEventType.OnCallPolicy, + displayColor: onCalldutyPolicyExecutionLog.status + ? this.getDisplayColorByStatus( + onCalldutyPolicyExecutionLog.status, + ) + : Blue500, + moreInformationInMarkdown: moreInformationInMarkdown, + feedInfoInMarkdown: feedInfoInMarkdown, + }); + } } } }