@oneuptime/common 7.0.3652 → 7.0.3668

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 (80) hide show
  1. package/Models/DatabaseModels/Alert.ts +19 -0
  2. package/Models/DatabaseModels/Incident.ts +19 -0
  3. package/Models/DatabaseModels/ScheduledMaintenance.ts +19 -0
  4. package/Server/API/BaseAPI.ts +48 -0
  5. package/Server/Infrastructure/Postgres/SchemaMigrations/1739282331053-MigrationName.ts +29 -0
  6. package/Server/Infrastructure/Postgres/SchemaMigrations/1739374537088-MigrationName.ts +29 -0
  7. package/Server/Infrastructure/Postgres/SchemaMigrations/Index.ts +4 -0
  8. package/Server/Services/IncidentService.ts +101 -0
  9. package/Server/Services/MonitorService.ts +43 -0
  10. package/Server/Services/ProjectService.ts +3 -3
  11. package/Server/Services/TeamMemberService.ts +1 -0
  12. package/Server/Services/UserService.ts +6 -1
  13. package/Server/Services/WorkspaceNotificationRuleService.ts +442 -7
  14. package/Server/Services/WorkspaceProjectAuthTokenService.ts +43 -15
  15. package/Server/Types/Workflow/Components/Slack/SendMessageToChannel.ts +1 -1
  16. package/Server/Utils/Slack/Slack.ts +361 -26
  17. package/Types/Workspace/NotificationRules/BaseNotificationRule.ts +9 -0
  18. package/Types/Workspace/NotificationRules/NotificationRuleCondition.ts +23 -32
  19. package/Types/Workspace/NotificationRules/NotificationRuleTypes/AlertNotificationRule.ts +15 -0
  20. package/Types/Workspace/NotificationRules/NotificationRuleTypes/IncidentNotificationRule.ts +15 -0
  21. package/Types/Workspace/NotificationRules/NotificationRuleTypes/MonitorStatusNotificationRule.ts +7 -0
  22. package/Types/Workspace/NotificationRules/NotificationRuleTypes/ScheduledMaintenanceNotificationRule.ts +14 -0
  23. package/Types/Workspace/NotificationRules/NotificationRuleUtil.ts +261 -0
  24. package/Types/Workspace/WorkspaceChannelInvitationPayload.ts +4 -0
  25. package/Types/Workspace/WorkspaceMessagePayload.ts +30 -0
  26. package/build/dist/Models/DatabaseModels/Alert.js +21 -0
  27. package/build/dist/Models/DatabaseModels/Alert.js.map +1 -1
  28. package/build/dist/Models/DatabaseModels/Incident.js +21 -0
  29. package/build/dist/Models/DatabaseModels/Incident.js.map +1 -1
  30. package/build/dist/Models/DatabaseModels/ScheduledMaintenance.js +21 -0
  31. package/build/dist/Models/DatabaseModels/ScheduledMaintenance.js.map +1 -1
  32. package/build/dist/Server/API/BaseAPI.js +34 -2
  33. package/build/dist/Server/API/BaseAPI.js.map +1 -1
  34. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1739282331053-MigrationName.js +16 -0
  35. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1739282331053-MigrationName.js.map +1 -0
  36. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1739374537088-MigrationName.js +16 -0
  37. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/1739374537088-MigrationName.js.map +1 -0
  38. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js +4 -0
  39. package/build/dist/Server/Infrastructure/Postgres/SchemaMigrations/Index.js.map +1 -1
  40. package/build/dist/Server/Services/IncidentService.js +84 -0
  41. package/build/dist/Server/Services/IncidentService.js.map +1 -1
  42. package/build/dist/Server/Services/MonitorService.js +34 -0
  43. package/build/dist/Server/Services/MonitorService.js.map +1 -1
  44. package/build/dist/Server/Services/ProjectService.js +3 -3
  45. package/build/dist/Server/Services/ProjectService.js.map +1 -1
  46. package/build/dist/Server/Services/TeamMemberService.js +1 -0
  47. package/build/dist/Server/Services/TeamMemberService.js.map +1 -1
  48. package/build/dist/Server/Services/UserService.js +4 -1
  49. package/build/dist/Server/Services/UserService.js.map +1 -1
  50. package/build/dist/Server/Services/WorkspaceNotificationRuleService.js +316 -4
  51. package/build/dist/Server/Services/WorkspaceNotificationRuleService.js.map +1 -1
  52. package/build/dist/Server/Services/WorkspaceProjectAuthTokenService.js +29 -4
  53. package/build/dist/Server/Services/WorkspaceProjectAuthTokenService.js.map +1 -1
  54. package/build/dist/Server/Types/Workflow/Components/Slack/SendMessageToChannel.js +1 -1
  55. package/build/dist/Server/Types/Workflow/Components/Slack/SendMessageToChannel.js.map +1 -1
  56. package/build/dist/Server/Utils/Slack/Slack.js +227 -18
  57. package/build/dist/Server/Utils/Slack/Slack.js.map +1 -1
  58. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleCondition.js +20 -21
  59. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleCondition.js.map +1 -1
  60. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/AlertNotificationRule.js +2 -0
  61. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/AlertNotificationRule.js.map +1 -0
  62. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/IncidentNotificationRule.js +2 -0
  63. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/IncidentNotificationRule.js.map +1 -0
  64. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/MonitorStatusNotificationRule.js +2 -0
  65. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/MonitorStatusNotificationRule.js.map +1 -0
  66. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/ScheduledMaintenanceNotificationRule.js +2 -0
  67. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleTypes/ScheduledMaintenanceNotificationRule.js.map +1 -0
  68. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleUtil.js +216 -0
  69. package/build/dist/Types/Workspace/NotificationRules/NotificationRuleUtil.js.map +1 -0
  70. package/build/dist/Types/Workspace/WorkspaceChannelInvitationPayload.js +2 -0
  71. package/build/dist/Types/Workspace/WorkspaceChannelInvitationPayload.js.map +1 -0
  72. package/build/dist/Types/Workspace/WorkspaceMessagePayload.js +2 -0
  73. package/build/dist/Types/Workspace/WorkspaceMessagePayload.js.map +1 -0
  74. package/package.json +2 -2
  75. package/Types/Workspace/NotificationRules/SlackNotificationRule.ts +0 -19
  76. package/Types/Workspace/WorkspaceNotificationPayload.ts +0 -3
  77. package/build/dist/Types/Workspace/NotificationRules/SlackNotificationRule.js +0 -2
  78. package/build/dist/Types/Workspace/NotificationRules/SlackNotificationRule.js.map +0 -1
  79. package/build/dist/Types/Workspace/WorkspaceNotificationPayload.js +0 -2
  80. package/build/dist/Types/Workspace/WorkspaceNotificationPayload.js.map +0 -1
