Skip to content

Commit 265154b

Browse files
committed
fix(): fix new pattern validation
1 parent e56f1db commit 265154b

2 files changed

Lines changed: 29 additions & 32 deletions

File tree

src/models/project.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -315,22 +315,6 @@ export default class ProjectModel extends AbstractModel<ProjectDBScheme> impleme
315315
return udpatedPattern;
316316
}
317317

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-
334318
/**
335319
* Method that removes pattern by its id
336320
* @param payload - object that contains id of the pattern to be removed

src/resolvers/projectPatterns.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,38 @@ interface UpdateProjectPatternMutationPayload {
4141
* Type that represents payload for remove project pattern mutation
4242
*/
4343
interface RemoveProjectPatternMutationPayload {
44+
/**
45+
* Id of the pattern to be removed
46+
*/
4447
id: string;
4548

49+
/**
50+
* Id of the project to remove pattern
51+
*/
4652
projectId: string;
4753
}
4854

55+
/**
56+
* Validates a new event grouping pattern against existing patterns
57+
*
58+
* @param newEventGroupingPattern - The new pattern to validate
59+
* @param existingEventGroupingPatternList - List of existing patterns to check against
60+
* @throws Error if pattern is invalid or collides with existing patterns
61+
*/
62+
function validateNewEventGroupingPattern(
63+
newEventGroupingPattern: string
64+
): void {
65+
/**
66+
* Check if pattern is valid RegExp
67+
*/
68+
try {
69+
new RegExp(newEventGroupingPattern);
70+
} catch (error) {
71+
throw new ApolloError('Invalid regular expression pattern');
72+
}
73+
}
74+
75+
4976
export default {
5077
Mutation: {
5178
/**
@@ -66,13 +93,7 @@ export default {
6693
throw new ApolloError('No project with such id');
6794
}
6895

69-
const existingPatterns = await project.getProjectPatterns();
70-
71-
existingPatterns.forEach(pattern => {
72-
if (pattern.pattern.match(new RegExp(input.pattern)) || input.pattern.match(new RegExp(pattern.pattern))) {
73-
throw new ApolloError('New pattern collides with existing one');
74-
}
75-
});
96+
validateNewEventGroupingPattern(input.pattern);
7697

7798
return project.createProjectEventGroupingPattern({ pattern: input.pattern });
7899
},
@@ -95,15 +116,7 @@ export default {
95116
throw new ApolloError('No project with such id');
96117
}
97118

98-
const existingPatterns = await project.getProjectPatterns();
99-
100-
existingPatterns.forEach(pattern => {
101-
if (pattern._id.toString() !== input.id) {
102-
if (pattern.pattern.match(new RegExp(input.pattern)) || input.pattern.match(new RegExp(pattern.pattern))) {
103-
throw new ApolloError('New pattern collides with existing one');
104-
}
105-
}
106-
});
119+
validateNewEventGroupingPattern(input.pattern);
107120

108121
return project.updateProjectEventGroupingPattern(input);
109122
},

0 commit comments

Comments
 (0)