From 9dcd295fd06f3d2d93c25fbf405299dd0b78169c Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 14 Jan 2025 13:59:24 +0000 Subject: [PATCH] feat: update incident feed event types for owner additions and enhance related services --- Common/Models/DatabaseModels/IncidentFeed.ts | 3 +- .../Services/IncidentOwnerTeamService.ts | 50 +++++++++++++++++++ .../Services/IncidentOwnerUserService.ts | 50 +++++++++++++++++++ Common/UI/Components/Feed/FeedItem.tsx | 4 +- .../src/Components/Incident/IncidentFeed.tsx | 6 +-- .../SendOwnerAddedNotification.ts | 11 ---- 6 files changed, 107 insertions(+), 17 deletions(-) diff --git a/Common/Models/DatabaseModels/IncidentFeed.ts b/Common/Models/DatabaseModels/IncidentFeed.ts index f53022fd40b..b119bbd004b 100644 --- a/Common/Models/DatabaseModels/IncidentFeed.ts +++ b/Common/Models/DatabaseModels/IncidentFeed.ts @@ -25,7 +25,8 @@ export enum IncidentFeedEventType { PublicNote = "PublicNote", SubscriberNotificationSent = "SubscriberNotificationSent", OwnerNotificationSent = "OwnerNotificationSent", - OwnerAdded = "OwnerAdded", + OwnerUserAdded = "OwnerUserAdded", + OwnerTeamAdded = "OwnerTeamAdded", IncidentCreated = "IncidentCreated", IncidentStateChanged = "IncidentStateChanged", PrivateNote = "PrivateNote", diff --git a/Common/Server/Services/IncidentOwnerTeamService.ts b/Common/Server/Services/IncidentOwnerTeamService.ts index 816900cd658..72848322090 100644 --- a/Common/Server/Services/IncidentOwnerTeamService.ts +++ b/Common/Server/Services/IncidentOwnerTeamService.ts @@ -1,10 +1,60 @@ +import ObjectID from "../../Types/ObjectID"; +import { OnCreate } from "../Types/Database/Hooks"; import DatabaseService from "./DatabaseService"; import Model from "Common/Models/DatabaseModels/IncidentOwnerTeam"; +import IncidentFeedService from "./IncidentFeedService"; +import { IncidentFeedEventType } from "../../Models/DatabaseModels/IncidentFeed"; +import { Blue500 } from "../../Types/BrandColors"; +import TeamService from "./TeamService"; +import Team from "../../Models/DatabaseModels/Team"; export class Service extends DatabaseService { public constructor() { super(Model); } + + public override async onCreateSuccess(onCreate: OnCreate, createdItem: Model): Promise { + // add incident feed. + + const incidentId: ObjectID | undefined = createdItem.incidentId; + const projectId: ObjectID | undefined = createdItem.projectId; + const teamId: ObjectID | undefined = createdItem.teamId; + const createdByUserId: ObjectID | undefined = createdItem.createdByUserId || onCreate.createBy.props.userId; + + + if (incidentId && teamId && projectId) { + + + const team: Team | null = await TeamService.findOneById({ + id: teamId, + select: { + name: true + }, + props: { + isRoot: true + } + }) + + if (team && team.name) { + + + await IncidentFeedService.createIncidentFeed({ + + incidentId: incidentId, + projectId: projectId, + incidentFeedEventType: IncidentFeedEventType.OwnerTeamAdded, + displayColor: Blue500, + feedInfoInMarkdown: `Team ${team.name} was added to the incident as the owner.`, + userId: createdByUserId || undefined, + }); + + } + + } + + return createdItem; + + } } export default new Service(); diff --git a/Common/Server/Services/IncidentOwnerUserService.ts b/Common/Server/Services/IncidentOwnerUserService.ts index 39b501d6a75..a25c6d207e9 100644 --- a/Common/Server/Services/IncidentOwnerUserService.ts +++ b/Common/Server/Services/IncidentOwnerUserService.ts @@ -1,10 +1,60 @@ +import ObjectID from "../../Types/ObjectID"; import DatabaseService from "./DatabaseService"; import Model from "Common/Models/DatabaseModels/IncidentOwnerUser"; +import IncidentFeedService from "./IncidentFeedService"; +import { IncidentFeedEventType } from "../../Models/DatabaseModels/IncidentFeed"; +import { Blue500 } from "../../Types/BrandColors"; +import User from "../../Models/DatabaseModels/User"; +import UserService from "./UserService"; +import { OnCreate } from "../Types/Database/Hooks"; export class Service extends DatabaseService { public constructor() { super(Model); } + + + public override async onCreateSuccess(onCreate: OnCreate, createdItem: Model): Promise { + // add incident feed. + + const incidentId: ObjectID | undefined = createdItem.incidentId; + const projectId: ObjectID | undefined = createdItem.projectId; + const userId: ObjectID | undefined = createdItem.userId; + const createdByUserId: ObjectID | undefined = createdItem.createdByUserId || onCreate.createBy.props.userId; + + + if (incidentId && userId && projectId) { + + + const user: User | null = await UserService.findOneById({ + id: userId, + select: { + name: true, + email: true + }, + props: { + isRoot: true + } + }) + + if (user && user.name) { + + await IncidentFeedService.createIncidentFeed({ + incidentId: incidentId, + projectId: projectId, + incidentFeedEventType: IncidentFeedEventType.OwnerUserAdded, + displayColor: Blue500, + feedInfoInMarkdown: `${user.name.toString()} (${user.email?.toString()}) was added to the incident as the owner.`, + userId: createdByUserId || undefined, + }); + + } + + } + + return createdItem; + + } } export default new Service(); diff --git a/Common/UI/Components/Feed/FeedItem.tsx b/Common/UI/Components/Feed/FeedItem.tsx index 71b83e7b27c..6d84987a28c 100644 --- a/Common/UI/Components/Feed/FeedItem.tsx +++ b/Common/UI/Components/Feed/FeedItem.tsx @@ -58,12 +58,12 @@ const FeedItem: FunctionComponent = ( const getUserIcon: GetReactElementFunction = (): ReactElement => { return
- {!props.user?.profilePictureFile && } - {props.user?.profilePictureFile && } diff --git a/Dashboard/src/Components/Incident/IncidentFeed.tsx b/Dashboard/src/Components/Incident/IncidentFeed.tsx index ac79a903d06..53289aa5482 100644 --- a/Dashboard/src/Components/Incident/IncidentFeed.tsx +++ b/Dashboard/src/Components/Incident/IncidentFeed.tsx @@ -68,12 +68,12 @@ const IncidentFeedElement: FunctionComponent = ( icon = IconProp.Lock; } - if(incidentFeed.incidentFeedEventType === IncidentFeedEventType.OwnerAdded){ + if(incidentFeed.incidentFeedEventType === IncidentFeedEventType.OwnerUserAdded){ icon = IconProp.User; } - if(incidentFeed.incidentFeedEventType === IncidentFeedEventType.OwnerAdded){ - icon = IconProp.User; + if(incidentFeed.incidentFeedEventType === IncidentFeedEventType.OwnerTeamAdded){ + icon = IconProp.Team; } return { diff --git a/Worker/Jobs/IncidentOwners/SendOwnerAddedNotification.ts b/Worker/Jobs/IncidentOwners/SendOwnerAddedNotification.ts index 08158173c1f..030b7ad7add 100644 --- a/Worker/Jobs/IncidentOwners/SendOwnerAddedNotification.ts +++ b/Worker/Jobs/IncidentOwners/SendOwnerAddedNotification.ts @@ -19,9 +19,6 @@ import IncidentOwnerTeam from "Common/Models/DatabaseModels/IncidentOwnerTeam"; import IncidentOwnerUser from "Common/Models/DatabaseModels/IncidentOwnerUser"; import Monitor from "Common/Models/DatabaseModels/Monitor"; import User from "Common/Models/DatabaseModels/User"; -import IncidentFeedService from "Common/Server/Services/IncidentFeedService"; -import { IncidentFeedEventType } from "Common/Models/DatabaseModels/IncidentFeed"; -import { Blue500 } from "Common/Types/BrandColors"; RunCron( "IncidentOwner:SendOwnerAddedEmail", @@ -212,14 +209,6 @@ RunCron( eventType: NotificationSettingEventType.SEND_INCIDENT_OWNER_ADDED_NOTIFICATION, }); - - await IncidentFeedService.createIncidentFeed({ - incidentId: incident.id!, - projectId: incident.projectId!, - incidentFeedEventType: IncidentFeedEventType.OwnerAdded, - displayColor: Blue500, - feedInfoInMarkdown: `User added as owner to this Incident: ${user.name} (${user.email})`, - }); } } },