@@ -1,22 +1,457 @@
1
+ import ObjectID from "../../Types/ObjectID";
1
2
  import NotificationRuleEventType from "../../Types/Workspace/NotificationRules/EventType";
2
- import { WorkspaceNotificationPayload } from "../../Types/Workspace/WorkspaceNotificationPayload";
3
3
  import WorkspaceType from "../../Types/Workspace/WorkspaceType";
4
- import logger from "../Utils/Logger";
5
4
  import DatabaseService from "./DatabaseService";
6
5
  import Model from "Common/Models/DatabaseModels/WorkspaceNotificationRule";
6
+ import IncidentNotificationRule from "../../Types/Workspace/NotificationRules/NotificationRuleTypes/IncidentNotificationRule";
7
+ import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
8
+ import Incident from "../../Models/DatabaseModels/Incident";
9
+ import IncidentService from "./IncidentService";
10
+ import { NotificationRuleConditionCheckOn } from "../../Types/Workspace/NotificationRules/NotificationRuleCondition";
11
+ import BadDataException from "../../Types/Exception/BadDataException";
12
+ import Label from "../../Models/DatabaseModels/Label";
13
+ import MonitorService from "./MonitorService";
14
+ import Alert from "../../Models/DatabaseModels/Alert";
15
+ import AlertService from "./AlertService";
16
+ import ScheduledMaintenanceService from "./ScheduledMaintenanceService";
17
+ import ScheduledMaintenance from "../../Models/DatabaseModels/ScheduledMaintenance";
18
+ import MonitorStatusTimeline from "Common/Models/DatabaseModels/MonitorStatusTimeline";
19
+ import MonitorStatusTimelineService from "./MonitorStatusTimelineService";
20
+ import { WorkspaceNotificationRuleUtil } from "../../Types/Workspace/NotificationRules/NotificationRuleUtil";
21
+ import TeamMemberService from "./TeamMemberService";
22
+ import User from "../../Models/DatabaseModels/User";
23
+ import AlertNotificationRule from "../../Types/Workspace/NotificationRules/NotificationRuleTypes/AlertNotificationRule";
24
+ import ScheduledMaintenanceNotificationRule from "../../Types/Workspace/NotificationRules/NotificationRuleTypes/ScheduledMaintenanceNotificationRule";
25
+ import BaseNotificationRule from "../../Types/Workspace/NotificationRules/BaseNotificationRule";
26
+
27
+ export interface NotificationFor {
28
+ incidentId?: ObjectID | undefined;
29
+ alertId?: ObjectID | undefined;
30
+ scheduledMaintenanceId?: ObjectID | undefined;
31
+ monitorStatusTimelineId?: ObjectID | undefined;
32
+ }
7
33
 
