44const _ = require ( 'lodash' ) ;
55const jwt = require ( 'jsonwebtoken' ) ;
66const co = require ( 'co' ) ;
7- const fs = require ( 'fs' ) ;
8- const path = require ( 'path' ) ;
97const { logger, busService, eventScheduler, notificationService } = require ( '../../index' ) ;
108const { createEventScheduler, SCHEDULED_EVENT_STATUS } = eventScheduler ;
119
@@ -16,29 +14,43 @@ const {
1614 SETTINGS_EMAIL_SERVICE_ID ,
1715 ACTIVE_USER_STATUSES ,
1816} = require ( '../constants' ) ;
19- const { EVENTS , EVENT_BUNDLES } = require ( '../events-config' ) ;
17+ const { EVENT_BUNDLES } = require ( '../events-config' ) ;
2018const helpers = require ( '../helpers' ) ;
2119const service = require ( '../service' ) ;
2220
2321
2422function replacePlaceholders ( term , data ) {
25- let placeholders = term . match ( / < [ a - z A - Z ] + > / g) ;
23+ const placeholders = term . match ( / < [ a - z A - Z ] + > / g) ;
2624 let ret = term ;
2725 if ( placeholders && placeholders . length ) {
2826 _ ( 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';
3437 ret = ret . replace ( p , values . join ( ', ' ) ) ;
3538 } ) ;
3639 }
3740 return ret ;
3841}
3942
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+
4052function getSections ( projectUserEvents ) {
41- let sections = [ ] ;
53+ const sections = [ ] ;
4254 _ . chain ( projectUserEvents )
4355 . groupBy ( value => getEventGroupKey ( value ) )
4456 . forIn ( ( value , key ) => {
@@ -49,7 +61,7 @@ function getSections(projectUserEvents) {
4961 notifications : _ ( value ) . map ( v => v . data . data ) . value ( ) ,
5062 } ) ;
5163 } 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 ) => {
5365 let title = EVENT_BUNDLES [ key ] . title ;
5466 title = replacePlaceholders ( title , _ ( groupValue ) . map ( g => g . data . data ) . value ( ) ) ;
5567 sections . push ( {
@@ -140,15 +152,6 @@ const scheduler = createEventScheduler(
140152 handleScheduledEvents
141153) ;
142154
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-
152155/**
153156 * Prepares data to be provided to the template to render a single notification.
154157 *
@@ -245,30 +248,31 @@ function handler(topicName, messageJSON, notification) {
245248 } ;
246249 eventMessage . data [ eventMessage . data . type ] = true ;
247250 _ . assign ( eventMessage . data , notification . contents ) ;
248-
251+
249252 // message service may return tags
250253 // 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 ) ;
255256 if ( phaseId ) {
256257 eventMessage . data . phaseId = phaseId ;
257258 }
258259
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
260261 // we build a link to the post
261262 if ( eventMessage . data . topicId ) {
262263 // phase post
263264 if ( eventMessage . data . phaseId ) {
265+ // eslint-disable-next-line max-len
264266 eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } /plan#phase-${ eventMessage . data . phaseId } -posts-${ eventMessage . data . postId } ` ;
265267
266268 // dashboard post
267269 } else if ( eventMessage . data . postId ) {
270+ // eslint-disable-next-line max-len
268271 eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } #comment-${ eventMessage . data . postId } ` ;
269272
270273 // dashboard topic
271274 } else {
275+ // eslint-disable-next-line max-len
272276 eventMessage . data . postURL = `${ config . CONNECT_URL } /projects/${ eventMessage . data . projectId } #feed-${ eventMessage . data . topicId } ` ;
273277 }
274278 }
@@ -319,19 +323,21 @@ function handler(topicName, messageJSON, notification) {
319323 }
320324
321325 // 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` ) ;
323328 bundlePeriod = bundlePeriod && bundlePeriod . trim ( ) . length > 0 ? bundlePeriod : null ;
324329 // if bundling is not explicitly set and the event is not a messaging event, assume bundling enabled
325330 if ( ! bundlePeriod && ! messagingEvent ) {
326331 // 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 ) ;
328333 if ( eventBundleCategory ) {
329334 const eventBundle = EVENT_BUNDLES [ eventBundleCategory ] ;
330335 // if we find the event category for the notification, use the bundle settings from the first event
331336 if ( eventBundle && eventBundle . types && eventBundle . types . length ) {
332337 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 ) ;
335341 bundlePeriod = firstEvtBundlePeriod ;
336342 logger . debug ( 'Assuming bundle period of first event in the event category=>' , bundlePeriod ) ;
337343 }
@@ -341,7 +347,7 @@ function handler(topicName, messageJSON, notification) {
341347 }
342348 logger . debug ( 'bundlePeriod=>' , bundlePeriod ) ;
343349
344- if ( bundlePeriod && 'immediately' !== bundlePeriod && ! requiresImmediateAttention ) {
350+ if ( bundlePeriod && bundlePeriod !== 'immediately' && ! requiresImmediateAttention ) {
345351 if ( ! SCHEDULED_EVENT_PERIOD [ bundlePeriod ] ) {
346352 throw new Error ( `User's '${ notification . userId } ' setting for service`
347353 + ` '${ SETTINGS_EMAIL_SERVICE_ID } ' option 'bundlePeriod' has unsupported value '${ bundlePeriod } '.` ) ;
@@ -359,7 +365,7 @@ function handler(topicName, messageJSON, notification) {
359365 } else {
360366 // send single field "notificationsHTML" with the rendered template
361367 eventMessage . data = wrapIndividualNotification ( { data : eventMessage } ) ;
362- //console.log(eventMessage.data.contents);
368+ // console.log(eventMessage.data.contents);
363369
364370 // send event to bus api
365371 return busService . postEvent ( {
@@ -374,7 +380,7 @@ function handler(topicName, messageJSON, notification) {
374380 . catch ( ( err ) => {
375381 logger . error ( `Failed to send ${ eventType } event`
376382 + `; error: ${ err . message } `
377- + `; with body ${ JSON . stringify ( eventMessage ) } to bus api` ) ;
383+ + `; with body ${ JSON . stringify ( eventMessage ) } to bus api` ) ;
378384 } ) ;
379385 }
380386 } ) ;
0 commit comments