@oneuptime/common 7.0.3717 → 7.0.3786

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (203) hide show
  1. package/Models/DatabaseModels/Alert.ts +3 -2
  2. package/Models/DatabaseModels/Incident.ts +3 -2
  3. package/Models/DatabaseModels/Index.ts +3 -0
  4. package/Models/DatabaseModels/ProjectUser.ts +335 -0
  5. package/Models/DatabaseModels/ScheduledMaintenance.ts +3 -2
  6. package/Server/API/SlackAPI.ts +65 -97
  7. package/Server/API/UserOnCallLogTimelineAPI.ts +2 -9
  8. package/Server/Infrastructure/Postgres/SchemaMigrations/1740597525803-MigrationName.ts +17 -0
  9. package/Server/Infrastructure/Postgres/SchemaMigrations/1740598793630-MigrationName.ts +17 -0
  10. package/Server/Infrastructure/Postgres/SchemaMigrations/1741031019972-MigrationName.ts +17 -0
  11. package/Server/Infrastructure/Postgres/SchemaMigrations/1741209339971-MigrationName.ts +101 -0
  12. package/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +8 -0
  13. package/Server/Middleware/SlackAuthorization.ts +11 -2
  14. package/Server/Services/AlertFeedService.ts +2 -2
  15. package/Server/Services/AlertInternalNoteService.ts +2 -2
  16. package/Server/Services/AlertOwnerTeamService.ts +4 -4
  17. package/Server/Services/AlertOwnerUserService.ts +3 -3
  18. package/Server/Services/AlertService.ts +62 -20
  19. package/Server/Services/AlertStateTimelineService.ts +8 -18
  20. package/Server/Services/IncidentFeedService.ts +101 -2
  21. package/Server/Services/IncidentInternalNoteService.ts +47 -4
  22. package/Server/Services/IncidentOwnerTeamService.ts +57 -4
  23. package/Server/Services/IncidentOwnerUserService.ts +59 -15
  24. package/Server/Services/IncidentPublicNoteService.ts +41 -4
  25. package/Server/Services/IncidentService.ts +279 -193
  26. package/Server/Services/IncidentStateService.ts +25 -0
  27. package/Server/Services/IncidentStateTimelineService.ts +37 -19
  28. package/Server/Services/MonitorStatusTimelineService.ts +7 -17
  29. package/Server/Services/OnCallDutyPolicyEscalationRuleService.ts +2 -0
  30. package/Server/Services/OnCallDutyPolicyExecutionLogService.ts +74 -7
  31. package/Server/Services/OnCallDutyPolicyExecutionLogTimelineService.ts +63 -4
  32. package/Server/Services/OnCallDutyPolicyService.ts +13 -0
  33. package/Server/Services/ProjectUserService.ts +130 -0
  34. package/Server/Services/ScheduledMaintenanceFeedService.ts +2 -2
  35. package/Server/Services/ScheduledMaintenanceInternalNoteService.ts +12 -10
  36. package/Server/Services/ScheduledMaintenanceOwnerTeamService.ts +22 -18
  37. package/Server/Services/ScheduledMaintenanceOwnerUserService.ts +28 -30
  38. package/Server/Services/ScheduledMaintenancePublicNoteService.ts +12 -10
  39. package/Server/Services/ScheduledMaintenanceService.ts +16 -10
  40. package/Server/Services/ScheduledMaintenanceStateTimelineService.ts +1 -1
  41. package/Server/Services/StatusPageSubscriberService.ts +3 -0
  42. package/Server/Services/TeamMemberService.ts +20 -0
  43. package/Server/Services/UserNotificationRuleService.ts +74 -0
  44. package/Server/Services/UserOnCallLogService.ts +1 -1
  45. package/Server/Services/UserService.ts +35 -0
  46. package/Server/Services/WorkspaceNotificationRuleService.ts +508 -149
  47. package/Server/Services/WorkspaceUserAuthTokenService.ts +23 -0
  48. package/Server/Utils/Express.ts +1 -1
  49. package/Server/Utils/StartServer.ts +6 -1
  50. package/Server/Utils/Workspace/MicrosoftTeams/Messages/Incident.ts +195 -0
  51. package/Server/Utils/Workspace/Slack/Actions/ActionTypes.ts +20 -0
  52. package/Server/Utils/Workspace/Slack/Actions/Auth.ts +266 -0
  53. package/Server/Utils/Workspace/Slack/Actions/Incident.ts +1117 -0
  54. package/Server/Utils/Workspace/Slack/Messages/Incident.ts +116 -0
  55. package/Server/Utils/Workspace/Slack/Slack.ts +555 -18
  56. package/Server/Utils/Workspace/Slack/app-manifest.json +18 -10
  57. package/Server/Utils/Workspace/Workspace.ts +194 -1
  58. package/Server/Utils/Workspace/WorkspaceBase.ts +145 -19
  59. package/Server/Utils/Workspace/WorkspaceMessages/Incident.ts +68 -0
  60. package/Types/Icon/IconProp.ts +1 -0
  61. package/Types/Workspace/NotificationRules/NotificationRuleCondition.ts +2 -1
  62. package/Types/Workspace/NotificationRules/NotificationRuleUtil.ts +251 -121
  63. package/Types/Workspace/NotificationRules/NotificationRuleWorkspaceChannel.ts +6 -0
  64. package/Types/Workspace/WorkspaceMessagePayload.ts +71 -2
  65. package/UI/Components/ComingSoon/ComingSoon.tsx +13 -3
  66. package/UI/Components/Forms/Fields/FormField.tsx +2 -2
  67. package/UI/Components/Icon/Icon.tsx +39 -2
  68. package/UI/Components/ModelTable/BaseModelTable.tsx +16 -0
  69. package/UI/Components/Radio/Radio.tsx +11 -2
  70. package/UI/Components/Table/TableCard.tsx +2 -2
  71. package/build/dist/Models/DatabaseModels/Alert.js.map +1 -1
  72. package/build/dist/Models/DatabaseModels/Incident.js.map +1 -1
  73. package/build/dist/Models/DatabaseModels/Index.js +2 -0
  74. package/build/dist/Models/DatabaseModels/Index.js.map +1 -1
  75. package/build/dist/Models/DatabaseModels/ProjectUser.js +340 -0
  76. package/build/dist/Models/DatabaseModels/ProjectUser.js.map +1 -0
  77. package/build/dist/Models/DatabaseModels/ScheduledMaintenance.js.map +1 -1
  78. package/build/dist/Server/API/SlackAPI.js +39 -79
  79. package/build/dist/Server/API/SlackAPI.js.map +1 -1
  80. package/build/dist/Server/API/UserOnCallLogTimelineAPI.js.map +1 -1
  81. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1740597525803-MigrationName.js +12 -0
  82. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1740597525803-MigrationName.js.map +1 -0
  83. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1740598793630-MigrationName.js +12 -0
  84. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1740598793630-MigrationName.js.map +1 -0
  85. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1741031019972-MigrationName.js +12 -0
  86. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1741031019972-MigrationName.js.map +1 -0
  87. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1741209339971-MigrationName.js +42 -0
  88. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1741209339971-MigrationName.js.map +1 -0
  89. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js +8 -0
  90. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js.map +1 -1
  91. package/build/dist/Server/Middleware/SlackAuthorization.js +8 -2
  92. package/build/dist/Server/Middleware/SlackAuthorization.js.map +1 -1
  93. package/build/dist/Server/Services/AlertFeedService.js +2 -2
  94. package/build/dist/Server/Services/AlertFeedService.js.map +1 -1
  95. package/build/dist/Server/Services/AlertInternalNoteService.js +2 -2
  96. package/build/dist/Server/Services/AlertInternalNoteService.js.map +1 -1
  97. package/build/dist/Server/Services/AlertOwnerTeamService.js +4 -4
  98. package/build/dist/Server/Services/AlertOwnerTeamService.js.map +1 -1
  99. package/build/dist/Server/Services/AlertOwnerUserService.js +3 -3
  100. package/build/dist/Server/Services/AlertOwnerUserService.js.map +1 -1
  101. package/build/dist/Server/Services/AlertService.js +44 -19
  102. package/build/dist/Server/Services/AlertService.js.map +1 -1
  103. package/build/dist/Server/Services/AlertStateTimelineService.js +6 -16
  104. package/build/dist/Server/Services/AlertStateTimelineService.js.map +1 -1
  105. package/build/dist/Server/Services/IncidentFeedService.js +62 -2
  106. package/build/dist/Server/Services/IncidentFeedService.js.map +1 -1
  107. package/build/dist/Server/Services/IncidentInternalNoteService.js +35 -4
  108. package/build/dist/Server/Services/IncidentInternalNoteService.js.map +1 -1
  109. package/build/dist/Server/Services/IncidentOwnerTeamService.js +42 -4
  110. package/build/dist/Server/Services/IncidentOwnerTeamService.js.map +1 -1
  111. package/build/dist/Server/Services/IncidentOwnerUserService.js +43 -15
  112. package/build/dist/Server/Services/IncidentOwnerUserService.js.map +1 -1
  113. package/build/dist/Server/Services/IncidentPublicNoteService.js +32 -4
  114. package/build/dist/Server/Services/IncidentPublicNoteService.js.map +1 -1
  115. package/build/dist/Server/Services/IncidentService.js +221 -170
  116. package/build/dist/Server/Services/IncidentService.js.map +1 -1
  117. package/build/dist/Server/Services/IncidentStateService.js +14 -0
  118. package/build/dist/Server/Services/IncidentStateService.js.map +1 -1
  119. package/build/dist/Server/Services/IncidentStateTimelineService.js +31 -17
  120. package/build/dist/Server/Services/IncidentStateTimelineService.js.map +1 -1
  121. package/build/dist/Server/Services/MonitorStatusTimelineService.js +5 -15
  122. package/build/dist/Server/Services/MonitorStatusTimelineService.js.map +1 -1
  123. package/build/dist/Server/Services/OnCallDutyPolicyEscalationRuleService.js +1 -0
  124. package/build/dist/Server/Services/OnCallDutyPolicyEscalationRuleService.js.map +1 -1
  125. package/build/dist/Server/Services/OnCallDutyPolicyExecutionLogService.js +62 -7
  126. package/build/dist/Server/Services/OnCallDutyPolicyExecutionLogService.js.map +1 -1
  127. package/build/dist/Server/Services/OnCallDutyPolicyExecutionLogTimelineService.js +51 -5
  128. package/build/dist/Server/Services/OnCallDutyPolicyExecutionLogTimelineService.js.map +1 -1
  129. package/build/dist/Server/Services/OnCallDutyPolicyService.js +6 -0
  130. package/build/dist/Server/Services/OnCallDutyPolicyService.js.map +1 -1
  131. package/build/dist/Server/Services/ProjectUserService.js +106 -0
  132. package/build/dist/Server/Services/ProjectUserService.js.map +1 -0
  133. package/build/dist/Server/Services/ScheduledMaintenanceFeedService.js +2 -2
  134. package/build/dist/Server/Services/ScheduledMaintenanceFeedService.js.map +1 -1
  135. package/build/dist/Server/Services/ScheduledMaintenanceInternalNoteService.js +2 -2
  136. package/build/dist/Server/Services/ScheduledMaintenanceInternalNoteService.js.map +1 -1
  137. package/build/dist/Server/Services/ScheduledMaintenanceOwnerTeamService.js +4 -4
  138. package/build/dist/Server/Services/ScheduledMaintenanceOwnerTeamService.js.map +1 -1
  139. package/build/dist/Server/Services/ScheduledMaintenanceOwnerUserService.js +8 -16
  140. package/build/dist/Server/Services/ScheduledMaintenanceOwnerUserService.js.map +1 -1
  141. package/build/dist/Server/Services/ScheduledMaintenancePublicNoteService.js +2 -2
  142. package/build/dist/Server/Services/ScheduledMaintenancePublicNoteService.js.map +1 -1
  143. package/build/dist/Server/Services/ScheduledMaintenanceService.js +5 -2
  144. package/build/dist/Server/Services/ScheduledMaintenanceService.js.map +1 -1
  145. package/build/dist/Server/Services/ScheduledMaintenanceStateTimelineService.js +1 -1
  146. package/build/dist/Server/Services/ScheduledMaintenanceStateTimelineService.js.map +1 -1
  147. package/build/dist/Server/Services/StatusPageSubscriberService.js +3 -0
  148. package/build/dist/Server/Services/StatusPageSubscriberService.js.map +1 -1
  149. package/build/dist/Server/Services/TeamMemberService.js +17 -0
  150. package/build/dist/Server/Services/TeamMemberService.js.map +1 -1
  151. package/build/dist/Server/Services/UserNotificationRuleService.js +52 -0
  152. package/build/dist/Server/Services/UserNotificationRuleService.js.map +1 -1
  153. package/build/dist/Server/Services/UserOnCallLogService.js +1 -1
  154. package/build/dist/Server/Services/UserOnCallLogService.js.map +1 -1
  155. package/build/dist/Server/Services/UserService.js +23 -0
  156. package/build/dist/Server/Services/UserService.js.map +1 -1
  157. package/build/dist/Server/Services/WorkspaceNotificationRuleService.js +306 -84
  158. package/build/dist/Server/Services/WorkspaceNotificationRuleService.js.map +1 -1
  159. package/build/dist/Server/Services/WorkspaceUserAuthTokenService.js +18 -0
  160. package/build/dist/Server/Services/WorkspaceUserAuthTokenService.js.map +1 -1
  161. package/build/dist/Server/Utils/StartServer.js +4 -0
  162. package/build/dist/Server/Utils/StartServer.js.map +1 -1
  163. package/build/dist/Server/Utils/Workspace/MicrosoftTeams/Messages/Incident.js +148 -0
  164. package/build/dist/Server/Utils/Workspace/MicrosoftTeams/Messages/Incident.js.map +1 -0
  165. package/build/dist/Server/Utils/Workspace/Slack/Actions/ActionTypes.js +19 -0
  166. package/build/dist/Server/Utils/Workspace/Slack/Actions/ActionTypes.js.map +1 -0
  167. package/build/dist/Server/Utils/Workspace/Slack/Actions/Auth.js +167 -0
  168. package/build/dist/Server/Utils/Workspace/Slack/Actions/Auth.js.map +1 -0
  169. package/build/dist/Server/Utils/Workspace/Slack/Actions/Incident.js +727 -0
  170. package/build/dist/Server/Utils/Workspace/Slack/Actions/Incident.js.map +1 -0
  171. package/build/dist/Server/Utils/Workspace/Slack/Messages/Incident.js +82 -0
  172. package/build/dist/Server/Utils/Workspace/Slack/Messages/Incident.js.map +1 -0
  173. package/build/dist/Server/Utils/Workspace/Slack/Slack.js +397 -14
  174. package/build/dist/Server/Utils/Workspace/Slack/Slack.js.map +1 -1
  175. package/build/dist/Server/Utils/Workspace/Slack/app-manifest.json +18 -10
  176. package/build/dist/Server/Utils/Workspace/Workspace.js +126 -0
  177. package/build/dist/Server/Utils/Workspace/Workspace.js.map +1 -1
  178. package/build/dist/Server/Utils/Workspace/WorkspaceBase.js +69 -11
  179. package/build/dist/Server/Utils/Workspace/WorkspaceBase.js.map +1 -1
  180. package/build/dist/Server/Utils/Workspace/WorkspaceMessages/Incident.js +47 -0
  181. package/build/dist/Server/Utils/Workspace/WorkspaceMessages/Incident.js.map +1 -0
  182. package/build/dist/Types/Icon/IconProp.js +1 -0
  183. package/build/dist/Types/Icon/IconProp.js.map +1 -1
  184. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleCondition.js +2 -1
  185. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleCondition.js.map +1 -1
  186. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleUtil.js +214 -120
  187. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleUtil.js.map +1 -1
  188. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleWorkspaceChannel.js +2 -0
  189. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleWorkspaceChannel.js.map +1 -0
  190. package/build/dist/UI/Components/ComingSoon/ComingSoon.js +3 -2
  191. package/build/dist/UI/Components/ComingSoon/ComingSoon.js.map +1 -1
  192. package/build/dist/UI/Components/Forms/Fields/FormField.js +2 -2
  193. package/build/dist/UI/Components/Forms/Fields/FormField.js.map +1 -1
  194. package/build/dist/UI/Components/Icon/Icon.js +19 -2
  195. package/build/dist/UI/Components/Icon/Icon.js.map +1 -1
  196. package/build/dist/UI/Components/ModelTable/BaseModelTable.js +11 -0
  197. package/build/dist/UI/Components/ModelTable/BaseModelTable.js.map +1 -1
  198. package/build/dist/UI/Components/Radio/Radio.js +5 -2
  199. package/build/dist/UI/Components/Radio/Radio.js.map +1 -1
  200. package/build/dist/UI/Components/Table/TableCard.js +2 -2
  201. package/build/dist/UI/Components/Table/TableCard.js.map +1 -1
  202. package/package.json +3 -2
  203. package/Server/Utils/Workspace/Slack/app-manifest.example.json +0 -198
