|
1 | 1 | import { Collection, ObjectId } from 'mongodb'; |
2 | 2 | import AbstractModel from './abstractModel'; |
3 | 3 | import { NotificationsChannelsDBScheme } from '../types/notification-channels'; |
4 | | -import { ProjectDBScheme } from '@hawk.so/types'; |
| 4 | +import { ProjectDBScheme, ProjectNotificationsRuleDBScheme, ProjectEventGroupingPatternsDBScheme } from '@hawk.so/types'; |
5 | 5 | import { v4 as uuid } from 'uuid'; |
6 | 6 |
|
7 | | -/** |
8 | | - * This structure represents a single rule of notifications settings |
9 | | - */ |
10 | | -export interface ProjectNotificationsRuleDBScheme { |
11 | | - /** |
12 | | - * Id of Rule |
13 | | - */ |
14 | | - _id: ObjectId; |
15 | | - |
16 | | - /** |
17 | | - * Allows to disable rule without removing |
18 | | - */ |
19 | | - isEnabled: boolean; |
20 | | - |
21 | | - /** |
22 | | - * Creator of the rule |
23 | | - */ |
24 | | - uidAdded: ObjectId; |
25 | | - |
26 | | - /** |
27 | | - * Receive type: 'SEEN_MORE' or 'ONLY_NEW' |
28 | | - */ |
29 | | - whatToReceive: ReceiveTypes; |
30 | | - |
31 | | - /** |
32 | | - * Only those which contains passed words |
33 | | - */ |
34 | | - including: string[]; |
35 | | - |
36 | | - /** |
37 | | - * Skip those which contains passed words |
38 | | - */ |
39 | | - excluding: string[]; |
40 | | - |
41 | | - /** |
42 | | - * Available channels to receive |
43 | | - */ |
44 | | - channels: NotificationsChannelsDBScheme; |
45 | | - |
46 | | - /** |
47 | | - * If this number of events is reached in the eventThresholdPeriod, the rule will be triggered |
48 | | - */ |
49 | | - threshold?: number; |
50 | | - |
51 | | - /** |
52 | | - * Size of period (in milliseconds) to count events to compare to rule threshold |
53 | | - */ |
54 | | - thresholdPeriod?: number; |
55 | | -} |
56 | | - |
57 | 7 | /** |
58 | 8 | * Available options of 'What to receive' |
59 | 9 | */ |
@@ -159,6 +109,36 @@ interface UpdateProjectNotificationsRulePayload { |
159 | 109 | thresholdPeriod?: number; |
160 | 110 | } |
161 | 111 |
|
| 112 | +/** |
| 113 | + * Payload for creating new project pattern |
| 114 | + */ |
| 115 | +type CreateProjectPatternPayload = { |
| 116 | + pattern: string; |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Payload for updating project patterns |
| 121 | + * It will just rewrite the whole lits of patterns |
| 122 | + */ |
| 123 | +type UpdateProjectPatternPayload = { |
| 124 | + /** |
| 125 | + * Id of the pattern to be updated |
| 126 | + */ |
| 127 | + id: string; |
| 128 | + |
| 129 | + /** |
| 130 | + * New pattern string |
| 131 | + */ |
| 132 | + pattern: string; |
| 133 | +}; |
| 134 | + |
| 135 | +type RemoveProjectPatternPayload = { |
| 136 | + /** |
| 137 | + * Id of the pattern to be removed |
| 138 | + */ |
| 139 | + id: string; |
| 140 | +} |
| 141 | + |
162 | 142 | /** |
163 | 143 | * Project model to work with project data |
164 | 144 | */ |
@@ -208,6 +188,11 @@ export default class ProjectModel extends AbstractModel<ProjectDBScheme> impleme |
208 | 188 | */ |
209 | 189 | public notifications!: ProjectNotificationsRuleDBScheme[]; |
210 | 190 |
|
| 191 | + /** |
| 192 | + * Project events grouping pattern list |
| 193 | + */ |
| 194 | + public eventGroupingPatterns!: ProjectEventGroupingPatternsDBScheme[]; |
| 195 | + |
211 | 196 | /** |
212 | 197 | * Model's collection |
213 | 198 | */ |
@@ -282,6 +267,93 @@ export default class ProjectModel extends AbstractModel<ProjectDBScheme> impleme |
282 | 267 | return rule; |
283 | 268 | } |
284 | 269 |
|
| 270 | + /** |
| 271 | + * Method for appending patterns list with new pattern |
| 272 | + * @param payload - object that contains pattern string |
| 273 | + * @returns - pattern, that has been added |
| 274 | + */ |
| 275 | + public async createProjectEventGroupingPattern(payload: CreateProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> { |
| 276 | + const pattern: ProjectEventGroupingPatternsDBScheme = { |
| 277 | + _id: new ObjectId(), |
| 278 | + pattern: payload.pattern, |
| 279 | + }; |
| 280 | + |
| 281 | + await this.collection.updateOne({ |
| 282 | + _id: this._id, |
| 283 | + }, |
| 284 | + { |
| 285 | + $push: { |
| 286 | + eventGroupingPatterns: { |
| 287 | + $each: [ pattern ], |
| 288 | + $position: 0, |
| 289 | + }, |
| 290 | + }, |
| 291 | + }); |
| 292 | + |
| 293 | + return pattern; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Method that rewrites pattern by id |
| 298 | + * @param payload - object that contains id of the pattern to be updated and new pattern string |
| 299 | + * @returns - updated pattern |
| 300 | + */ |
| 301 | + public async updateProjectEventGroupingPattern(payload: UpdateProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> { |
| 302 | + const udpatedPattern = { |
| 303 | + _id: new ObjectId(payload.id), |
| 304 | + pattern: payload.pattern, |
| 305 | + }; |
| 306 | + |
| 307 | + await this.collection.updateOne({ |
| 308 | + _id: this._id, |
| 309 | + 'eventGroupingPatterns._id': new ObjectId(udpatedPattern._id), |
| 310 | + }, |
| 311 | + { |
| 312 | + $set: { 'eventGroupingPatterns.$.pattern': udpatedPattern.pattern }, |
| 313 | + }); |
| 314 | + |
| 315 | + return udpatedPattern; |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Method that removes pattern by its id |
| 320 | + * @param payload - object that contains id of the pattern to be removed |
| 321 | + */ |
| 322 | + public async removeProjectEventGroupingPattern(payload: RemoveProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> { |
| 323 | + const project = await this.collection.findOne({ |
| 324 | + _id: this._id, |
| 325 | + }); |
| 326 | + |
| 327 | + if (!project) { |
| 328 | + throw new Error('Project with such id does not exist'); |
| 329 | + } |
| 330 | + |
| 331 | + const patternList = await this.collection.findOne( |
| 332 | + { |
| 333 | + _id: this._id, |
| 334 | + 'eventGroupingPatterns._id': new ObjectId(payload.id), |
| 335 | + }, |
| 336 | + { projection: { 'eventGroupingPatterns.$': 1 } } |
| 337 | + ); |
| 338 | + |
| 339 | + const deletedPattern = patternList?.eventGroupingPatterns[0]; |
| 340 | + |
| 341 | + if (deletedPattern === undefined) { |
| 342 | + throw new Error('Pattern with such id does not exist'); |
| 343 | + } |
| 344 | + |
| 345 | + await this.collection.updateOne( |
| 346 | + { |
| 347 | + _id: new ObjectId(this._id), |
| 348 | + }, |
| 349 | + { |
| 350 | + $pull: { eventGroupingPatterns: { _id: new ObjectId(payload.id) } }, |
| 351 | + } |
| 352 | + ); |
| 353 | + |
| 354 | + return deletedPattern; |
| 355 | + } |
| 356 | + |
285 | 357 | /** |
286 | 358 | * Updates notifications rule in project |
287 | 359 | * @param payload - data for updating |
|
0 commit comments