|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | import _ from 'lodash'
|
4 |
| -import { EVENT, PROJECT_MEMBER_ROLE } from '../../constants' |
| 4 | +import { |
| 5 | + EVENT, |
| 6 | + PROJECT_MEMBER_ROLE |
| 7 | +} from '../../constants' |
5 | 8 | import util from '../../util'
|
6 | 9 | import models from '../../models'
|
7 | 10 | import directProject from '../../services/directProject'
|
8 | 11 |
|
9 | 12 |
|
10 |
| -module.exports = (app, logger) => { |
11 |
| - // Handle internal events |
12 |
| - const internalEvents = [ |
13 |
| - EVENT.INTERNAL.PROJECT_MEMBER_ADDED, |
14 |
| - EVENT.INTERNAL.PROJECT_MEMBER_REMOVED |
15 |
| - ] |
| 13 | +const projectMemberAddedHandler = (logger, msg, channel) => { |
| 14 | + const origRequestId = msg.properties.correlationId |
| 15 | + const newMember = JSON.parse(msg.content.toString()) |
16 | 16 |
|
17 |
| - // Publish messages to the queue |
18 |
| - _.map(internalEvents, (evt) => { |
19 |
| - app.on(evt, ({payload, props}) => { |
20 |
| - logger.debug('handling ', evt) |
21 |
| - let key = evt.substring(evt.indexOf('.') + 1) |
22 |
| - return app.services.pubsub.publish(key, payload, props) |
23 |
| - }) |
24 |
| - }) |
25 |
| - |
26 |
| - |
27 |
| - // EXTERNAL events |
28 |
| - app.on(EVENT.EXTERNAL.PROJECT_MEMBER_ADDED, (msg, next) => { |
29 |
| - const origRequestId = msg.properties.correlationId |
30 |
| - logger = logger.child({requestId: origRequestId}) |
31 |
| - let newMember = JSON.parse(msg.content.toString()) |
32 |
| - logger.debug(`received msg '${EVENT.EXTERNAL.PROJECT_MEMBER_ADDED}'`, newMember) |
33 |
| - |
34 |
| - if (newMember.role === PROJECT_MEMBER_ROLE.COPILOT) { |
35 |
| - // Add co-pilot when a co-pilot is added to a project |
36 |
| - return models.Project.getDirectProjectId(newMember.projectId) |
37 |
| - .then(directProjectId => { |
38 |
| - if (directProjectId) { |
39 |
| - // retrieve system user token |
40 |
| - return util.getSystemUserToken(logger) |
41 |
| - .then(token => { |
42 |
| - const req = { |
43 |
| - id: origRequestId, |
44 |
| - log: logger, |
45 |
| - headers: { authorization: `Bearer ${token}` } |
| 17 | + if (newMember.role === PROJECT_MEMBER_ROLE.COPILOT) { |
| 18 | + // Add co-pilot when a co-pilot is added to a project |
| 19 | + return models.Project.getDirectProjectId(newMember.projectId) |
| 20 | + .then(directProjectId => { |
| 21 | + if (directProjectId) { |
| 22 | + // retrieve system user token |
| 23 | + return util.getSystemUserToken(logger) |
| 24 | + .then(token => { |
| 25 | + const req = { |
| 26 | + id: origRequestId, |
| 27 | + log: logger, |
| 28 | + headers: { |
| 29 | + authorization: `Bearer ${token}` |
46 | 30 | }
|
47 |
| - return directProject.addCopilot(req, directProjectId, { |
| 31 | + } |
| 32 | + return directProject.addCopilot(req, directProjectId, { |
48 | 33 | copilotUserId: newMember.userId
|
49 | 34 | })
|
50 |
| - .then((resp) => { |
51 |
| - next() |
52 |
| - }) |
53 |
| - }) |
54 |
| - .catch(err => { |
55 |
| - logger.error('Error caught while adding co-pilot from direct', err) |
56 |
| - return next(err) |
57 |
| - }) |
58 |
| - } else { |
59 |
| - next() |
60 |
| - } |
61 |
| - }) |
62 |
| - .catch(err => next(err)) |
63 |
| - } else { |
64 |
| - // nothing to do |
65 |
| - next() |
66 |
| - } |
67 |
| - }) |
| 35 | + .then(resp => { |
| 36 | + logger.debug('added copilot to direct') |
| 37 | + // acknowledge |
| 38 | + channel.ack(msg) |
| 39 | + }) |
| 40 | + }) |
| 41 | + .catch(err => { |
| 42 | + logger.error('Error caught while adding co-pilot from direct', err) |
| 43 | + channel.nack(msg, false, false) |
| 44 | + }) |
| 45 | + } else { |
| 46 | + logger.info('project not associated with a direct project, skipping') |
| 47 | + ack(msg) |
| 48 | + } |
| 49 | + }) |
| 50 | + .catch(err => { |
| 51 | + // if the message has been redelivered dont attempt to reprocess it |
| 52 | + logger.error('Error retrieving project', err, msg) |
| 53 | + channel.nack(msg, false, !msg.fields.redelivered) |
| 54 | + }) |
| 55 | + } else { |
| 56 | + // nothing to do |
| 57 | + channel.ack(msg) |
| 58 | + } |
| 59 | +} |
68 | 60 |
|
69 |
| - app.on(EVENT.EXTERNAL.PROJECT_MEMBER_REMOVED, (msg, next) => { |
70 |
| - const origRequestId = msg.properties.correlationId |
71 |
| - const member = JSON.parse(msg.content.toString()) |
72 |
| - logger = logger.child({requestId: origRequestId}) |
73 |
| - logger.debug(`received msg '${EVENT.EXTERNAL.PROJECT_MEMBER_REMOVED}'`, member) |
| 61 | +const projectMemberRemovedHandler = (logger, msg, channel) => { |
| 62 | + const origRequestId = msg.properties.correlationId |
| 63 | + const member = JSON.parse(msg.content.toString()) |
74 | 64 |
|
75 |
| - if (member.role === PROJECT_MEMBER_ROLE.COPILOT) { |
76 |
| - // Add co-pilot when a co-pilot is added to a project |
77 |
| - return models.Project.getDirectProjectId(member.projectId) |
78 |
| - .then(directProjectId => { |
79 |
| - if (directProjectId) { |
80 |
| - // retrieve system user token |
81 |
| - return util.getSystemUserToken(logger) |
82 |
| - .then(token => { |
83 |
| - const req = { |
84 |
| - id: origRequestId, |
85 |
| - log: logger, |
86 |
| - headers: { authorization: `Bearer ${token}` } |
| 65 | + if (member.role === PROJECT_MEMBER_ROLE.COPILOT) { |
| 66 | + // Add co-pilot when a co-pilot is added to a project |
| 67 | + return models.Project.getDirectProjectId(member.projectId) |
| 68 | + .then(directProjectId => { |
| 69 | + if (directProjectId) { |
| 70 | + // retrieve system user token |
| 71 | + return util.getSystemUserToken(logger) |
| 72 | + .then(token => { |
| 73 | + const req = { |
| 74 | + id: origRequestId, |
| 75 | + log: logger, |
| 76 | + headers: { |
| 77 | + authorization: `Bearer ${token}` |
87 | 78 | }
|
88 |
| - return directProject.deleteCopilot(req, directProjectId, { |
| 79 | + } |
| 80 | + return directProject.deleteCopilot(req, directProjectId, { |
89 | 81 | copilotUserId: member.userId
|
90 | 82 | })
|
91 |
| - }) |
92 |
| - .catch(err => { |
93 |
| - logger.error('Error caught while removing co-pilot from direct', err) |
94 |
| - return next(err) |
95 |
| - }) |
96 |
| - } else { |
97 |
| - // nothing to do |
98 |
| - next() |
99 |
| - } |
100 |
| - }) |
101 |
| - .catch(err => next(err)) |
102 |
| - } else { |
103 |
| - // nothing to do |
104 |
| - next() |
105 |
| - } |
106 |
| - }) |
| 83 | + .then(resp => { |
| 84 | + logger.debug('removed copilot from direct') |
| 85 | + // acknowledge |
| 86 | + channel.ack(msg) |
| 87 | + }) |
| 88 | + }) |
| 89 | + .catch(err => { |
| 90 | + logger.error('Error caught while removing co-pilot from direct', err) |
| 91 | + channel.nack(msg, false, false) |
| 92 | + }) |
| 93 | + } else { |
| 94 | + logger.info('project not associated with a direct project, skipping') |
| 95 | + channel.ack(msg) |
| 96 | + } |
| 97 | + }) |
| 98 | + .catch(err => { |
| 99 | + // if the message has been redelivered dont attempt to reprocess it |
| 100 | + logger.error('Error retrieving project', err, msg) |
| 101 | + channel.nack(msg, false, !msg.fields.redelivered) |
| 102 | + }) |
| 103 | + } else { |
| 104 | + // nothing to do |
| 105 | + channel.ack(msg) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + projectMemberAddedHandler, |
| 111 | + projectMemberRemovedHandler |
107 | 112 | }
|
0 commit comments