Skip to content

Commit 6f76a38

Browse files
author
vikasrohit
authored
Merge pull request #51 from appirio-tech/dev
[Prod Release] Opportunity/Lead status sync with Connect projects
2 parents 86b12e3 + fa8f466 commit 6f76a38

File tree

4 files changed

+67
-53
lines changed

4 files changed

+67
-53
lines changed

consumer/src/salesforce-worker.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,42 @@ export function consumeMessage(message) {
2323
debug('Got Connect_SFDC__e', message);
2424
const payload = _.get(message, 'payload');
2525
const eventType = _.get(payload, 'Type__c');
26+
const original = JSON.parse(_.get(payload, 'Original__c'));
27+
const updated = JSON.parse(_.get(payload, 'Updated__c'));
28+
let statusToBe = null;
29+
let statusChangeReason = null;
2630
if (eventType === 'billingAccount.updated') {
27-
const original = JSON.parse(_.get(payload, 'Original__c'));
28-
const updated = JSON.parse(_.get(payload, 'Updated__c'));
2931
const oldStatus = _.get(original, 'Active__c');
3032
const updatedStatus = _.get(updated, 'Active__c');
3133
debug(`${oldStatus} === ${updatedStatus}`);
3234
if (oldStatus !== updatedStatus && updatedStatus === true) {
33-
const projectId = _.get(updated, 'TC_Connect_Project_ID__c');
34-
debug(`Activating project with id ${projectId}`);
35-
// TODO retrieve project id from the payload
36-
if (projectId) {
37-
ProjectService.activateProject(projectId);
38-
}
35+
statusToBe = 'active'
3936
}
37+
} else if (eventType === 'opportunity.won') {
38+
// TODO
39+
} else if (eventType === 'opportunity.lost') {
40+
// Cancel connect project
41+
statusToBe = 'cancelled'
42+
statusChangeReason = _.get(updated, 'Loss_Description__c', 'Opportunity Lost');
43+
} else if (eventType === 'lead.disqualified') {
44+
// Cancel the project
45+
statusToBe = 'cancelled'
46+
statusChangeReason = _.get(updated, 'Disqualified_Reason__c', 'Lead Disqualified');
47+
} else if (eventType === 'opportunity.create') {
48+
// Move to reviewed status
49+
statusToBe = 'reviewed'
50+
} else if (eventType === 'lead.qualified') {
51+
// Move to reviewed status
52+
statusToBe = 'reviewed'
53+
}
54+
let projectId = _.get(updated, 'TC_Connect_Project_ID__c');
55+
if (!projectId) {
56+
projectId = _.get(updated, 'TC_Connect_Project_Id__c');
57+
}
58+
debug(`Status to be updated: ${statusToBe} for project with id ${projectId}`);
59+
if (statusToBe && projectId) {
60+
debug(`Updating status to ${statusToBe} project with id ${projectId}`);
61+
ProjectService.updateProjectStatus(projectId, statusToBe, statusChangeReason);
4062
}
4163
}
4264

consumer/src/services/ProjectService.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const request = require('superagent');
66
const config = require('config');
77
const _ = require('lodash');
88

