@@ -39,6 +39,16 @@ interface CreateProjectNotificationsRuleMutationPayload {
3939 * Available channels to receive
4040 */
4141 channels : NotificationsChannelsDBScheme ;
42+
43+ /**
44+ * Threshold to receive notification
45+ */
46+ threshold : number ;
47+
48+ /**
49+ * Period to receive notification
50+ */
51+ thresholdPeriod : number ;
4252}
4353
4454/**
@@ -67,16 +77,50 @@ interface ProjectNotificationsRulePointer {
6777}
6878
6979/**
70- * Return true if all passed channels are empty
71- * @param channels - project notifications channels
80+ * Returns true is threshold and threshold period are valid
81+ * @param threshold - threshold of the notification rule to be checked
82+ * @param thresholdPeriod - threshold period of the notification rule to be checked
7283 */
73- function isChannelsEmpty ( channels : NotificationsChannelsDBScheme ) : boolean {
74- const notEmptyChannels = Object . entries ( channels )
75- . filter ( ( [ _ , channel ] ) => {
76- return ( channel as NotificationsChannelSettingsDBScheme ) . endpoint . replace ( / \s + / , '' ) . trim ( ) . length !== 0 ;
77- } ) ;
84+ function validateNotificationsRuleTresholdAndPeriod (
85+ threshold : ProjectNotificationsRuleDBScheme [ 'threshold' ] ,
86+ thresholdPeriod : ProjectNotificationsRuleDBScheme [ 'thresholdPeriod' ]
87+ ) : string | null {
88+ const validThresholdPeriods = [ 60_000 , 3_600_000 , 86_400_000 , 604_800_000 ] ;
89+
90+ if ( thresholdPeriod === undefined || ! validThresholdPeriods . includes ( thresholdPeriod ) ) {
91+ return 'Threshold period should be one of the following: 60000, 3600000, 86400000, 604800000' ;
92+ }
93+
94+ if ( threshold === undefined || threshold < 1 ) {
95+ return 'Threshold should be greater than 0' ;
96+ }
97+
98+ return null ;
99+ }
78100
79- return notEmptyChannels . length === 0 ;
101+ /**
102+ * Return true if all passed channels are filled with correct endpoints
103+ */
104+ function validateNotificationsRuleChannels ( channels : NotificationsChannelsDBScheme ) : string | null {
105+ if ( channels . email ! . isEnabled ) {
106+ if ( ! / ^ [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , } $ / . test ( channels . email ! . endpoint ) ) {
107+ return 'Invalid email endpoint passed' ;
108+ }
109+ }
110+
111+ if ( channels . slack ! . isEnabled ) {
112+ if ( ! / ^ h t t p s : \/ \/ h o o k s \. s l a c k \. c o m \/ s e r v i c e s \/ [ A - Z a - z 0 - 9 ] + \/ [ A - Z a - z 0 - 9 ] + \/ [ A - Z a - z 0 - 9 ] + $ / . test ( channels . slack ! . endpoint ) ) {
113+ return 'Invalid slack endpoint passed' ;
114+ }
115+ }
116+
117+ if ( channels . telegram ! . isEnabled ) {
118+ if ( ! / ^ h t t p s : \/ \/ n o t i f y \. b o t \. c o d e x \. s o \/ u \/ [ A - Z a - z 0 - 9 ] + $ / . test ( channels . telegram ! . endpoint ) ) {
119+ return 'Invalid telegram endpoint passed' ;
120+ }
121+ }
122+
123+ return null ;
80124}
81125
82126/**
@@ -102,8 +146,18 @@ export default {
102146 throw new ApolloError ( 'No project with such id' ) ;
103147 }
104148
105- if ( isChannelsEmpty ( input . channels ) ) {
106- throw new UserInputError ( 'At least one channel is required' ) ;
149+ const channelsValidationResult = validateNotificationsRuleChannels ( input . channels ) ;
150+
151+ if ( channelsValidationResult !== null ) {
152+ throw new UserInputError ( channelsValidationResult ) ;
153+ }
154+
155+ if ( input . whatToReceive === ReceiveTypes . SEEN_MORE ) {
156+ const thresholdValidationResult = validateNotificationsRuleTresholdAndPeriod ( input . threshold , input . thresholdPeriod ) ;
157+
158+ if ( thresholdValidationResult !== null ) {
159+ throw new UserInputError ( thresholdValidationResult ) ;
160+ }
107161 }
108162
109163 return project . createNotificationsRule ( {
@@ -130,8 +184,18 @@ export default {
130184 throw new ApolloError ( 'No project with such id' ) ;
131185 }
132186
133- if ( isChannelsEmpty ( input . channels ) ) {
134- throw new UserInputError ( 'At least one channel is required' ) ;
187+ const channelsValidationResult = validateNotificationsRuleChannels ( input . channels ) ;
188+
189+ if ( channelsValidationResult !== null ) {
190+ throw new UserInputError ( channelsValidationResult ) ;
191+ }
192+
193+ if ( input . whatToReceive === ReceiveTypes . SEEN_MORE ) {
194+ const thresholdValidationResult = validateNotificationsRuleTresholdAndPeriod ( input . threshold , input . thresholdPeriod ) ;
195+
196+ if ( thresholdValidationResult !== null ) {
197+ throw new UserInputError ( thresholdValidationResult ) ;
198+ }
135199 }
136200
137201 return project . updateNotificationsRule ( input ) ;
0 commit comments