Skip to content

Commit 4794e6d

Browse files
committed
feat(resolvers): add project patterns resolvers
1 parent 5aed97e commit 4794e6d

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

src/resolvers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const project = require('./project');
1313
const event = require('./event');
1414
const plans = require('./plans').default;
1515
const projectNotifications = require('./projectNotifications').default;
16+
const projectPatterns = require('./projectPatterns').default;
1617
const userNotifications = require('./userNotifications').default;
1718
const billing = require('./billingNew').default;
1819
const EncodedJSON = require('./encodedJSON').default;
@@ -71,6 +72,7 @@ const resolvers = [
7172
project,
7273
event,
7374
projectNotifications,
75+
projectPatterns,
7476
userNotifications,
7577
plans,
7678
billing,

src/resolvers/projectNotifications.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
ProjectNotificationsRuleDBScheme,
32
ReceiveTypes
43
} from '../models/project';
4+
import { ProjectNotificationsRuleDBScheme } from '@hawk.so/types';
55
import { ResolverContextWithUser } from '../types/graphql';
66
import { ApolloError, UserInputError } from 'apollo-server-express';
7-
import { NotificationsChannelsDBScheme, NotificationsChannelSettingsDBScheme } from '../types/notification-channels';
7+
import { NotificationsChannelsDBScheme } from '../types/notification-channels';
88

99
/**
1010
* Mutation payload for creating notifications rule from GraphQL Schema
@@ -223,7 +223,7 @@ export default {
223223
},
224224

225225
/**
226-
* Toggles isEnabled field in in project notifications rule
226+
* Toggles isEnabled field in project notifications rule
227227
* @param _obj - parent object
228228
* @param user - current authorized user {@see ../index.js}
229229
* @param factories - factories for working with models

src/resolvers/projectPatterns.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { ResolverContextWithUser } from "../types/graphql";
2+
import { ApolloError } from 'apollo-server-express';
3+
import { ProjectEventGroupingPatternsDBScheme } from "@hawk.so/types";
4+
5+
/**
6+
* Type that represents payload for create project pattern mutation
7+
*/
8+
interface CreateProjectPatternMutationPayload {
9+
/**
10+
* Id of the project to create new pattern
11+
*/
12+
projectId: string;
13+
14+
/**
15+
* New pattern to be inserted
16+
*/
17+
pattern: string;
18+
};
19+
20+
/**
21+
* Type that represents payload for update project pattern mutation
22+
*/
23+
interface UpdateProjectPatternMutationPayload {
24+
/**
25+
* Id of the pattern to be updated
26+
*/
27+
id: string,
28+
29+
/**
30+
* ProjectId of the pattern to be updated
31+
*/
32+
projectId: string,
33+
34+
/**
35+
* New pattern
36+
*/
37+
pattern: string,
38+
};
39+
40+
/**
41+
* Type that represents payload for remove project pattern mutation
42+
*/
43+
interface RemoveProjectPatternMutationPayload {
44+
id: string,
45+
46+
projectId: string,
47+
}
48+
49+
export default {
50+
Mutation: {
51+
/**
52+
* Creates new events grouping pattern
53+
* @param _obj - parent object
54+
* @param user - current authorized user {@see ../index.js}
55+
* @param factories - factories for working with models
56+
* @param input - input data for creating
57+
*/
58+
async createProjectEventGroupingPattern(
59+
_obj: undefined,
60+
{ input }: { input: CreateProjectPatternMutationPayload },
61+
{ user, factories }: ResolverContextWithUser
62+
): Promise<ProjectEventGroupingPatternsDBScheme> {
63+
const project = await factories.projectsFactory.findById(input.projectId);
64+
65+
if (!project) {
66+
throw new ApolloError('No project with such id');
67+
}
68+
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+
})
76+
77+
return await project.createProjectEventGroupingPattern({ pattern: input.pattern });
78+
},
79+
80+
/**
81+
* Updates one events grouping pattern
82+
* @param _obj - parent object
83+
* @param user - current authorized user {@see ../index.js}
84+
* @param factories - factories for working with models
85+
* @param input - input data for creating
86+
*/
87+
async updateProjectEventGroupingPattern(
88+
_obj: undefined,
89+
{ input }: { input: UpdateProjectPatternMutationPayload },
90+
{ user, factories }: ResolverContextWithUser
91+
): Promise<ProjectEventGroupingPatternsDBScheme> {
92+
const project = await factories.projectsFactory.findById(input.projectId);
93+
94+
if (!project) {
95+
throw new ApolloError('No project with such id');
96+
}
97+
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+
});
107+
108+
return await project.updateProjectEventGroupingPattern(input);
109+
},
110+
111+
/**
112+
* Updates one events grouping pattern
113+
* @param _obj - parent object
114+
* @param user - current authorized user {@see ../index.js}
115+
* @param factories - factories for working with models
116+
* @param input - input data for creating
117+
*/
118+
async removeProjectEventGroupingPattern(
119+
obj: undefined,
120+
{ input }: { input: RemoveProjectPatternMutationPayload },
121+
{ user, factories }: ResolverContextWithUser
122+
): Promise<ProjectEventGroupingPatternsDBScheme> {
123+
const project = await factories.projectsFactory.findById(input.projectId);
124+
125+
if (!project) {
126+
throw new ApolloError('No project with such id');
127+
}
128+
129+
return await project.removeProjectEventGroupingPattern({ id: input.id });
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)