9+
const debug = require('debug')('app:project-service');
10+
911
/**
1012
* Get project details
1113
*
@@ -14,25 +16,22 @@ const _ = require('lodash');
1416
* @return {Promise} promise resolved to project details
1517
*/
1618
const getProject = (projectId) => {
17-
console.log(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`);
18-
console.log(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`);
19-
console.log(`AUTH0_URL: ${config.AUTH0_URL}`);
20-
console.log(`AUTH0_AUDIENCE: ${config.AUTH0_AUDIENCE}`);
21-
console.log(`AUTH0_PROXY_SERVER_URL: ${config.AUTH0_PROXY_SERVER_URL}`);
19+
debug(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`);
20+
debug(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`);
2221
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
2322
.then((token) => (
2423
request
2524
.get(`${config.projectApi.url}/projects/${projectId}`)
2625
.set('accept', 'application/json')
2726
.set('authorization', `Bearer ${token}`)
2827
.then((res) => {
29-
if (!_.get(res, 'body.result.success')) {
28+
if (res.status !== 200) {
3029
throw new Error(`Failed to get project details of project id: ${projectId}`);
3130
}
32-
const project = _.get(res, 'body.result.content');
31+
const project = _.get(res, 'body');
3332
return project;
3433
}).catch((err) => {
35-
const errorDetails = _.get(err, 'response.body.result.content.message');
34+
const errorDetails = _.get(err, 'response.body');
3635
throw new Error(
3736
`Failed to get project details of project id: ${projectId}.` +
3837
(errorDetails ? ' Server response: ' + errorDetails : '')
@@ -52,31 +51,32 @@ const getProject = (projectId) => {
5251
*
5352
* @return {Promise} promise resolved to the updated project
5453
*/
55-
const activateProject = (projectId) => {
56-
console.log(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`);
57-
console.log(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`);
58-
console.log(`AUTH0_URL: ${config.AUTH0_URL}`);
59-
console.log(`AUTH0_AUDIENCE: ${config.AUTH0_AUDIENCE}`);
60-
console.log(`AUTH0_PROXY_SERVER_URL: ${config.AUTH0_PROXY_SERVER_URL}`);
54+
const updateProjectStatus = (projectId, status='active', changeReason) => {
55+
debug(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`);
56+
debug(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`);
57+
const updatedProject = { status };
58+
if (changeReason) {
59+
updatedProject.cancelReason = changeReason;
60+
}
6161
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
6262
.then((token) => (
6363
request
6464
.patch(`${config.projectApi.url}/projects/${projectId}`)
6565
.set('accept', 'application/json')
6666
.set('Authorization', `Bearer ${token}`)
67-
.send({ param : { status : 'active' } })
67+
.send(updatedProject)
6868
.then((res) => {
69-
if (!_.get(res, 'body.result.success')) {
70-
throw new Error(`Failed to activate project with id: ${projectId}`);
69+
if (res.status !== 200) {
70+
throw new Error(`Failed to update project with id: ${projectId}`);
7171
}
72-
const project = _.get(res, 'body.result.content');
72+
const project = _.get(res, 'body');
7373
if (project) {
74-
console.log(`Successfully activated the project with id ${projectId}`);
74+
debug(`Successfully updated the project ${projectId} with status ${status}`);
7575
}
7676
return project;
7777
}).catch((err) => {
78-
console.log(err);
79-
const errorDetails = _.get(err, 'response.body.result.content.message');
78+
debug(err);
79+
const errorDetails = _.get(err, 'response.body');
8080
throw new Error(
8181
`Failed to update project with id: ${projectId}.` +
8282
(errorDetails ? ' Server response: ' + errorDetails : '')
@@ -91,5 +91,5 @@ const activateProject = (projectId) => {
9191

9292
module.exports = {
9393
getProject,
94-
activateProject
94+
updateProjectStatus
9595
};

consumer/test/ProjectService.spec.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,13 @@ const authenticateResponse = {
3131
};
3232

3333
const getProjectResponse = {
34-
id: '-88f3803:1557f8485b0:-b0a',
35-
result: {
36-
success: true,
37-
status: 200,
38-
metadata: null,
39-
content: {
40-
id: '265522',
41-
modifiedBy: null,
42-
modifiedAt: '2016-06-01T16:57:47.000Z',
43-
createdBy: null,
44-
createdAt: '2002-02-06T18:06:40.000Z',
45-
status: 'active',
46-
name: 'Test Project',
47-
},
48-
},
49-
version: 'v4',
34+
id: '265522',
35+
modifiedBy: null,
36+
modifiedAt: '2016-06-01T16:57:47.000Z',
37+
createdBy: null,
38+
createdAt: '2002-02-06T18:06:40.000Z',
39+
status: 'active',
40+
name: 'Test Project',
5041
};
5142

5243

@@ -80,8 +71,8 @@ describe('ProjectService', () => {
8071
})
8172
.get('/projects/1234')
8273
.reply(200, getProjectResponse);
83-
const user = await ProjectService.getProject(1234);
84-
expect(user).to.deep.equal(getProjectResponse.result.content);
74+
const project = await ProjectService.getProject(1234);
75+
expect(project).to.deep.equal(getProjectResponse);
8576
fakeHttp.done();
8677
});
8778

@@ -93,8 +84,9 @@ describe('ProjectService', () => {
9384
})
9485
.patch('/projects/1234')
9586
.reply(200, getProjectResponse);
96-
const user = await ProjectService.activateProject(1234);
97-
expect(user).to.deep.equal(getProjectResponse.result.content);
87+
const project = await ProjectService.updateProjectStatus(1234);
88+
console.log(project, 'project')
89+
expect(project).to.deep.equal(getProjectResponse);
9890
fakeHttp.done();
9991
});
10092
});

consumer/test/salesforce-worker.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('salesforce-worker', () => {
1414
}
1515
}
1616
describe('consumeMessage', () => {
17-
let activateProjectSpy;
17+
let updateProjectStatusSpy;
1818
beforeEach(() => {
19-
activateProjectSpy = ProjectService.activateProject = sinon.spy();
19+
updateProjectStatusSpy = ProjectService.updateProjectStatus = sinon.spy();
2020
});
2121

2222
/**
@@ -29,7 +29,7 @@ describe('salesforce-worker', () => {
2929

3030
it('should consume and active project successfully', (done) => {
3131
invokeConsume(done);
32-
activateProjectSpy.should.have.been.calledWith(1234);
32+
updateProjectStatusSpy.should.have.been.calledWith(1234);
3333
done();
3434
});
3535
});

0 commit comments

Comments
 (0)