@@ -2,7 +2,6 @@ import ObjectID from "../../Types/ObjectID";
2
2
  import NotificationRuleEventType from "../../Types/Workspace/NotificationRules/EventType";
3
3
  import WorkspaceType from "../../Types/Workspace/WorkspaceType";
4
4
  import DatabaseService from "./DatabaseService";
5
- import Model from "Common/Models/DatabaseModels/WorkspaceNotificationRule";
6
5
  import IncidentNotificationRule from "../../Types/Workspace/NotificationRules/NotificationRuleTypes/IncidentNotificationRule";
7
6
  import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
8
7
  import Incident from "../../Models/DatabaseModels/Incident";
@@ -26,8 +25,9 @@ import { WorkspaceChannel } from "../Utils/Workspace/WorkspaceBase";
26
25
  import WorkspaceUtil from "../Utils/Workspace/Workspace";
27
26
  import WorkspaceUserAuthToken from "../../Models/DatabaseModels/WorkspaceUserAuthToken";
28
27
  import WorkspaceUserAuthTokenService from "./WorkspaceUserAuthTokenService";
29
- import WorkspaceMessagePayload, {
28
+ import {
30
29
  WorkspaceMessageBlock,
30
+ WorkspacePayloadMarkdown,
31
31
  } from "../../Types/Workspace/WorkspaceMessagePayload";
32
32
  import WorkspaceProjectAuthToken, {
33
33
  MiscData,
@@ -35,6 +35,14 @@ import WorkspaceProjectAuthToken, {
35
35
  } from "../../Models/DatabaseModels/WorkspaceProjectAuthToken";
36
36
  import WorkspaceProjectAuthTokenService from "./WorkspaceProjectAuthTokenService";
37
37
  import logger from "../Utils/Logger";
38
+ import NotificationRuleWorkspaceChannel from "../../Types/Workspace/NotificationRules/NotificationRuleWorkspaceChannel";
39
+ import WorkspaceNotificationRule from "Common/Models/DatabaseModels/WorkspaceNotificationRule";
40
+ import UserService from "./UserService";
41
+
42
+ export interface MessageBlocksByWorkspaceType {
43
+ workspaceType: WorkspaceType;
44
+ messageBlocks: Array<WorkspaceMessageBlock>;
45
+ }
38
46
 
39
47
  export interface NotificationFor {
40
48
  incidentId?: ObjectID | undefined;
@@ -43,9 +51,13 @@ export interface NotificationFor {
43
51
  monitorStatusTimelineId?: ObjectID | undefined;
44
52
  }
45
53
 
46
- export class Service extends DatabaseService<Model> {
54
+ export class Service extends DatabaseService<WorkspaceNotificationRule> {
47
55
  public constructor() {
48
- super(Model);
56
+ super(WorkspaceNotificationRule);
57
+ }
58
+
59
+ public static getAllWorkspaceTypes(): Array<WorkspaceType> {
60
+ return [WorkspaceType.Slack, WorkspaceType.MicrosoftTeams];
49
61
  }
50
62
 
51
63
  public getBotUserIdFromprojectAuthToken(data: {
@@ -73,21 +85,53 @@ export class Service extends DatabaseService<Model> {
73
85
  throw new BadDataException("Workspace type not supported");
74
86
  }
75
87
 
76
- public async createInviteAndPostToChannelsBasedOnRules(data: {
88
+ public async getExistingChannelNamesBasedOnEventType(data: {
89
+ projectId: ObjectID;
90
+ workspaceType: WorkspaceType;
91
+ notificationRuleEventType: NotificationRuleEventType;
92
+ }): Promise<Array<string>> {
93
+ logger.debug("getExistingChannelNamesBasedOnEventType called with data:");
94
+ logger.debug(data);
95
+
96
+ const notificationRules: Array<WorkspaceNotificationRule> =
97
+ await this.getNotificationRules({
98
+ projectId: data.projectId,
99
+ workspaceType: data.workspaceType,
100
+ notificationRuleEventType: data.notificationRuleEventType,
101
+ });
102
+
103
+ logger.debug("Notification rules retrieved:");
104
+ logger.debug(notificationRules);
105
+
106
+ const existingChannelNames: Array<string> =
107
+ this.getExistingChannelNamesFromNotificationRules({
108
+ notificationRules: notificationRules.map(
109
+ (rule: WorkspaceNotificationRule) => {
110
+ return rule.notificationRule as BaseNotificationRule;
111
+ },
112
+ ),
113
+ }) || [];
114
+
115
+ logger.debug("Existing channel names:");
116
+ logger.debug(existingChannelNames);
117
+
118
+ return existingChannelNames;
119
+ }
120
+
121
+ public async createChannelsAndInviteUsersToChannelsBasedOnRules(data: {
77
122
  projectId: ObjectID;
78
123
  notificationRuleEventType: NotificationRuleEventType;
79
124
  notificationFor: NotificationFor;
80
125
  channelNameSiffix: string;
81
- messageBlocks: Array<WorkspaceMessageBlock>;
82
126
  }): Promise<{
83
- channelsCreated: Array<WorkspaceChannel>;
127
+ channelsCreated: Array<NotificationRuleWorkspaceChannel>;
84
128
  } | null> {
85
129
  logger.debug(
86
130
  "WorkspaceNotificationRuleService.createInviteAndPostToChannelsBasedOnRules",
87
131
  );
88
132
  logger.debug(data);
89
133
 
90
- const channelsCreated: Array<WorkspaceChannel> = [];
134
+ const channelsCreated: Array<NotificationRuleWorkspaceChannel> = [];
91
135
 
92
136
  const projectAuths: Array<WorkspaceProjectAuthToken> =
93
137
  await WorkspaceProjectAuthTokenService.getProjectAuths({
@@ -114,7 +158,7 @@ export class Service extends DatabaseService<Model> {
114
158
  const authToken: string = projectAuth.authToken;
115
159
  const workspaceType: WorkspaceType = projectAuth.workspaceType;
116
160
 
117
- const notificationRules: Array<Model> =
161
+ const notificationRules: Array<WorkspaceNotificationRule> =
118
162
  await this.getMatchingNotificationRules({
119
163
  projectId: data.projectId,
120
164
  workspaceType: workspaceType,
@@ -130,13 +174,11 @@ export class Service extends DatabaseService<Model> {
130
174
  }
131
175
 
132
176
  logger.debug("Creating channels based on rules");
133
- const createdWorkspaceChannels: Array<WorkspaceChannel> =
177
+ const createdWorkspaceChannels: Array<NotificationRuleWorkspaceChannel> =
134
178
  await this.createChannelsBasedOnRules({
135
- projectOrUserAuthTokenForWorkspasce: authToken,
179
+ projectOrUserAuthTokenForWorkspace: authToken,
136
180
  workspaceType: workspaceType,
137
- notificationRules: notificationRules.map((rule: Model) => {
138
- return rule.notificationRule as CreateChannelNotificationRule;
139
- }),
181
+ notificationRules: notificationRules,
140
182
  channelNameSiffix: data.channelNameSiffix,
141
183
  notificationEventType: data.notificationRuleEventType,
142
184
  });
@@ -147,24 +189,20 @@ export class Service extends DatabaseService<Model> {
147
189
  logger.debug("Inviting users and teams to channels based on rules");
148
190
  await this.inviteUsersAndTeamsToChannelsBasedOnRules({
149
191
  projectId: data.projectId,
150
- projectOrUserAuthTokenForWorkspasce: authToken,
192
+ projectAuth: projectAuth,
151
193
  workspaceType: workspaceType,
152
- notificationRules: notificationRules.map((rule: Model) => {
153
- return rule.notificationRule as CreateChannelNotificationRule;
154
- }),
155
- channelNames: createdWorkspaceChannels.map(
156
- (channel: WorkspaceChannel) => {
157
- return channel.name;
158
- },
159
- ),
194
+ notificationRules: notificationRules,
195
+ notificationChannels: createdWorkspaceChannels,
160
196
  });
161
197
 
162
198
  logger.debug("Getting existing channel names from notification rules");
163
199
  const existingChannelNames: Array<string> =
164
200
  this.getExistingChannelNamesFromNotificationRules({
165
- notificationRules: notificationRules.map((rule: Model) => {
166
- return rule.notificationRule as BaseNotificationRule;
167
- }),
201
+ notificationRules: notificationRules.map(
202
+ (rule: WorkspaceNotificationRule) => {
203
+ return rule.notificationRule as BaseNotificationRule;
204
+ },
205
+ ),
168
206
  }) || [];
169
207
 
170
208
  logger.debug("Existing channel names:");
@@ -181,19 +219,6 @@ export class Service extends DatabaseService<Model> {
181
219
  logger.debug(existingChannelNames);
182
220
 
183
221
  logger.debug("Posting messages to workspace channels");
184
- await this.postToWorkspaceChannels({
185
- workspaceUserId: this.getBotUserIdFromprojectAuthToken({
186
- projectAuthToken: projectAuth,
187
- workspaceType: workspaceType,
188
- }),
189
- projectOrUserAuthTokenForWorkspasce: authToken,
190
- workspaceType: workspaceType,
191
- workspaceMessagePayload: {
192
- _type: "WorkspaceMessagePayload",
193
- channelNames: existingChannelNames,
194
- messageBlocks: data.messageBlocks,
195
- },
196
- });
197
222
 
198
223
  logger.debug("Channels created:");
199
224
  logger.debug(createdWorkspaceChannels);
@@ -207,71 +232,344 @@ export class Service extends DatabaseService<Model> {
207
232
  };
208
233
  }
209
234
 
210
- public async postToWorkspaceChannels(data: {
211
- workspaceUserId: string;
212
- projectOrUserAuthTokenForWorkspasce: string;
235
+ public async getNotificationRulesWhereOnCallIsTrue(data: {
236
+ projectId: ObjectID;
237
+ notificationFor: NotificationFor;
238
+ notificationRuleEventType: NotificationRuleEventType;
239
+ }): Promise<Array<WorkspaceNotificationRule>> {
240
+ const workspaceTypes: Array<WorkspaceType> = Service.getAllWorkspaceTypes();
241
+
242
+ const result: Array<WorkspaceNotificationRule> = [];
243
+
244
+ for (const workspaceType of workspaceTypes) {
245
+ // get matching notification rules
246
+ const notificationRules: Array<WorkspaceNotificationRule> =
247
+ await this.getMatchingNotificationRules({
248
+ projectId: data.projectId,
249
+ notificationFor: data.notificationFor,
250
+ workspaceType: workspaceType,
251
+ notificationRuleEventType: data.notificationRuleEventType,
252
+ });
253
+
254
+ const filteredNotificationRules: Array<WorkspaceNotificationRule> =
255
+ notificationRules.filter((rule: WorkspaceNotificationRule) => {
256
+ return (rule.notificationRule as IncidentNotificationRule)
257
+ .shouldAutomaticallyInviteOnCallUsersToNewChannel;
258
+ });
259
+
260
+ result.push(...filteredNotificationRules);
261
+ }
262
+
263
+ return result;
264
+ }
265
+
266
+ public async getNotificationRulesWhereInviteOwnersIsTrue(data: {
267
+ projectId: ObjectID;
268
+ notificationFor: NotificationFor;
269
+ notificationRuleEventType: NotificationRuleEventType;
270
+ }): Promise<Array<WorkspaceNotificationRule>> {
271
+ const workspaceTypes: Array<WorkspaceType> = Service.getAllWorkspaceTypes();
272
+
273
+ const result: Array<WorkspaceNotificationRule> = [];
274
+
275
+ for (const workspaceType of workspaceTypes) {
276
+ // get matching notification rules
277
+ const notificationRules: Array<WorkspaceNotificationRule> =
278
+ await this.getMatchingNotificationRules({
279
+ projectId: data.projectId,
280
+ notificationFor: data.notificationFor,
281
+ workspaceType: workspaceType,
282
+ notificationRuleEventType: data.notificationRuleEventType,
283
+ });
284
+
285
+ const filteredNotificationRules: Array<WorkspaceNotificationRule> =
286
+ notificationRules.filter((rule: WorkspaceNotificationRule) => {
287
+ return (rule.notificationRule as CreateChannelNotificationRule)
288
+ .shouldInviteOwnersToNewChannel;
289
+ });
290
+
291
+ result.push(...filteredNotificationRules);
292
+ }
293
+
294
+ return result;
295
+ }
296
+
297
+ public async inviteUsersAndTeamsToChannelsBasedOnRules(data: {
298
+ projectId: ObjectID;
299
+ projectAuth: WorkspaceProjectAuthToken;
213
300
  workspaceType: WorkspaceType;
214
- workspaceMessagePayload: WorkspaceMessagePayload;
301
+ notificationRules: Array<WorkspaceNotificationRule>;
302
+ notificationChannels: Array<NotificationRuleWorkspaceChannel>;
215
303
  }): Promise<void> {
216
- logger.debug("postToWorkspaceChannels called with data:");
304
+ logger.debug("inviteUsersAndTeamsToChannelsBasedOnRules called with data:");
217
305
  logger.debug(data);
218
306
 
219
- await WorkspaceUtil.getWorkspaceTypeUtil(data.workspaceType).sendMessage({
220
- userId: data.workspaceUserId,
221
- workspaceMessagePayload: data.workspaceMessagePayload,
222
- authToken: data.projectOrUserAuthTokenForWorkspasce,
307
+ const inviteUserPayloads: Array<{
308
+ notificationRuleId: string;
309
+ userIds: Array<ObjectID>;
310
+ }> = await this.getUsersIdsToInviteToChannel({
311
+ notificationRules: data.notificationRules,
223
312
  });
224
313
 
225
- logger.debug("Message posted to workspace channels successfully");
314
+ logger.debug("User IDs to invite by Workspace Notification Rule ID:");
315
+ logger.debug(inviteUserPayloads);
316
+
317
+ for (const inviteUserPayload of inviteUserPayloads) {
318
+ const userIds: Array<ObjectID> = inviteUserPayload.userIds;
319
+ const workspaceUserIds: Array<string> = [];
320
+
321
+ for (const userId of userIds) {
322
+ const workspaceUserId: string | null =
323
+ await this.getWorkspaceUserIdFromOneUptimeUserId({
324
+ projectId: data.projectId,
325
+ workspaceType: data.workspaceType,
326
+ oneuptimeUserId: userId,
327
+ });
328
+
329
+ if (workspaceUserId) {
330
+ workspaceUserIds.push(workspaceUserId);
331
+ } else {
332
+ try {
333
+ // send a message to channel that user cannot be invited because the account is not connected to workspace.
334
+
335
+ const channelIds: Array<string> =
336
+ data.notificationChannels
337
+ ?.filter((channel: NotificationRuleWorkspaceChannel) => {
338
+ return (
339
+ channel.notificationRuleId ===
340
+ inviteUserPayload.notificationRuleId
341
+ );
342
+ })
343
+ ?.map((channel: NotificationRuleWorkspaceChannel) => {
344
+ return channel.id as string;
345
+ }) || [];
346
+
347
+ logger.debug("Channel IDs to send message to:");
348
+ logger.debug(channelIds);
349
+
350
+ await WorkspaceUtil.getWorkspaceTypeUtil(
351
+ data.workspaceType,
352
+ ).sendMessage({
353
+ userId: data.projectAuth.workspaceProjectId!,
354
+ authToken: data.projectAuth.authToken!,
355
+ workspaceMessagePayload: {
356
+ _type: "WorkspaceMessagePayload",
357
+ channelNames: [],
358
+
359
+ channelIds: channelIds,
360
+ workspaceType: data.workspaceType,
361
+ messageBlocks: [
362
+ {
363
+ _type: "WorkspacePayloadMarkdown",
364
+ text: `${await UserService.getUserMarkdownString({
365
+ userId: userId,
366
+ projectId: data.projectId,
367
+ })} cannot be invited to the channel because the account is not connected to ${data.workspaceType}. Please go to User Settings > ${data.workspaceType} on OneUptime Dashboard and connect the account.`,
368
+ } as WorkspacePayloadMarkdown,
369
+ ],
370
+ },
371
+ });
372
+ } catch (e) {
373
+ logger.error("Error in sending message to channel");
374
+ logger.error(e);
375
+ }
376
+ }
377
+
378
+ logger.debug("Workspace User IDs to invite:");
379
+ logger.debug(workspaceUserIds);
380
+
381
+ await WorkspaceUtil.getWorkspaceTypeUtil(
382
+ data.workspaceType,
383
+ ).inviteUsersToChannels({
384
+ authToken: data.projectAuth.authToken!,
385
+ workspaceChannelInvitationPayload: {
386
+ channelNames: data.notificationChannels
387
+ .filter((channel: NotificationRuleWorkspaceChannel) => {
388
+ return (
389
+ channel.notificationRuleId ===
390
+ inviteUserPayload.notificationRuleId
391
+ );
392
+ })
393
+ .map((channel: NotificationRuleWorkspaceChannel) => {
394
+ return channel.name;
395
+ }),
396
+ workspaceUserIds: workspaceUserIds,
397
+ },
398
+ });
399
+ }
400
+ }
401
+
402
+ logger.debug("Users invited to channels successfully");
226
403
  }
227
404
 
228
- public async inviteUsersAndTeamsToChannelsBasedOnRules(data: {
405
+ public async inviteUsersBasedOnRulesAndWorkspaceChannels(data: {
406
+ workspaceChannels: Array<NotificationRuleWorkspaceChannel>;
229
407
  projectId: ObjectID;
230
- projectOrUserAuthTokenForWorkspasce: string;
231
- workspaceType: WorkspaceType;
232
- notificationRules: Array<CreateChannelNotificationRule>;
233
- channelNames: Array<string>;
408
+ notificationRules: Array<WorkspaceNotificationRule>;
409
+ userIds: Array<ObjectID>;
234
410
  }): Promise<void> {
235
- logger.debug("inviteUsersAndTeamsToChannelsBasedOnRules called with data:");
411
+ logger.debug(
412
+ "inviteUsersBasedOnRulesAndWorkspaceChannels called with data:",
413
+ );
236
414
  logger.debug(data);
415
+ const userIds: Array<ObjectID> = data.userIds;
237
416
 
238
- const inviteUserIds: Array<ObjectID> =
239
- await this.getUsersIdsToInviteToChannel({
240
- notificationRules: data.notificationRules,
241
- });
417
+ logger.debug("Users:");
418
+ logger.debug(userIds);
242
419
 
243
- logger.debug("User IDs to invite:");
244
- logger.debug(inviteUserIds);
420
+ // get all Workspaces.
421
+ const workspaceTypes: Array<WorkspaceType> = Service.getAllWorkspaceTypes();
245
422
 
246
- const workspaceUserIds: Array<string> = [];
423
+ for (const workspaceType of workspaceTypes) {
424
+ // filter rules by workspaceType.
247
425
 
248
- for (const userId of inviteUserIds) {
249
- const workspaceUserId: string | null =
250
- await this.getWorkspaceUserIdFromOneUptimeUserId({
251
- projectId: data.projectId,
252
- workspaceType: data.workspaceType,
253
- oneuptimeUserId: userId,
426
+ const notificationRules: Array<WorkspaceNotificationRule> =
427
+ data.notificationRules.filter((rule: WorkspaceNotificationRule) => {
428
+ return rule.workspaceType === workspaceType;
429
+ });
430
+
431
+ logger.debug("Notification rules for workspace type:");
432
+ logger.debug(notificationRules);
433
+
434
+ const channelsToInviteToBasedOnRule: Array<NotificationRuleWorkspaceChannel> =
435
+ data.workspaceChannels.filter(
436
+ (channel: NotificationRuleWorkspaceChannel) => {
437
+ return notificationRules.find((rule: WorkspaceNotificationRule) => {
438
+ return rule.id?.toString() === channel.notificationRuleId;
439
+ });
440
+ },
441
+ );
442
+
443
+ logger.debug("Channels to invite to based on rule:");
444
+ logger.debug(channelsToInviteToBasedOnRule);
445
+
446
+ if (channelsToInviteToBasedOnRule.length === 0) {
447
+ logger.debug("No channels to invite to based on rule.");
448
+ continue;
449
+ }
450
+
451
+ // get auth token for workspace.
452
+
453
+ const projectAuth: WorkspaceProjectAuthToken | null =
454
+ await WorkspaceProjectAuthTokenService.findOneBy({
455
+ query: {
456
+ projectId: data.projectId,
457
+ workspaceType: workspaceType,
458
+ },
459
+ select: {
460
+ authToken: true,
461
+ },
462
+ props: {
463
+ isRoot: true,
464
+ },
254
465
  });
255
466
 
256
- if (workspaceUserId) {
257
- workspaceUserIds.push(workspaceUserId);
467
+ if (!projectAuth) {
468
+ logger.debug("No project auth found for workspace type");
469
+ continue;
258
470
  }
471
+
472
+ // inivte users to channels.
473
+
474
+ const workspaceUserIds: Array<string> = [];
475
+
476
+ for (const userId of userIds) {
477
+ const workspaceUserId: string | null =
478
+ await this.getWorkspaceUserIdFromOneUptimeUserId({
479
+ projectId: data.projectId,
480
+ workspaceType: workspaceType,
481
+ oneuptimeUserId: userId,
482
+ });
483
+
484
+ if (workspaceUserId) {
485
+ workspaceUserIds.push(workspaceUserId);
486
+ } else {
487
+ try {
488
+ // send a message to channel that user cannot be invited because the account is not connected to workspace.
489
+
490
+ const channelIds: Array<string> = channelsToInviteToBasedOnRule.map(
491
+ (channel: NotificationRuleWorkspaceChannel) => {
492
+ return channel.id as string;
493
+ },
494
+ );
495
+
496
+ logger.debug("Channel IDs to send message to:");
497
+ logger.debug(channelIds);
498
+
499
+ await WorkspaceUtil.getWorkspaceTypeUtil(workspaceType).sendMessage(
500
+ {
501
+ userId: projectAuth.workspaceProjectId!,
502
+ authToken: projectAuth.authToken!,
503
+ workspaceMessagePayload: {
504
+ _type: "WorkspaceMessagePayload",
505
+ channelNames: [],
506
+
507
+ channelIds: channelIds,
508
+ workspaceType: workspaceType,
509
+ messageBlocks: [
510
+ {
511
+ _type: "WorkspacePayloadMarkdown",
512
+ text: `${await UserService.getUserMarkdownString({
513
+ userId: userId,
514
+ projectId: data.projectId,
515
+ })} cannot be invited to the channel because the account is not connected to ${workspaceType}. Please go to User Settings > ${workspaceType} on OneUptime Dashboard and connect the account.`,
516
+ } as WorkspacePayloadMarkdown,
517
+ ],
518
+ },
519
+ },
520
+ );
521
+ } catch (e) {
522
+ logger.error("Error in sending message to channel");
523
+ logger.error(e);
524
+ }
525
+ }
526
+ }
527
+
528
+ logger.debug("Workspace User IDs to invite:");
529
+ logger.debug(workspaceUserIds);
530
+
531
+ const channelNames: Array<string> = channelsToInviteToBasedOnRule.map(
532
+ (channel: NotificationRuleWorkspaceChannel) => {
533
+ return channel.name;
534
+ },
535
+ );
536
+
537
+ logger.debug("Channel names to invite to:");
538
+ logger.debug(channelNames);
539
+
540
+ await WorkspaceUtil.getWorkspaceTypeUtil(
541
+ workspaceType,
542
+ ).inviteUsersToChannels({
543
+ authToken: projectAuth.authToken!,
544
+ workspaceChannelInvitationPayload: {
545
+ channelNames: channelNames,
546
+ workspaceUserIds: workspaceUserIds,
547
+ },
548
+ });
259
549
  }
550
+ }
260
551
 
261
- logger.debug("Workspace User IDs to invite:");
262
- logger.debug(workspaceUserIds);
263
-
264
- await WorkspaceUtil.getWorkspaceTypeUtil(
265
- data.workspaceType,
266
- ).inviteUsersToChannels({
267
- authToken: data.projectOrUserAuthTokenForWorkspasce,
268
- workspaceChannelInvitationPayload: {
269
- channelNames: data.channelNames,
270
- workspaceUserIds: workspaceUserIds,
271
- },
272
- });
552
+ public async inviteTeamsBasedOnRulesAndWorkspaceChannels(data: {
553
+ workspaceChannels: Array<NotificationRuleWorkspaceChannel>;
554
+ projectId: ObjectID;
555
+ notificationRules: Array<WorkspaceNotificationRule>;
556
+ teamIds: Array<ObjectID>;
557
+ }): Promise<void> {
558
+ const usersInTeam: Array<User> = await TeamMemberService.getUsersInTeams(
559
+ data.teamIds,
560
+ );
273
561
 
274
- logger.debug("Users invited to channels successfully");
562
+ logger.debug("Users in teams:");
563
+ logger.debug(usersInTeam);
564
+
565
+ return this.inviteUsersBasedOnRulesAndWorkspaceChannels({
566
+ workspaceChannels: data.workspaceChannels,
567
+ projectId: data.projectId,
568
+ notificationRules: data.notificationRules,
569
+ userIds: usersInTeam.map((user: User) => {
570
+ return user.id!;
571
+ }),
572
+ });
275
573
  }
276
574
 
277
575
  public async getWorkspaceUserIdFromOneUptimeUserId(data: {
@@ -309,55 +607,69 @@ export class Service extends DatabaseService<Model> {
309
607
  }
310
608
 
311
609
  public async createChannelsBasedOnRules(data: {
312
- projectOrUserAuthTokenForWorkspasce: string;
610
+ projectOrUserAuthTokenForWorkspace: string;
313
611
  workspaceType: WorkspaceType;
314
- notificationRules: Array<CreateChannelNotificationRule>;
612
+ notificationRules: Array<WorkspaceNotificationRule>;
315
613
  channelNameSiffix: string;
316
614
  notificationEventType: NotificationRuleEventType;
317
- }): Promise<Array<WorkspaceChannel>> {
615
+ }): Promise<Array<NotificationRuleWorkspaceChannel>> {
318
616
  logger.debug("createChannelsBasedOnRules called with data:");
319
617
  logger.debug(data);
320
618
 
321
- const createdWorkspaceChannels: Array<WorkspaceChannel> = [];
619
+ const createdWorkspaceChannels: Array<NotificationRuleWorkspaceChannel> =
620
+ [];
322
621
  const createdChannelNames: Array<string> = [];
323
622
 
324
- const newChannelNames: Array<string> =
325
- this.getNewChannelNamesFromNotificationRules({
326
- notificationRules: data.notificationRules,
327
- channelNameSiffix: data.channelNameSiffix,
328
- notificationEventType: data.notificationEventType,
329
- });
623
+ const notificationChannels: Array<{
624
+ channelName: string;
625
+ notificationRuleId: string;
626
+ }> = this.getnotificationChannelssFromNotificationRules({
627
+ notificationRules: data.notificationRules,
628
+ channelNameSiffix: data.channelNameSiffix,
629
+ notificationEventType: data.notificationEventType,
630
+ });
330
631
 
331
632
  logger.debug("New channel names to be created:");
332
- logger.debug(newChannelNames);
633
+ logger.debug(notificationChannels);
333
634
 
334
- if (!newChannelNames || newChannelNames.length === 0) {
635
+ if (!notificationChannels || notificationChannels.length === 0) {
335
636
  logger.debug("No new channel names found. Returning empty array.");
336
637
  return [];
337
638
  }
338
639
 
339
- for (const newChannelName of newChannelNames) {
340
- if (createdChannelNames.includes(newChannelName)) {
640
+ for (const notificationChannel of notificationChannels) {
641
+ if (
642
+ createdChannelNames.filter((name: string) => {
643
+ return name === notificationChannel.channelName;
644
+ }).length > 0
645
+ ) {
341
646
  logger.debug(
342
- `Channel name ${newChannelName} already created. Skipping.`,
647
+ `Channel name ${notificationChannel.channelName} already created. Skipping.`,
343
648
  );
344
649
  continue;
345
650
  }
346
651
 
347
- logger.debug(`Creating new channel with name: ${newChannelName}`);
652
+ logger.debug(
653
+ `Creating new channel with name: ${notificationChannel.channelName}`,
654
+ );
348
655
  const channel: WorkspaceChannel =
349
656
  await WorkspaceUtil.getWorkspaceTypeUtil(
350
657
  data.workspaceType,
351
658
  ).createChannel({
352
- authToken: data.projectOrUserAuthTokenForWorkspasce,
353
- channelName: newChannelName,
659
+ authToken: data.projectOrUserAuthTokenForWorkspace,
660
+ channelName: notificationChannel.channelName,
354
661
  });
355
662
 
663
+ const notificationWorkspaceChannel: NotificationRuleWorkspaceChannel = {
664
+ ...channel,
665
+ notificationRuleId: notificationChannel.notificationRuleId,
666
+ };
667
+
356
668
  logger.debug("Channel created:");
357
669
  logger.debug(channel);
358
670
 
359
671
  createdChannelNames.push(channel.name);
360
- createdWorkspaceChannels.push(channel);
672
+ createdWorkspaceChannels.push(notificationWorkspaceChannel);
361
673
  }
362
674
 
363
675
  logger.debug("Returning created workspace channels:");
@@ -367,15 +679,25 @@ export class Service extends DatabaseService<Model> {
367
679
  }
368
680
 
369
681
  public async getUsersIdsToInviteToChannel(data: {
370
- notificationRules: Array<CreateChannelNotificationRule>;
371
- }): Promise<Array<ObjectID>> {
682
+ notificationRules: Array<WorkspaceNotificationRule>;
683
+ }): Promise<
684
+ Array<{
685
+ notificationRuleId: string;
686
+ userIds: Array<ObjectID>;
687
+ }>
688
+ > {
372
689
  logger.debug("getUsersIdsToInviteToChannel called with data:");
373
690
  logger.debug(data);
374
691
 
375
- const inviteUserIds: Array<ObjectID> = [];
692
+ const result: Array<{
693
+ notificationRuleId: string;
694
+ userIds: Array<ObjectID>;
695
+ }> = [];
376
696
 
377
- for (const notificationRule of data.notificationRules) {
378
- const workspaceRules: CreateChannelNotificationRule = notificationRule;
697
+ for (const workspaceNotificationRule of data.notificationRules) {
698
+ const inviteUserIds: Array<ObjectID> = [];
699
+ const workspaceRules: CreateChannelNotificationRule =
700
+ workspaceNotificationRule.notificationRule as CreateChannelNotificationRule;
379
701
 
380
702
  if (workspaceRules.shouldCreateNewChannel) {
381
703
  if (
@@ -433,12 +755,19 @@ export class Service extends DatabaseService<Model> {
433
755
  }
434
756
  }
435
757
  }
758
+
759
+ if (inviteUserIds.length > 0) {
760
+ result.push({
761
+ notificationRuleId: workspaceNotificationRule.id!.toString(),
762
+ userIds: inviteUserIds,
763
+ });
764
+ }
436
765
  }
437
766
 
438
767
  logger.debug("Final list of user IDs to invite:");
439
- logger.debug(inviteUserIds);
768
+ logger.debug(result);
440
769
 
441
- return inviteUserIds;
770
+ return result;
442
771
  }
443
772
 
444
773
  public getExistingChannelNamesFromNotificationRules(data: {
@@ -480,42 +809,58 @@ export class Service extends DatabaseService<Model> {
480
809
  return channelNames;
481
810
  }
482
811
 
483
- public getNewChannelNamesFromNotificationRules(data: {
812
+ public getnotificationChannelssFromNotificationRules(data: {
484
813
  notificationEventType: NotificationRuleEventType;
485
- notificationRules: Array<CreateChannelNotificationRule>;
814
+ notificationRules: Array<WorkspaceNotificationRule>;
486
815
  channelNameSiffix: string;
487
- }): Array<string> {
488
- logger.debug("getNewChannelNamesFromNotificationRules called with data:");
816
+ }): Array<{
817
+ channelName: string;
818
+ notificationRuleId: string;
819
+ }> {
820
+ logger.debug(
821
+ "getnotificationChannelssFromNotificationRules called with data:",
822
+ );
489
823
  logger.debug(data);
490
824
 
491
- const channelNames: Array<string> = [];
825
+ const channels: Array<{
826
+ channelName: string;
827
+ notificationRuleId: string;
828
+ }> = [];
492
829
 
493
830
  for (const notificationRule of data.notificationRules) {
494
- const workspaceRules: CreateChannelNotificationRule = notificationRule;
831
+ const workspaceRules: CreateChannelNotificationRule =
832
+ notificationRule.notificationRule as CreateChannelNotificationRule;
495
833
 
496
834
  logger.debug("Processing notification rule:");
497
835
  logger.debug(workspaceRules);
498
836
 
499
- if (
500
- workspaceRules.shouldCreateNewChannel &&
501
- workspaceRules.newChannelTemplateName
502
- ) {
503
- const newChannelName: string =
837
+ if (workspaceRules.shouldCreateNewChannel) {
838
+ const notificationChannels: string =
504
839
  workspaceRules.newChannelTemplateName ||
505
840
  `oneuptime-${data.notificationEventType.toLowerCase()}-`;
506
841
 
507
842
  logger.debug("New channel template name:");
508
- logger.debug(newChannelName);
843
+ logger.debug(notificationChannels);
509
844
 
510
845
  // add suffix and then check if it is already added or not.
511
- const channelName: string = newChannelName + data.channelNameSiffix;
846
+ const channelName: string =
847
+ notificationChannels + data.channelNameSiffix;
512
848
 
513
849
  logger.debug("Final channel name with suffix:");
514
850
  logger.debug(channelName);
515
851
 
516
- if (!channelNames.includes(channelName)) {
852
+ if (
853
+ channels.filter(
854
+ (name: { channelName: string; notificationRuleId: string }) => {
855
+ return name.channelName === channelName;
856
+ },
857
+ ).length === 0
858
+ ) {
517
859
  // if channel name is not already added then add it.
518
- channelNames.push(channelName);
860
+ channels.push({
861
+ channelName: channelName,
862
+ notificationRuleId: notificationRule.id!.toString() || "",
863
+ });
519
864
  logger.debug(`Channel name ${channelName} added to the list.`);
520
865
  } else {
521
866
  logger.debug(
@@ -526,34 +871,37 @@ export class Service extends DatabaseService<Model> {
526
871
  }
527
872
 
528
873
  logger.debug("Final list of new channel names:");
529
- logger.debug(channelNames);
874
+ logger.debug(channels);
530
875
 
531
- return channelNames;
876
+ return channels;
532
877
  }
533
878
 
534
879
  private async getNotificationRules(data: {
535
880
  projectId: ObjectID;
536
881
  workspaceType: WorkspaceType;
537
882
  notificationRuleEventType: NotificationRuleEventType;
538
- }): Promise<Array<Model>> {
883
+ }): Promise<Array<WorkspaceNotificationRule>> {
539
884
  logger.debug("getNotificationRules called with data:");
540
885
  logger.debug(data);
541
886
 
542
- const notificationRules: Array<Model> = await this.findBy({
543
- query: {
544
- projectId: data.projectId,
545
- workspaceType: data.workspaceType,
546
- eventType: data.notificationRuleEventType,
547
- },
548
- select: {
549
- notificationRule: true,
550
- },
551
- props: {
552
- isRoot: true,
553
- },
554
- skip: 0,
555
- limit: LIMIT_PER_PROJECT,
556
- });
887
+ const notificationRules: Array<WorkspaceNotificationRule> =
888
+ await this.findBy({
889
+ query: {
890
+ projectId: data.projectId,
891
+ workspaceType: data.workspaceType,
892
+ eventType: data.notificationRuleEventType,
893
+ },
894
+ select: {
895
+ notificationRule: true,
896
+ workspaceType: true,
897
+ eventType: true,
898
+ },
899
+ props: {
900
+ isRoot: true,
901
+ },
902
+ skip: 0,
903
+ limit: LIMIT_PER_PROJECT,
904
+ });
557
905
 
558
906
  logger.debug("Notification rules retrieved:");
559
907
  logger.debug(notificationRules);
@@ -869,12 +1217,16 @@ export class Service extends DatabaseService<Model> {
869
1217
  workspaceType: WorkspaceType;
870
1218
  notificationRuleEventType: NotificationRuleEventType;
871
1219
  notificationFor: NotificationFor;
872
- }): Promise<Array<Model>> {
873
- const notificationRules: Array<Model> = await this.getNotificationRules({
874
- projectId: data.projectId,
875
- workspaceType: data.workspaceType,
876
- notificationRuleEventType: data.notificationRuleEventType,
877
- });
1220
+ }): Promise<Array<WorkspaceNotificationRule>> {
1221
+ const notificationRules: Array<WorkspaceNotificationRule> =
1222
+ await this.getNotificationRules({
1223
+ projectId: data.projectId,
1224
+ workspaceType: data.workspaceType,
1225
+ notificationRuleEventType: data.notificationRuleEventType,
1226
+ });
1227
+
1228
+ logger.debug("Notification rules retrieved:");
1229
+ logger.debug(notificationRules);
878
1230
 
879
1231
  const values: {
880
1232
  [key in NotificationRuleConditionCheckOn]:
@@ -885,9 +1237,13 @@ export class Service extends DatabaseService<Model> {
885
1237
  notificationFor: data.notificationFor,
886
1238
  });
887
1239
 
888
- const matchingNotificationRules: Array<Model> = [];
1240
+ logger.debug("Values based on notification for:");
1241
+ logger.debug(values);
1242
+
1243
+ const matchingNotificationRules: Array<WorkspaceNotificationRule> = [];
889
1244
 
890
1245
  for (const notificationRule of notificationRules) {
1246
+ logger.debug("Checking if rule matches:");
891
1247
  if (
892
1248
  WorkspaceNotificationRuleUtil.isRuleMatching({
893
1249
  notificationRule:
@@ -895,7 +1251,10 @@ export class Service extends DatabaseService<Model> {
895
1251
  values: values,
896
1252
  })
897
1253
  ) {
1254
+ logger.debug("Rule matches. Adding to the list.");
898
1255
  matchingNotificationRules.push(notificationRule);
1256
+ } else {
1257
+ logger.debug("Rule does not match. Skipping.");
899
1258
  }
900
1259
  }
901
1260