Skip to content

Commit 3c55d2f

Browse files
author
Parth Shah
committed
Merge branch 'release/v1.3.0'
2 parents 9b3e597 + 595bd9b commit 3c55d2f

File tree

11 files changed

+131
-10
lines changed

11 files changed

+131
-10
lines changed

config/custom-environment-variables.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
"masterUrl": "DB_MASTER_URL",
2929
"maxPoolSize": "DB_MAX_POOL_SIZE",
3030
"minPoolSize": "DB_MIN_POOL_SIZE"
31-
}
31+
},
32+
"analyticsKey": "ANALYTICS_KEY"
3233
}

config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
"maxPoolSize": 50,
3434
"minPoolSize": 4,
3535
"idleTimeout": 1000
36-
}
36+
},
37+
"analyticsKey": ""
3738
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tc-projects-service",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "Projects microservice",
55
"main": "index.js",
66
"engines": {
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"amqplib": "^0.5.1",
51+
"analytics-node": "^2.1.1",
5152
"app-module-path": "^1.0.7",
5253
"bluebird": "^3.4.1",
5354
"body-parser": "^1.15.0",

src/app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ app.logger = logger
6060
// =======================
6161
logger.info('Registering models ... ', !!models)
6262

63+
// =======================
64+
// Analytics
65+
// =======================
66+
const analyticsKey = config.get('analyticsKey')
67+
if (!_.isEmpty(analyticsKey))
68+
require('./events/analytics')(analyticsKey, app, logger)
6369

6470
// ========================
6571
// Permissions
@@ -79,4 +85,6 @@ app.routerRef = router
7985
// =======================
8086
require('./services')(app, logger)
8187

88+
89+
8290
module.exports = app

src/constants.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import _ from 'lodash'
32
export const PROJECT_TYPE = {
43
APP_DEV : 'app_dev',
54
GENERIC : 'generic',
@@ -37,10 +36,7 @@ export const EVENT = {
3736
PROJECT_MEMBER_UPDATED: 'project.member.updated',
3837

3938
PROJECT_DRAFT_CREATED: 'project.draft-created',
40-
PROJECT_LAUNCHED: 'project.launched',
4139
PROJECT_UPDATED: 'project.updated',
42-
PROJECT_CANCELLED: 'project.cancelled',
43-
PROJECT_COMPLETED: 'project.completed',
4440
PROJECT_DELETED: 'project.deleted'
4541
}
4642
}

src/events/analytics.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import Analytics from 'analytics-node'
2+
import {
3+
EVENT,
4+
PROJECT_STATUS
5+
} from '../constants'
6+
import 'config'
7+
import _ from 'lodash'
8+
9+
const PROJECT_CREATED = 'Project Created'
10+
const PROJECT_SUBMITTED = 'Project Submitted'
11+
const PROJECT_REVIEWED = 'Project Reviewed'
12+
const PROJECT_ACTIVATED = 'Project Acvtivated'
13+
const PROJECT_COMPLETED = 'Project Completed'
14+
const PROJECT_CANCELED = 'Project Canceled'
15+
const PROJECT_DELETED = 'Project Deleted'
16+
const PROJECT_PAUSED = 'Project Paused'
17+
const PROJECT_SPEC_UPDATED = 'Project Spec Updated'
18+
const PROJECT_MEMBER_ADDED = 'Project Member Added'
19+
const PROJECT_MEMBER_REMOVED = 'Project Member Removed'
20+
21+
const mapEventTypes = {
22+
[PROJECT_STATUS.DRAFT]: PROJECT_CREATED,
23+
[PROJECT_STATUS.IN_REVIEW]: PROJECT_SUBMITTED,
24+
[PROJECT_STATUS.REVIEWED]: PROJECT_REVIEWED,
25+
[PROJECT_STATUS.ACTIVE]: PROJECT_ACTIVATED,
26+
[PROJECT_STATUS.COMPLETED]: PROJECT_COMPLETED,
27+
[PROJECT_STATUS.CANCELLED]: PROJECT_CANCELED,
28+
[PROJECT_STATUS.PAUSED]: PROJECT_PAUSED
29+
}
30+
31+
module.exports = (analyticsKey, app, logger) => {
32+
33+
// Initialize analytics
34+
const analytics = analyticsKey ? new Analytics(analyticsKey) : null
35+
36+
/**
37+
* Invokes analytics track and attaches additional props
38+
* @param {Object} req Http request
39+
* @param {String} eventType analytics event
40+
* @param {Object} props properties to track
41+
*/
42+
const track = (req, eventType, props) => {
43+
if (analytics) {
44+
const userId = _.get(req, 'authUser.userId', 'system').toString()
45+
// add referrer ?
46+
const _props = _.assign({}, { referrer: req.get('Referer') }, props)
47+
const data = {
48+
userId,
49+
event: eventType,
50+
properties: _props
51+
}
52+
req.log.debug('Analytics tracking:', data)
53+
analytics.track(data)
54+
}
55+
56+
}
57+
58+
app.on(EVENT.ROUTING_KEY.PROJECT_DRAFT_CREATED, ({req, project}) => {
59+
track(req, PROJECT_CREATED, {
60+
id: project.id,
61+
name: project.name,
62+
type: project.type,
63+
products: _.get(project, 'products', [])
64+
})
65+
})
66+
67+
app.on(EVENT.ROUTING_KEY.PROJECT_DELETED, ({req, id}) => {
68+
track(req, PROJECT_CREATED, { id })
69+
})
70+
71+
app.on(EVENT.ROUTING_KEY.PROJECT_UPDATED, ({req, original, updated }) => {
72+
// determine what changed
73+
let eventType
74+
if (original.status !== updated.status) {
75+
// status was changed
76+
eventType = mapEventTypes[updated.status]
77+
} else if (!_.isEqual(original.details, updated.details)) {
78+
// specifications updated
79+
eventType = PROJECT_SPEC_UPDATED
80+
}
81+
if (eventType) {
82+
track(req, eventType, {
83+
id: updated.id,
84+
name: updated.name,
85+
type: updated.type,
86+
products: _.get(updated, 'products', [])
87+
})
88+
}
89+
90+
})
91+
92+
93+
app.on(EVENT.ROUTING_KEY.PROJECT_MEMBER_ADDED, ({req, member}) => {
94+
track(req, PROJECT_MEMBER_ADDED, { role: member.role })
95+
})
96+
97+
app.on(EVENT.ROUTING_KEY.PROJECT_MEMBER_REMOVED, ({req, member}) => {
98+
track(req, PROJECT_MEMBER_REMOVED, { role: member.role })
99+
})
100+
101+
}

src/routes/projectMembers/create.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module.exports = [
6969
newMember,
7070
{ correlationId: req.id }
7171
)
72+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_MEMBER_ADDED, { req, member: newMember })
7273
res.status(201).json(util.wrapResponse(req.id, newMember, 1, 201))
7374
})
7475
.catch((err) => {

src/routes/projectMembers/delete.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ module.exports = [
3636
})
3737
.then(member => {
3838
// fire event
39+
member = member.get({plain:true})
3940
req.app.services.pubsub.publish(
4041
EVENT.ROUTING_KEY.PROJECT_MEMBER_REMOVED,
41-
member.get({plain:true}),
42+
member,
4243
{ correlationId: req.id }
4344
)
45+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_MEMBER_REMOVED, { req, member })
4446
res.status(204).json({})
4547
})
4648
.catch(err => next(err))

