From 968b02cca8f4cf3e6c49fba1635b748fdff61595 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 14:05:59 +0530 Subject: [PATCH 1/9] feat: git#4058-Retrieve & Display Connect Project Billing Account from SFDC --- consumer/src/salesforce-worker.js | 29 +++++++++++++------------ consumer/src/services/ProjectService.js | 10 +++------ consumer/test/ProjectService.spec.js | 3 +-- consumer/test/salesforce-worker.spec.js | 6 ++--- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index fe855a6..439390d 100644 --- a/consumer/src/salesforce-worker.js +++ b/consumer/src/salesforce-worker.js @@ -28,24 +28,25 @@ export function consumeMessage(message) { const eventType = _.get(payload, 'Type__c'); const original = JSON.parse(_.get(payload, 'Original__c')); const updated = JSON.parse(_.get(payload, 'Updated__c')); - let statusToBe = null; - let statusChangeReason = null; + const delta = { status: statusToBe, cancelReason: statusChangeReason } if (eventType === 'billingAccount.updated') { const oldStatus = _.get(original, 'Active__c'); const updatedStatus = _.get(updated, 'Active__c'); debug(`${oldStatus} === ${updatedStatus}`); + // billing account activated if (oldStatus !== updatedStatus && updatedStatus === true) { - statusToBe = 'active'; + delta.status = 'active'; + delta.billingAccountId = parseInt(_.get(updated, 'TopCoder_Billing_Account_Id__c', 0), 10); } } else if (eventType === 'opportunity.won') { // TODO } else if (eventType === 'opportunity.lost') { // Cancel connect project - statusToBe = 'cancelled'; - statusChangeReason = _.get(updated, 'Loss_Description__c', 'Opportunity Lost'); + delta.status = 'cancelled'; + delta.cancelReason = _.get(updated, 'Loss_Description__c', 'Opportunity Lost'); } else if (eventType === 'opportunity.create') { // Move to reviewed status - statusToBe = 'reviewed'; + delta.status = 'reviewed'; } else if (eventType === 'lead.status.update') { const oldStatus = _.get(original, 'Status'); const updatedStatus = _.get(updated, 'Status'); @@ -54,18 +55,18 @@ export function consumeMessage(message) { const nurtureReason = _.get(updated, 'Nurture_Reason__c'); if (nurtureReason === 'BDR Rejected') { // Move to paused status - statusToBe = 'paused'; + delta.status = 'paused'; } } else if (updatedStatus === 'Disqualified') { // Cancel the project - statusToBe = 'cancelled'; - statusChangeReason = _.get(updated, 'Disqualified_Reason__c', 'Lead Disqualified'); + delta.status = 'cancelled'; + delta.cancelReason = _.get(updated, 'Disqualified_Reason__c', 'Lead Disqualified'); } else if (updatedStatus === 'Qualified') { // Move to reviewed status - statusToBe = 'reviewed'; + delta.status = 'reviewed'; } else if (updatedStatus === 'Working') { // Move to in_review status - statusToBe = 'in_review'; + delta.status = 'in_review'; } } } @@ -74,9 +75,9 @@ export function consumeMessage(message) { projectId = _.get(updated, 'TC_Connect_Project_Id__c'); } debug(`Status to be updated: ${statusToBe} for project with id ${projectId}`); - if (statusToBe && projectId) { - debug(`Updating status to ${statusToBe} project with id ${projectId}`); - ProjectService.updateProjectStatus(projectId, statusToBe, statusChangeReason); + if (delta && delta.status && projectId) { + debug(`Updating project with delta ${delta} with id ${projectId}`); + ProjectService.updateProject(projectId, delta); } } diff --git a/consumer/src/services/ProjectService.js b/consumer/src/services/ProjectService.js index 1f5ab65..671b556 100644 --- a/consumer/src/services/ProjectService.js +++ b/consumer/src/services/ProjectService.js @@ -52,13 +52,9 @@ const getProject = (projectId) => { * * @return {Promise} promise resolved to the updated project */ -const updateProjectStatus = (projectId, status = 'active', changeReason) => { +const updateProject = (projectId, updatedProject) => { debug(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`); debug(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`); - const updatedProject = { status }; - if (changeReason) { - updatedProject.cancelReason = changeReason; - } return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => ( request @@ -72,7 +68,7 @@ const updateProjectStatus = (projectId, status = 'active', changeReason) => { } const project = _.get(res, 'body'); if (project) { - debug(`Successfully updated the project ${projectId} with status ${status}`); + debug(`Successfully updated the project ${projectId} with delat ${updatedProject}`); } return project; }) @@ -93,5 +89,5 @@ const updateProjectStatus = (projectId, status = 'active', changeReason) => { module.exports = { getProject, - updateProjectStatus, + updateProject, }; diff --git a/consumer/test/ProjectService.spec.js b/consumer/test/ProjectService.spec.js index 46c5e25..5c5554e 100644 --- a/consumer/test/ProjectService.spec.js +++ b/consumer/test/ProjectService.spec.js @@ -63,8 +63,7 @@ describe('ProjectService', () => { }) .patch('/projects/1234') .reply(200, getProjectResponse); - const project = await ProjectService.updateProjectStatus(1234); - console.log(project, 'project'); + const project = await ProjectService.updateProject(1234); expect(project).to.deep.equal(getProjectResponse); fakeHttp.done(); }); diff --git a/consumer/test/salesforce-worker.spec.js b/consumer/test/salesforce-worker.spec.js index 1ccb9c7..c0aa153 100644 --- a/consumer/test/salesforce-worker.spec.js +++ b/consumer/test/salesforce-worker.spec.js @@ -14,9 +14,9 @@ describe('salesforce-worker', () => { }, }; describe('consumeMessage', () => { - let updateProjectStatusSpy; + let updateProjectSpy; beforeEach(() => { - updateProjectStatusSpy = ProjectService.updateProjectStatus = sinon.spy(); + updateProjectSpy = ProjectService.updateProject = sinon.spy(); }); /** @@ -29,7 +29,7 @@ describe('salesforce-worker', () => { it('should consume and active project successfully', (done) => { invokeConsume(done); - updateProjectStatusSpy.should.have.been.calledWith(1234); + updateProjectSpy.should.have.been.calledWith(1234); done(); }); }); From 06d9fa5392ee2c9ee4edf332e6a902152e656f84 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 14:07:05 +0530 Subject: [PATCH 2/9] chore: temporary deployable feature branch --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 16c4c55..9f1fb60 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -94,7 +94,7 @@ workflows: - test filters: branches: - only: ['dev', 'feature/kafka_event_listener'] + only: ['dev', 'feature/updateing_billing_account_from_sfdc'] - deployProd: context : org-global requires: From 8c1cad8a3264b256343a600a51a97f1e0ba08158 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 14:11:27 +0530 Subject: [PATCH 3/9] fix: typo fix --- consumer/src/salesforce-worker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index 439390d..e58473d 100644 --- a/consumer/src/salesforce-worker.js +++ b/consumer/src/salesforce-worker.js @@ -28,7 +28,7 @@ export function consumeMessage(message) { const eventType = _.get(payload, 'Type__c'); const original = JSON.parse(_.get(payload, 'Original__c')); const updated = JSON.parse(_.get(payload, 'Updated__c')); - const delta = { status: statusToBe, cancelReason: statusChangeReason } + const delta = {} if (eventType === 'billingAccount.updated') { const oldStatus = _.get(original, 'Active__c'); const updatedStatus = _.get(updated, 'Active__c'); @@ -74,7 +74,7 @@ export function consumeMessage(message) { if (!projectId) { projectId = _.get(updated, 'TC_Connect_Project_Id__c'); } - debug(`Status to be updated: ${statusToBe} for project with id ${projectId}`); + debug(`Delta to be updated: ${delta} for project with id ${projectId}`); if (delta && delta.status && projectId) { debug(`Updating project with delta ${delta} with id ${projectId}`); ProjectService.updateProject(projectId, delta); From 1637a28df57ea31fcb08744ca2136c979ae6bbba Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 16:43:59 +0530 Subject: [PATCH 4/9] fix: fixed name of billing account event name and some logging statements --- consumer/src/salesforce-worker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index e58473d..53790b5 100644 --- a/consumer/src/salesforce-worker.js +++ b/consumer/src/salesforce-worker.js @@ -29,7 +29,7 @@ export function consumeMessage(message) { const original = JSON.parse(_.get(payload, 'Original__c')); const updated = JSON.parse(_.get(payload, 'Updated__c')); const delta = {} - if (eventType === 'billingAccount.updated') { + if (eventType === 'billingAccount.update') { const oldStatus = _.get(original, 'Active__c'); const updatedStatus = _.get(updated, 'Active__c'); debug(`${oldStatus} === ${updatedStatus}`); @@ -74,8 +74,8 @@ export function consumeMessage(message) { if (!projectId) { projectId = _.get(updated, 'TC_Connect_Project_Id__c'); } - debug(`Delta to be updated: ${delta} for project with id ${projectId}`); - if (delta && delta.status && projectId) { + debug(`Delta to be updated: ${JSON.stringify(delta)} for project with id ${projectId}`); + if (delta.status && projectId) { debug(`Updating project with delta ${delta} with id ${projectId}`); ProjectService.updateProject(projectId, delta); } @@ -89,7 +89,7 @@ function start() { debug('CLient created...'); client.setHeader('Authorization', `OAuth ${accessToken}`); const sub = client.subscribe('/event/Connect_SFDC__e', consumeMessage); - debug('Subscribed', sub); + debug(`Subscribed ${sub}`); }); } From 557284396a35d1914d6a18b44f943908973e9e7d Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 17:03:34 +0530 Subject: [PATCH 5/9] fix: fixed unit test --- consumer/test/salesforce-worker.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumer/test/salesforce-worker.spec.js b/consumer/test/salesforce-worker.spec.js index c0aa153..1ccb592 100644 --- a/consumer/test/salesforce-worker.spec.js +++ b/consumer/test/salesforce-worker.spec.js @@ -8,7 +8,7 @@ import './setup'; describe('salesforce-worker', () => { const sampleSalesforceEvent = { payload: { - Type__c: 'billingAccount.updated', + Type__c: 'billingAccount.update', Original__c: '{ "TC_Connect_Project_ID__c": 1234, "Active__c" : false }', Updated__c: '{ "TC_Connect_Project_ID__c": 1234, "Active__c" : true }', }, From 2fcc15204d797ed89b4c60de5f404a5fa65138d4 Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 17:29:05 +0530 Subject: [PATCH 6/9] fix: fixed JSON logging statement --- consumer/src/services/ProjectService.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/consumer/src/services/ProjectService.js b/consumer/src/services/ProjectService.js index 671b556..cc72e1e 100644 --- a/consumer/src/services/ProjectService.js +++ b/consumer/src/services/ProjectService.js @@ -52,7 +52,7 @@ const getProject = (projectId) => { * * @return {Promise} promise resolved to the updated project */ -const updateProject = (projectId, updatedProject) => { +const updateProject = (projectId, delta) => { debug(`AUTH0_CLIENT_ID: ${config.AUTH0_CLIENT_ID.substring(0, 5)}`); debug(`AUTH0_CLIENT_SECRET: ${config.AUTH0_CLIENT_SECRET.substring(0, 5)}`); return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) @@ -61,14 +61,14 @@ const updateProject = (projectId, updatedProject) => { .patch(`${config.projectApi.url}/projects/${projectId}`) .set('accept', 'application/json') .set('Authorization', `Bearer ${token}`) - .send(updatedProject) + .send(delta) .then((res) => { if (res.status !== 200) { throw new Error(`Failed to update project with id: ${projectId}`); } const project = _.get(res, 'body'); if (project) { - debug(`Successfully updated the project ${projectId} with delat ${updatedProject}`); + debug(`Successfully updated the project ${projectId} with delta ${JSON.stringify(delta)}`); } return project; }) From 261ae24eee53e0f52afe6ec3ec3aee8d900ff4bc Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 17:55:56 +0530 Subject: [PATCH 7/9] fix: fixed JSON logging statement --- consumer/src/salesforce-worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index 53790b5..a7cc3c1 100644 --- a/consumer/src/salesforce-worker.js +++ b/consumer/src/salesforce-worker.js @@ -89,7 +89,7 @@ function start() { debug('CLient created...'); client.setHeader('Authorization', `OAuth ${accessToken}`); const sub = client.subscribe('/event/Connect_SFDC__e', consumeMessage); - debug(`Subscribed ${sub}`); + debug(`Subscribed ${JSON.stringify(sub)}`); }); } From 01e20228f2d46f70fc6ab439255d912c5ad40ede Mon Sep 17 00:00:00 2001 From: vikasrohit Date: Fri, 7 Aug 2020 18:28:29 +0530 Subject: [PATCH 8/9] Removed temporary change Removed configuration for making the feature branch deployable --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9f1fb60..aca5407 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -94,7 +94,7 @@ workflows: - test filters: branches: - only: ['dev', 'feature/updateing_billing_account_from_sfdc'] + only: ['dev'] - deployProd: context : org-global requires: From 3d2a6266afde643fcab5668300449545457355ab Mon Sep 17 00:00:00 2001 From: Vikas Agarwal Date: Fri, 7 Aug 2020 18:29:53 +0530 Subject: [PATCH 9/9] Fixed error in logging --- consumer/src/salesforce-worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index a7cc3c1..d6529ad 100644 --- a/consumer/src/salesforce-worker.js +++ b/consumer/src/salesforce-worker.js @@ -89,7 +89,7 @@ function start() { debug('CLient created...'); client.setHeader('Authorization', `OAuth ${accessToken}`); const sub = client.subscribe('/event/Connect_SFDC__e', consumeMessage); - debug(`Subscribed ${JSON.stringify(sub)}`); + debug('Subscribed ', sub); }); }