8
34
  export class Service extends DatabaseService<Model> {
9
35
  public constructor() {
10
36
  super(Model);
11
37
  }
12
38
 
13
- public async notifyWorkspace(data: {
39
+ public async getUsersIdsToInviteToChannel(data: {
40
+ notificationRules: Array<
41
+ | IncidentNotificationRule
42
+ | AlertNotificationRule
43
+ | ScheduledMaintenanceNotificationRule
44
+ >;
45
+ }): Promise<Array<ObjectID>> {
46
+ const inviteUserIds: Array<ObjectID> = [];
47
+
48
+ for (const notificationRule of data.notificationRules) {
49
+ const workspaceRules:
50
+ | IncidentNotificationRule
51
+ | AlertNotificationRule
52
+ | ScheduledMaintenanceNotificationRule = notificationRule;
53
+
54
+ if (workspaceRules.shouldCreateNewChannel) {
55
+ if (
56
+ workspaceRules.inviteUsersToNewChannel &&
57
+ workspaceRules.inviteUsersToNewChannel.length > 0
58
+ ) {
59
+ const userIds: Array<ObjectID> =
60
+ workspaceRules.inviteUsersToNewChannel || [];
61
+
62
+ for (const userId of userIds) {
63
+ if (
64
+ !inviteUserIds.find((id: ObjectID) => {
65
+ return id.toString() === userId.toString();
66
+ })
67
+ ) {
68
+ inviteUserIds.push(new ObjectID(userId.toString()));
69
+ }
70
+ }
71
+ }
72
+
73
+ if (
74
+ workspaceRules.inviteTeamsToNewChannel &&
75
+ workspaceRules.inviteTeamsToNewChannel.length > 0
76
+ ) {
77
+ let teamIds: Array<ObjectID> =
78
+ workspaceRules.inviteTeamsToNewChannel || [];
79
+
80
+ teamIds = teamIds.map((teamId: ObjectID) => {
81
+ return new ObjectID(teamId.toString());
82
+ });
83
+
84
+ const usersInTeam: Array<User> =
85
+ await TeamMemberService.getUsersInTeams(teamIds);
86
+
87
+ for (const user of usersInTeam) {
88
+ if (
89
+ !inviteUserIds.find((id: ObjectID) => {
90
+ return id.toString() === user._id?.toString();
91
+ })
92
+ ) {
93
+ const userId: string | undefined = user._id?.toString();
94
+ if (userId) {
95
+ inviteUserIds.push(new ObjectID(userId));
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ return inviteUserIds;
104
+ }
105
+
106
+ public getChannelNamesFromNotificaitonRules(data: {
107
+ notificationRules: Array<BaseNotificationRule>;
108
+ }): Array<string> {
109
+ const channelNames: Array<string> = [];
110
+
111
+ for (const notificationRule of data.notificationRules) {
112
+ const workspaceRules: BaseNotificationRule = notificationRule;
113
+
114
+ if (workspaceRules.shouldPostToExistingChannel) {
115
+ const existingChannelNames: Array<string> =
116
+ workspaceRules.existingChannelNames.split(",");
117
+
118
+ for (const channelName of existingChannelNames) {
119
+ if (!channelName) {
120
+ // if channel name is empty then skip it.
121
+ continue;
122
+ }
123
+
124
+ if (!channelNames.includes(channelName)) {
125
+ // if channel name is not already added then add it.
126
+ channelNames.push(channelName);
127
+ }
128
+ }
129
+ }
130
+ }
131
+
132
+ return channelNames;
133
+ }
134
+
135
+ private async getNotificationRules(data: {
136
+ projectId: ObjectID;
14
137
  workspaceType: WorkspaceType;
15
138
  notificationRuleEventType: NotificationRuleEventType;
16
- workspaceNotificationPayload: WorkspaceNotificationPayload;
17
- }): Promise<void> {
18
- logger.debug("Notify Workspace");
19
- logger.debug(data);
139
+ }): Promise<Array<Model>> {
140
+ return await this.findBy({
141
+ query: {
142
+ projectId: data.projectId,
143
+ workspaceType: data.workspaceType,
144
+ eventType: data.notificationRuleEventType,
145
+ },
146
+ select: {
147
+ notificationRule: true,
148
+ },
149
+ props: {
150
+ isRoot: true,
151
+ },
152
+ skip: 0,
153
+ limit: LIMIT_PER_PROJECT,
154
+ });
155
+ }
156
+
157
+ private async getValuesBasedOnNotificationFor(data: {
158
+ notificationFor: NotificationFor;
159
+ }): Promise<{
160
+ [key in NotificationRuleConditionCheckOn]:
161
+ | string
162
+ | Array<string>
163
+ | undefined;
164
+ }> {
165
+ if (data.notificationFor.incidentId) {
166
+ const incident: Incident | null = await IncidentService.findOneById({
167
+ id: data.notificationFor.incidentId,
168
+ select: {
169
+ title: true,
170
+ description: true,
171
+ incidentSeverity: true,
172
+ currentIncidentState: true,
173
+ labels: true,
174
+ monitors: true,
175
+ },
176
+ props: {
177
+ isRoot: true,
178
+ },
179
+ });
180
+
181
+ if (!incident) {
182
+ throw new BadDataException("Incident ID not found");
183
+ }
184
+
185
+ const monitorLabels: Array<Label> =
186
+ await MonitorService.getLabelsForMonitors({
187
+ monitorIds:
188
+ incident.monitors?.map((monitor: Incident) => {
189
+ return monitor.id!;
190
+ }) || [],
191
+ });
192
+
193
+ return {
194
+ [NotificationRuleConditionCheckOn.MonitorName]: undefined,
195
+ [NotificationRuleConditionCheckOn.IncidentTitle]: incident.title || "",
196
+ [NotificationRuleConditionCheckOn.IncidentDescription]:
197
+ incident.description || "",
198
+ [NotificationRuleConditionCheckOn.IncidentSeverity]:
199
+ incident.incidentSeverity?._id?.toString() || "",
200
+ [NotificationRuleConditionCheckOn.IncidentState]:
201
+ incident.currentIncidentState?._id?.toString() || "",
202
+ [NotificationRuleConditionCheckOn.MonitorType]: undefined,
203
+ [NotificationRuleConditionCheckOn.MonitorStatus]: undefined,
204
+ [NotificationRuleConditionCheckOn.AlertTitle]: undefined,
205
+ [NotificationRuleConditionCheckOn.AlertDescription]: undefined,
206
+ [NotificationRuleConditionCheckOn.AlertSeverity]: undefined,
207
+ [NotificationRuleConditionCheckOn.AlertState]: undefined,
208
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle]: undefined,
209
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription]:
210
+ undefined,
211
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceState]: undefined,
212
+ [NotificationRuleConditionCheckOn.IncidentLabels]:
213
+ incident.labels?.map((label: Label) => {
214
+ return label._id?.toString() || "";
215
+ }) || [],
216
+ [NotificationRuleConditionCheckOn.AlertLabels]: undefined,
217
+ [NotificationRuleConditionCheckOn.MonitorLabels]:
218
+ monitorLabels.map((label: Label) => {
219
+ return label._id?.toString() || "";
220
+ }) || [],
221
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels]:
222
+ undefined,
223
+ [NotificationRuleConditionCheckOn.Monitors]:
224
+ incident.monitors?.map((monitor: Incident) => {
225
+ return monitor._id?.toString() || "";
226
+ }) || [],
227
+ };
228
+ }
229
+
230
+ if (data.notificationFor.alertId) {
231
+ const alert: Alert | null = await AlertService.findOneById({
232
+ id: data.notificationFor.alertId,
233
+ select: {
234
+ title: true,
235
+ description: true,
236
+ alertSeverity: true,
237
+ currentAlertState: true,
238
+ labels: true,
239
+ monitor: true,
240
+ },
241
+ props: {
242
+ isRoot: true,
243
+ },
244
+ });
245
+
246
+ if (!alert) {
247
+ throw new BadDataException("Alert ID not found");
248
+ }
249
+
250
+ const monitorLabels: Array<Label> =
251
+ await MonitorService.getLabelsForMonitors({
252
+ monitorIds: alert?.monitor?.id ? [alert?.monitor?.id] : [],
253
+ });
254
+
255
+ return {
256
+ [NotificationRuleConditionCheckOn.MonitorName]: undefined,
257
+ [NotificationRuleConditionCheckOn.IncidentTitle]: undefined,
258
+ [NotificationRuleConditionCheckOn.IncidentDescription]: undefined,
259
+ [NotificationRuleConditionCheckOn.IncidentSeverity]: undefined,
260
+ [NotificationRuleConditionCheckOn.IncidentState]: undefined,
261
+ [NotificationRuleConditionCheckOn.MonitorType]: undefined,
262
+ [NotificationRuleConditionCheckOn.MonitorStatus]: undefined,
263
+ [NotificationRuleConditionCheckOn.AlertTitle]: alert.title || "",
264
+ [NotificationRuleConditionCheckOn.AlertDescription]:
265
+ alert.description || "",
266
+ [NotificationRuleConditionCheckOn.AlertSeverity]:
267
+ alert.alertSeverity?._id?.toString() || "",
268
+ [NotificationRuleConditionCheckOn.AlertState]:
269
+ alert.currentAlertState?._id?.toString() || "",
270
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle]: undefined,
271
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription]:
272
+ undefined,
273
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceState]: undefined,
274
+ [NotificationRuleConditionCheckOn.IncidentLabels]: undefined,
275
+ [NotificationRuleConditionCheckOn.AlertLabels]:
276
+ alert.labels?.map((label: Label) => {
277
+ return label._id?.toString() || "";
278
+ }) || [],
279
+ [NotificationRuleConditionCheckOn.MonitorLabels]:
280
+ monitorLabels.map((label: Label) => {
281
+ return label._id?.toString() || "";
282
+ }) || [],
283
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels]:
284
+ undefined,
285
+ [NotificationRuleConditionCheckOn.Monitors]: [
286
+ alert.monitor?.id!.toString() || "",
287
+ ],
288
+ };
289
+ }
290
+
291
+ if (data.notificationFor.scheduledMaintenanceId) {
292
+ const scheduledMaintenance: ScheduledMaintenance | null =
293
+ await ScheduledMaintenanceService.findOneById({
294
+ id: data.notificationFor.scheduledMaintenanceId,
295
+ select: {
296
+ title: true,
297
+ description: true,
298
+ currentScheduledMaintenanceState: true,
299
+ labels: true,
300
+ monitors: true,
301
+ },
302
+ props: {
303
+ isRoot: true,
304
+ },
305
+ });
306
+
307
+ if (!scheduledMaintenance) {
308
+ throw new BadDataException("Scheduled Maintenance ID not found");
309
+ }
310
+
311
+ const monitorLabels: Array<Label> =
312
+ await MonitorService.getLabelsForMonitors({
313
+ monitorIds:
314
+ scheduledMaintenance.monitors?.map(
315
+ (monitor: ScheduledMaintenance) => {
316
+ return monitor.id!;
317
+ },
318
+ ) || [],
319
+ });
320
+
321
+ return {
322
+ [NotificationRuleConditionCheckOn.MonitorName]: undefined,
323
+ [NotificationRuleConditionCheckOn.IncidentTitle]: undefined,
324
+ [NotificationRuleConditionCheckOn.IncidentDescription]: undefined,
325
+ [NotificationRuleConditionCheckOn.IncidentSeverity]: undefined,
326
+ [NotificationRuleConditionCheckOn.IncidentState]: undefined,
327
+ [NotificationRuleConditionCheckOn.MonitorType]: undefined,
328
+ [NotificationRuleConditionCheckOn.MonitorStatus]: undefined,
329
+ [NotificationRuleConditionCheckOn.AlertTitle]: undefined,
330
+ [NotificationRuleConditionCheckOn.AlertDescription]: undefined,
331
+ [NotificationRuleConditionCheckOn.AlertSeverity]: undefined,
332
+ [NotificationRuleConditionCheckOn.AlertState]: undefined,
333
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle]:
334
+ scheduledMaintenance.title || "",
335
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription]:
336
+ scheduledMaintenance.description || "",
337
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceState]:
338
+ scheduledMaintenance.currentScheduledMaintenanceState?._id?.toString() ||
339
+ "",
340
+ [NotificationRuleConditionCheckOn.IncidentLabels]: undefined,
341
+ [NotificationRuleConditionCheckOn.AlertLabels]: undefined,
342
+ [NotificationRuleConditionCheckOn.MonitorLabels]:
343
+ monitorLabels.map((label: Label) => {
344
+ return label._id?.toString() || "";
345
+ }) || [],
346
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels]:
347
+ scheduledMaintenance.labels?.map((label: Label) => {
348
+ return label._id?.toString() || "";
349
+ }) || [],
350
+ [NotificationRuleConditionCheckOn.Monitors]:
351
+ scheduledMaintenance.monitors?.map(
352
+ (monitor: ScheduledMaintenance) => {
353
+ return monitor._id?.toString() || "";
354
+ },
355
+ ) || [],
356
+ };
357
+ }
358
+
359
+ if (data.notificationFor.monitorStatusTimelineId) {
360
+ const monitorStatusTimeline: MonitorStatusTimeline | null =
361
+ await MonitorStatusTimelineService.findOneById({
362
+ id: data.notificationFor.monitorStatusTimelineId,
363
+ select: {
364
+ monitor: {
365
+ name: true,
366
+ labels: true,
367
+ monitorType: true,
368
+ },
369
+ monitorStatus: true,
370
+ },
371
+ props: {
372
+ isRoot: true,
373
+ },
374
+ });
375
+
376
+ if (!monitorStatusTimeline) {
377
+ throw new BadDataException("Monitor Status Timeline ID not found");
378
+ }
379
+
380
+ const monitorLabels: Array<Label> =
381
+ monitorStatusTimeline.monitor?.labels || [];
382
+
383
+ return {
384
+ [NotificationRuleConditionCheckOn.MonitorName]:
385
+ monitorStatusTimeline.monitor?.name || "",
386
+ [NotificationRuleConditionCheckOn.IncidentTitle]: undefined,
387
+ [NotificationRuleConditionCheckOn.IncidentDescription]: undefined,
388
+ [NotificationRuleConditionCheckOn.IncidentSeverity]: undefined,
389
+ [NotificationRuleConditionCheckOn.IncidentState]: undefined,
390
+ [NotificationRuleConditionCheckOn.MonitorType]:
391
+ monitorStatusTimeline.monitor?.monitorType || undefined,
392
+ [NotificationRuleConditionCheckOn.MonitorStatus]:
393
+ monitorStatusTimeline.monitorStatus?._id?.toString() || "",
394
+ [NotificationRuleConditionCheckOn.AlertTitle]: undefined,
395
+ [NotificationRuleConditionCheckOn.AlertDescription]: undefined,
396
+ [NotificationRuleConditionCheckOn.AlertSeverity]: undefined,
397
+ [NotificationRuleConditionCheckOn.AlertState]: undefined,
398
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle]: undefined,
399
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription]:
400
+ undefined,
401
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceState]: undefined,
402
+ [NotificationRuleConditionCheckOn.IncidentLabels]: undefined,
403
+ [NotificationRuleConditionCheckOn.AlertLabels]: undefined,
404
+ [NotificationRuleConditionCheckOn.MonitorLabels]:
405
+ monitorLabels.map((label: Label) => {
406
+ return label._id?.toString() || "";
407
+ }) || [],
408
+ [NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels]:
409
+ undefined,
410
+ [NotificationRuleConditionCheckOn.Monitors]: [
411
+ monitorStatusTimeline.monitor?._id?.toString() || "",
412
+ ],
413
+ };
414
+ }
415
+
416
+ throw new BadDataException("NotificationFor is not supported");
417
+ }
418
+
419
+ public async getMatchingNotificationRules(data: {
420
+ projectId: ObjectID;
421
+ workspaceType: WorkspaceType;
422
+ notificationRuleEventType: NotificationRuleEventType;
423
+ notificationFor: NotificationFor;
424
+ }): Promise<Array<Model>> {
425
+ const notificationRules: Array<Model> = await this.getNotificationRules({
426
+ projectId: data.projectId,
427
+ workspaceType: data.workspaceType,
428
+ notificationRuleEventType: data.notificationRuleEventType,
429
+ });
430
+
431
+ const values: {
432
+ [key in NotificationRuleConditionCheckOn]:
433
+ | string
434
+ | Array<string>
435
+ | undefined;
436
+ } = await this.getValuesBasedOnNotificationFor({
437
+ notificationFor: data.notificationFor,
438
+ });
439
+
440
+ const matchingNotificationRules: Array<Model> = [];
441
+
442
+ for (const notificationRule of notificationRules) {
443
+ if (
444
+ WorkspaceNotificationRuleUtil.isRuleMatching({
445
+ notificationRule:
446
+ notificationRule.notificationRule as IncidentNotificationRule,
447
+ values: values,
448
+ })
449
+ ) {
450
+ matchingNotificationRules.push(notificationRule);
451
+ }
452
+ }
453
+
454
+ return matchingNotificationRules;
20
455
  }