src/routes/projects/create.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = [
6161
* POST projects/
6262
* Create a project if the user has access
6363
*/
64-
(req, res, next) => {
64+
(req, res, next) => {
6565
var project = req.body.param
6666
const userRole = util.hasRole(req, USER_ROLE.MANAGER)
6767
? PROJECT_MEMBER_ROLE.MANAGER
@@ -122,6 +122,7 @@ module.exports = [
122122
// log the error and continue
123123
req.log.error('Error creating direct project')
124124
req.log.error(err)
125+
return Promise.resolve()
125126
})
126127
})
127128
.then(() => {
@@ -134,6 +135,8 @@ module.exports = [
134135
newProject,
135136
{ correlationId: req.id }
136137
)
138+
// emit event
139+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_DRAFT_CREATED, {req, project: newProject })
137140
res.status(201).json(util.wrapResponse(req.id, newProject, 1, 201))
138141
})
139142
.catch((err) => {

src/routes/projects/delete.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module.exports = [
3737
{ id: projectId },
3838
{ correlationId: req.id }
3939
)
40+
// emit event
41+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_DELETED, {req, id: projectId })
4042
res.status(204).json({})
4143
}
4244
})

src/routes/projects/update.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,15 @@ module.exports = [
165165
previousValue = _.omit(previousValue, ['deletedAt'])
166166
// publish original and updated project data
167167
req.app.services.pubsub.publish(
168-
EVENT.ROUTING_KEY.PROJECT_UPDATED,
168+
EVENT.ROUTING_KEY.PROJECT_UPDATED,
169169
{ original: previousValue, updated: project },
170170
{ correlationId: req.id }
171171
)
172+
req.app.emit(EVENT.ROUTING_KEY.PROJECT_UPDATED, {
173+
req,
174+
original: previousValue,
175+
updated: project
176+
})
172177
// check context for project members
173178
project.members = req.context.currentProjectMembers
174179
// get attachments

0 commit comments

Comments
 (0)