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