21
456
  }
22
457
  export default new Service();
@@ -4,31 +4,59 @@ import DatabaseService from "./DatabaseService";
4
4
  import Model, {
5
5
  SlackMiscData,
6
6
  } from "Common/Models/DatabaseModels/WorkspaceProjectAuthToken";
7
+ import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
7
8
 
8
9
  export class Service extends DatabaseService<Model> {
9
10
  public constructor() {
10
11
  super(Model);
11
12
  }
12
13
 
14
+ public async getProjectAuth(data: {
15
+ projectId: ObjectID;
16
+ workspaceType: WorkspaceType;
17
+ }): Promise<Model | null> {
18
+ return await this.findOneBy({
19
+ query: {
20
+ projectId: data.projectId,
21
+ workspaceType: data.workspaceType,
22
+ },
23
+ select: {
24
+ authToken: true,
25
+ workspaceProjectId: true,
26
+ miscData: true,
27
+ },
28
+ props: {
29
+ isRoot: true,
30
+ },
31
+ });
32
+ }
33
+
34
+ public async getProjectAuths(data: {
35
+ projectId: ObjectID;
36
+ }): Promise<Array<Model>> {
37
+ return await this.findBy({
38
+ query: {
39
+ projectId: data.projectId,
40
+ },
41
+ select: {
42
+ authToken: true,
43
+ workspaceProjectId: true,
44
+ miscData: true,
45
+ workspaceType: true,
46
+ },
47
+ skip: 0,
48
+ limit: LIMIT_PER_PROJECT,
49
+ props: {
50
+ isRoot: true,
51
+ },
52
+ });
53
+ }
54
+
13
55
  public async doesExist(data: {
14
56
  projectId: ObjectID;
15
57
  workspaceType: WorkspaceType;
16
58
  }): Promise<boolean> {
17
- return (
18
- (
19
- await this.countBy({
20
- query: {
21
- projectId: data.projectId,
22
- workspaceType: data.workspaceType,
23
- },
24
- skip: 0,
25
- limit: 1,
26
- props: {
27
- isRoot: true,
28
- },
29
- })
30
- ).toNumber() > 0
31
- );
59
+ return Boolean(await this.getProjectAuth(data));
32
60
  }
33
61
 
34
62
  public async refreshAuthToken(data: {
@@ -69,7 +69,7 @@ export default class SendMessageToChannel extends ComponentCode {
69
69
 
70
70
  try {
71
71
  // https://api.slack.com/messaging/webhooks#advanced_message_formatting
72
- apiResult = await SlackUtil.sendMessageToChannel({
72
+ apiResult = await SlackUtil.sendMessageToChannelViaIncomingWebhook({
73
73
  url: args["webhook-url"] as URL,
74
74
  text: args["text"] as string,
75
75
  });