4
4
const _ = require ( 'lodash' ) ;
5
5
const jwt = require ( 'jsonwebtoken' ) ;
6
6
const co = require ( 'co' ) ;
7
- const fs = require ( 'fs' ) ;
8
- const path = require ( 'path' ) ;
9
7
const { logger, busService, eventScheduler, notificationService } = require ( '../../index' ) ;
10
8
const { createEventScheduler, SCHEDULED_EVENT_STATUS } = eventScheduler ;
11
9
@@ -16,29 +14,43 @@ const {
16
14
SETTINGS_EMAIL_SERVICE_ID ,
17
15
ACTIVE_USER_STATUSES ,
18
16
} = require ( '../constants' ) ;
19
- const { EVENTS , EVENT_BUNDLES } = require ( '../events-config' ) ;
17
+ const { EVENT_BUNDLES } = require ( '../events-config' ) ;
20
18
const helpers = require ( '../helpers' ) ;
21
19
const service = require ( '../service' ) ;
22
20
23
21
24
22
function replacePlaceholders ( term , data ) {
25
- let placeholders = term . match ( / < [ a - z A - Z ] + > / g) ;
23
+ const placeholders = term . match ( / < [ a - z A - Z ] + > / g) ;
26
24
let ret = term ;
27
25
if ( placeholders && placeholders . length ) {
28
26
_ ( placeholders ) . each ( p => {
29
- let values = _ . map ( data , p . slice ( 1 , - 1 ) ) ;
30
- const total = values . length ;
31
- let replacement = values . length < 3 ?
32
- values . join ( ', ' ) :
33
- values . slice ( 0 , 2 ) . join ( ', ' ) + ' and ' + ( total - 3 ) + 'others' ;
27
+ const values = _ . map ( data , p . slice ( 1 , - 1 ) ) ;
28
+ // TODO remove this code if possible.
29
+ // This code appears to be not in use causing lint errors.
30
+ // For now I'm commenting it, in case it contains some valuable logic.
31
+ // But after confirmation that it's redundant it has to be removed.
32
+ //
33
+ // const total = values.length;
34
+ // const replacement = values.length < 3 ?
35
+ // values.join(', ') :
36
+ // values.slice(0, 2).join(', ') + ' and ' + (total - 3) + 'others';
34
37
ret = ret . replace ( p , values . join ( ', ' ) ) ;
35
38
} ) ;
36
39
}
37
40
return ret ;
38
41
}
39
42
43
+ function getEventGroupKey ( value ) {
44
+ const key = _ . chain ( EVENT_BUNDLES )
45
+ . keys ( )
46
+ . find ( k => _ . includes ( _ . get ( EVENT_BUNDLES , `${ k } .types` ) , _ . get ( value , 'data.data.type' ) ) )
47
+ . value ( ) ;
48
+ if ( ! key ) return 'DEFAULT' ;
49
+ return key ;
50
+ }
51
+
40
52
function getSections ( projectUserEvents ) {
41
- let sections = [ ] ;
53
+ const sections = [ ] ;
42
54
_ . chain ( projectUserEvents )
43
55
. groupBy ( value => getEventGroupKey ( value ) )
44
56
. forIn ( ( value , key ) => {
@@ -49,7 +61,7 @@ function getSections(projectUserEvents) {
49
61
notifications : _ ( value ) . map ( v => v . data . data ) . value ( ) ,
50
62
} ) ;
51
63
} else {
52
- _ . chain ( value ) . groupBy ( n => n . data . data [ EVENT_BUNDLES [ key ] . groupBy ] ) . forIn ( ( groupValue , groupKey ) => {
64
+ _ . chain ( value ) . groupBy ( n => n . data . data [ EVENT_BUNDLES [ key ] . groupBy ] ) . forIn ( ( groupValue ) => {
53
65
let title = EVENT_BUNDLES [ key ] . title ;
54
66
title = replacePlaceholders ( title , _ ( groupValue ) . map ( g => g . data . data ) . value ( ) ) ;
55
67
sections . push ( {
@@ -140,15 +152,6 @@ const scheduler = createEventScheduler(
140
152
handleScheduledEvents
141
153
) ;
142
154
143
- function getEventGroupKey ( value ) {
144
- let key = _ . chain ( EVENT_BUNDLES )
145
- . keys ( )
146
- . find ( key => _ . includes ( _ . get ( EVENT_BUNDLES , `${ key } .types` ) , _ . get ( value , 'data.data.type' ) ) )
147
- . value ( ) ;
148
- if ( ! key ) return 'DEFAULT' ;
149
- return key ;
150
- }
151
-
152
155
/**
153
156
* Prepares data to be provided to the template to render a single notification.
154
157
*
@@ -245,30 +248,31 @@ function handler(topicName, messageJSON, notification) {
245
248
} ;
246
249
eventMessage . data [ eventMessage . data . type ] = true ;
247
250
_ . assign ( eventMessage . data , notification . contents ) ;
248
-
251
+
249
252
// message service may return tags
250
253
// to understand if post notification is regarding phases or no, we will try to get phaseId from the tags
251
- const tags = _ . get ( notification . contents , 'tags' , [ ] )
252
- const PHASE_ID_REGEXP = / p h a s e # ( \d + ) /
253
- const phaseIds = tags . map ( ( tag ) => _ . get ( tag . match ( PHASE_ID_REGEXP ) , '1' , null ) )
254
- const phaseId = _ . find ( phaseIds , ( phaseId ) => phaseId !== null )
254
+ const tags = _ . get ( notification . contents , 'tags' , [ ] ) ;
255
+ const phaseId = helpers . extractPhaseId ( tags ) ;
255
256
if ( phaseId ) {
256
257
eventMessage . data . phaseId = phaseId ;
257
258
}
258
259
259
- // if the notification is regarding topic: dashboard topic, dashboard post or phase post
260
+ // if the notification is regarding topic: dashboard topic, dashboard post or phase post
260
261
// we build a link to the post
261
262
if ( eventMessage . data . topicId ) {
262
263
// phase post
263
264
if ( eventMessage . data . phaseId ) {
265
+ // eslint-disable-next-line max-len
264
266
eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } /plan#phase-${ eventMessage . data . phaseId } -posts-${ eventMessage . data . postId } ` ;
265
267
266
268
// dashboard post
267
269
} else if ( eventMessage . data . postId ) {
270
+ // eslint-disable-next-line max-len
268
271
eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } #comment-${ eventMessage . data . postId } ` ;
269
272
270
273
// dashboard topic
271
274
} else {
275
+ // eslint-disable-next-line max-len
272
276
eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } #feed-${ eventMessage . data . topicId } ` ;
273
277
}
274
278
}
@@ -319,19 +323,21 @@ function handler(topicName, messageJSON, notification) {
319
323
}
320
324
321
325
// if notifications has to be bundled
322
- let bundlePeriod = _ . get ( settings , `notifications['${ notificationType } '].${ SETTINGS_EMAIL_SERVICE_ID } .bundlePeriod` ) ;
326
+ let bundlePeriod = _ . get ( settings ,
327
+ `notifications['${ notificationType } '].${ SETTINGS_EMAIL_SERVICE_ID } .bundlePeriod` ) ;
323
328
bundlePeriod = bundlePeriod && bundlePeriod . trim ( ) . length > 0 ? bundlePeriod : null ;
324
329
// if bundling is not explicitly set and the event is not a messaging event, assume bundling enabled
325
330
if ( ! bundlePeriod && ! messagingEvent ) {
326
331
// finds the event category for the notification type
327
- let eventBundleCategory = _ . findKey ( EVENT_BUNDLES , b => b . types && b . types . indexOf ( notificationType ) !== - 1 ) ;
332
+ const eventBundleCategory = _ . findKey ( EVENT_BUNDLES , b => b . types && b . types . indexOf ( notificationType ) !== - 1 ) ;
328
333
if ( eventBundleCategory ) {
329
334
const eventBundle = EVENT_BUNDLES [ eventBundleCategory ] ;
330
335
// if we find the event category for the notification, use the bundle settings from the first event
331
336
if ( eventBundle && eventBundle . types && eventBundle . types . length ) {
332
337
const firstEvtInBundle = eventBundle . types [ 0 ] ;
333
- const firstEvtBundleSettingPath = `notifications['${ firstEvtInBundle } '].${ SETTINGS_EMAIL_SERVICE_ID } .bundlePeriod` ;
334
- let firstEvtBundlePeriod = _ . get ( settings , firstEvtBundleSettingPath ) ;
338
+ const firstEvtBundleSettingPath =
339
+ `notifications['${ firstEvtInBundle } '].${ SETTINGS_EMAIL_SERVICE_ID } .bundlePeriod` ;
340
+ const firstEvtBundlePeriod = _ . get ( settings , firstEvtBundleSettingPath ) ;
335
341
bundlePeriod = firstEvtBundlePeriod ;
336
342
logger . debug ( 'Assuming bundle period of first event in the event category=>' , bundlePeriod ) ;
337
343
}
@@ -341,7 +347,7 @@ function handler(topicName, messageJSON, notification) {
341
347
}
342
348
logger . debug ( 'bundlePeriod=>' , bundlePeriod ) ;
343
349
344
- if ( bundlePeriod && 'immediately' !== bundlePeriod && ! requiresImmediateAttention ) {
350
+ if ( bundlePeriod && bundlePeriod !== 'immediately' && ! requiresImmediateAttention ) {
345
351
if ( ! SCHEDULED_EVENT_PERIOD [ bundlePeriod ] ) {
346
352
throw new Error ( `User's '${ notification . userId } ' setting for service`
347
353
+ ` '${ SETTINGS_EMAIL_SERVICE_ID } ' option 'bundlePeriod' has unsupported value '${ bundlePeriod } '.` ) ;
@@ -359,7 +365,7 @@ function handler(topicName, messageJSON, notification) {
359
365
} else {
360
366
// send single field "notificationsHTML" with the rendered template
361
367
eventMessage . data = wrapIndividualNotification ( { data : eventMessage } ) ;
362
- //console.log(eventMessage.data.contents);
368
+ // console.log(eventMessage.data.contents);
363
369
364
370
// send event to bus api
365
371
return busService . postEvent ( {
@@ -374,7 +380,7 @@ function handler(topicName, messageJSON, notification) {
374
380
. catch ( ( err ) => {
375
381
logger . error ( `Failed to send ${ eventType } event`
376
382
+ `; error: ${ err . message } `
377
- + `; with body ${ JSON . stringify ( eventMessage ) } to bus api` ) ;
383
+ + `; with body ${ JSON . stringify ( eventMessage ) } to bus api` ) ;
378
384
} ) ;
379
385
}
380
386
} ) ;
0 commit comments