Skip to content

Commit dfb350b

Browse files
author
vikasrohit
authored
Updating project billing account from sfdc (#68)
* feat: git#4058-Retrieve & Display Connect Project Billing Account from SFDC * fix: fixed name of billing account event name and some logging statements
1 parent 95447c0 commit dfb350b

File tree

5 files changed

+28
-32
lines changed

5 files changed

+28
-32
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ workflows:
9494
- test
9595
filters:
9696
branches:
97-
only: ['dev', 'feature/kafka_event_listener']
97+
only: ['dev']
9898
- deployProd:
9999
context : org-global
100100
requires:

consumer/src/salesforce-worker.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ export function consumeMessage(message) {
2828
const eventType = _.get(payload, 'Type__c');
2929
const original = JSON.parse(_.get(payload, 'Original__c'));
3030
const updated = JSON.parse(_.get(payload, 'Updated__c'));
31-
let statusToBe = null;
32-
let statusChangeReason = null;
33-
if (eventType === 'billingAccount.updated') {
31+
const delta = {}
32+
if (eventType === 'billingAccount.update') {
3433
const oldStatus = _.get(original, 'Active__c');
3534
const updatedStatus = _.get(updated, 'Active__c');
3635
debug(`${oldStatus} === ${updatedStatus}`);
36+
// billing account activated
3737
if (oldStatus !== updatedStatus && updatedStatus === true) {
38-
statusToBe = 'active';
38+
delta.status = 'active';
39+
delta.billingAccountId = parseInt(_.get(updated, 'TopCoder_Billing_Account_Id__c', 0), 10);
3940
}
4041
} else if (eventType === 'opportunity.won') {
4142
// TODO
4243
} else if (eventType === 'opportunity.lost') {
4344
// Cancel connect project
44-
statusToBe = 'cancelled';
45-
statusChangeReason = _.get(updated, 'Loss_Description__c', 'Opportunity Lost');
45+
delta.status = 'cancelled';
46+
delta.cancelReason = _.get(updated, 'Loss_Description__c', 'Opportunity Lost');
4647
} else if (eventType === 'opportunity.create') {
4748
// Move to reviewed status
48-
statusToBe = 'reviewed';
49+
delta.status = 'reviewed';
4950
} else if (eventType === 'lead.status.update') {
5051
const oldStatus = _.get(original, 'Status');
5152
const updatedStatus = _.get(updated, 'Status');
@@ -54,29 +55,29 @@ export function consumeMessage(message) {
5455
const nurtureReason = _.get(updated, 'Nurture_Reason__c');
5556
if (nurtureReason === 'BDR Rejected') {
5657
// Move to paused status
57-
statusToBe = 'paused';
58+
delta.status = 'paused';
5859
}
5960
} else if (updatedStatus === 'Disqualified') {
6061
// Cancel the project
61-
statusToBe = 'cancelled';
62-
statusChangeReason = _.get(updated, 'Disqualified_Reason__c', 'Lead Disqualified');
62+
delta.status = 'cancelled';
63+
delta.cancelReason = _.get(updated, 'Disqualified_Reason__c', 'Lead Disqualified');
6364
} else if (updatedStatus === 'Qualified') {
6465
// Move to reviewed status
65-
statusToBe = 'reviewed';
66+
delta.status = 'reviewed';
6667
} else if (updatedStatus === 'Working') {
6768
// Move to in_review status
68-
statusToBe = 'in_review';
69+
delta.status = 'in_review';
6970
}
7071
}
7172
}
7273
let projectId = _.get(updated, 'TC_Connect_Project_ID__c');
7374
if (!projectId) {
7475
projectId = _.get(updated, 'TC_Connect_Project_Id__c');
7576
}
76-
debug(`Status to be updated: ${statusToBe} for project with id ${projectId}`);
77-
if (statusToBe && projectId) {
78-
debug(`Updating status to ${statusToBe} project with id ${projectId}`);
79-
ProjectService.updateProjectStatus(projectId, statusToBe, statusChangeReason);
77+
debug(`Delta to be updated: ${JSON.stringify(delta)} for project with id ${projectId}`);
78+
if (delta.status && projectId) {
79+
debug(`Updating project with delta ${delta} with id ${projectId}`);
80+
ProjectService.updateProject(projectId, delta);
8081
}
8182
}
8283

@@ -88,7 +89,7 @@ function start() {
8889
debug('CLient created...');
8990
client.setHeader('Authorization', `OAuth ${accessToken}`);
9091
const sub = client.subscribe('/event/Connect_SFDC__e', consumeMessage);
91-
debug('Subscribed', sub);
92+
debug('Subscribed ', sub);
9293
});
9394
}
9495

consumer/src/services/ProjectService.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,23 @@ const getProject = (projectId) => {
5252
*
5353
* @return {Promise} promise resolved to the updated project
5454
*/
55-
const updateProjectStatus = (projectId, status = 'active', changeReason) => {
55+
const updateProject = (projectId, delta) => {
5656
debug(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`);
5757
debug(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`);
58-
const updatedProject = { status };
59-
if (changeReason) {
60-
updatedProject.cancelReason = changeReason;
61-
}
6258
return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
6359
.then((token) => (
6460
request
6561
.patch(`${config.projectApi.url}/projects/${projectId}`)
6662
.set('accept', 'application/json')
6763
.set('Authorization', `Bearer ${token}`)
68-
.send(updatedProject)
64+
.send(delta)
6965
.then((res) => {
7066
if (res.status !== 200) {
7167
throw new Error(`Failed to update project with id: ${projectId}`);
7268
}
7369
const project = _.get(res, 'body');
7470
if (project) {
75-
debug(`Successfully updated the project ${projectId} with status ${status}`);
71+
debug(`Successfully updated the project ${projectId} with delta ${JSON.stringify(delta)}`);
7672
}
7773
return project;
7874
})
@@ -93,5 +89,5 @@ const updateProjectStatus = (projectId, status = 'active', changeReason) => {
9389

9490
module.exports = {
9591
getProject,
96-
updateProjectStatus,
92+
updateProject,
9793
};

consumer/test/ProjectService.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ describe('ProjectService', () => {
6363
})
6464
.patch('/projects/1234')
6565
.reply(200, getProjectResponse);
66-
const project = await ProjectService.updateProjectStatus(1234);
67-
console.log(project, 'project');
66+
const project = await ProjectService.updateProject(1234);
6867
expect(project).to.deep.equal(getProjectResponse);
6968
fakeHttp.done();
7069
});

consumer/test/salesforce-worker.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import './setup';
88
describe('salesforce-worker', () => {
99
const sampleSalesforceEvent = {
1010
payload: {
11-
Type__c: 'billingAccount.updated',
11+
Type__c: 'billingAccount.update',
1212
Original__c: '{ "TC_Connect_Project_ID__c": 1234, "Active__c" : false }',
1313
Updated__c: '{ "TC_Connect_Project_ID__c": 1234, "Active__c" : true }',
1414
},
1515
};
1616
describe('consumeMessage', () => {
17-
let updateProjectStatusSpy;
17+
let updateProjectSpy;
1818
beforeEach(() => {
19-
updateProjectStatusSpy = ProjectService.updateProjectStatus = sinon.spy();
19+
updateProjectSpy = ProjectService.updateProject = 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-
updateProjectStatusSpy.should.have.been.calledWith(1234);
32+
updateProjectSpy.should.have.been.calledWith(1234);
3333
done();
3434
});
3535
});

0 commit comments

Comments
 (0)