Skip to content

Commit 5aed97e

Browse files
committed
feat(models): added project patterns methods
1 parent fe49f54 commit 5aed97e

File tree

1 file changed

+136
-51
lines changed

1 file changed

+136
-51
lines changed

src/models/project.ts

Lines changed: 136 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,9 @@
11
import { Collection, ObjectId } from 'mongodb';
22
import AbstractModel from './abstractModel';
33
import { NotificationsChannelsDBScheme } from '../types/notification-channels';
4-
import { ProjectDBScheme } from '@hawk.so/types';
4+
import { ProjectDBScheme, ProjectNotificationsRuleDBScheme, ProjectEventGroupingPatternsDBScheme } from '@hawk.so/types';
55
import { v4 as uuid } from 'uuid';
66

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-
577
/**
588
* Available options of 'What to receive'
599
*/
@@ -159,6 +109,33 @@ interface UpdateProjectNotificationsRulePayload {
159109
thresholdPeriod?: number;
160110
}
161111

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+
id: string,
137+
}
138+
162139
/**
163140
* Project model to work with project data
164141
*/
@@ -208,6 +185,11 @@ export default class ProjectModel extends AbstractModel<ProjectDBScheme> impleme
208185
*/
209186
public notifications!: ProjectNotificationsRuleDBScheme[];
210187

188+
/**
189+
* Project events grouping pattern list
190+
*/
191+
public eventGroupingPatterns!: ProjectEventGroupingPatternsDBScheme[];
192+
211193
/**
212194
* Model's collection
213195
*/
@@ -282,6 +264,109 @@ export default class ProjectModel extends AbstractModel<ProjectDBScheme> impleme
282264
return rule;
283265
}
284266

267+
/**
268+
* Method for appending patterns list with new pattern
269+
* @param payload - object that contains pattern string
270+
* @returns - pattern, that has been added
271+
*/
272+
public async createProjectEventGroupingPattern(payload: CreateProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> {
273+
const pattern: ProjectEventGroupingPatternsDBScheme = {
274+
_id: new ObjectId(),
275+
pattern: payload.pattern,
276+
}
277+
278+
console.log('inserted new pattern to the db');
279+
280+
await this.collection.updateOne({
281+
_id: this._id,
282+
},
283+
{
284+
$push: {
285+
eventGroupingPatterns: {
286+
$each: [ pattern ],
287+
$position: 0,
288+
}
289+
}
290+
});
291+
292+
console.log('pattern: ', pattern);
293+
294+
return pattern;
295+
}
296+
297+
/**
298+
* Method that rewrites pattern by id
299+
* @param payload - object that contains id of the pattern to be updated and new pattern string
300+
* @returns - updated pattern
301+
*/
302+
public async updateProjectEventGroupingPattern(payload: UpdateProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> {
303+
const udpatedPattern = {
304+
_id: new ObjectId(payload.id),
305+
pattern: payload.pattern
306+
}
307+
308+
await this.collection.updateOne({
309+
_id: this._id, "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 gets all patterns for project
320+
* @returns - list of patterns related to the current project
321+
*/
322+
public async getProjectPatterns(): 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+
return project.eventGroupingPatterns ?? [];
332+
}
333+
334+
/**
335+
* Method that removes pattern by its id
336+
* @param payload - object that contains id of the pattern to be removed
337+
*/
338+
public async removeProjectEventGroupingPattern(payload: RemoveProjectPatternPayload): Promise<ProjectEventGroupingPatternsDBScheme> {
339+
const project = await this.collection.findOne({
340+
_id: this._id,
341+
});
342+
343+
if (!project) {
344+
throw new Error ('Project with such id does not exist');
345+
}
346+
347+
const patternList = await this.collection.findOne(
348+
{ _id: this._id, "eventGroupingPatterns._id": new ObjectId(payload.id) },
349+
{ projection: { "eventGroupingPatterns.$": 1 } }
350+
);
351+
352+
const deletedPattern = patternList?.eventGroupingPatterns[0];
353+
354+
if (deletedPattern === undefined) {
355+
throw new Error ('Pattern with such id does not exist')
356+
}
357+
358+
await this.collection.updateOne(
359+
{
360+
_id: new ObjectId(this._id)
361+
},
362+
{
363+
$pull: { eventGroupingPatterns: { _id: new ObjectId(payload.id) } }
364+
}
365+
);
366+
367+
return deletedPattern;
368+
}
369+
285370
/**
286371
* Updates notifications rule in project
287372
* @param payload - data for updating

0 commit comments

Comments
 (0)