diff --git a/.circleci/config.yml b/.circleci/config.yml index 16c4c55..aca5407 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'] - deployProd: context : org-global requires: diff --git a/consumer/src/salesforce-worker.js b/consumer/src/salesforce-worker.js index fe855a6..d6529ad 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; - if (eventType === 'billingAccount.updated') { + const delta = {} + if (eventType === 'billingAccount.update') { 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'; } } } @@ -73,10 +74,10 @@ 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}`); - if (statusToBe && projectId) { - debug(`Updating status to ${statusToBe} project with id ${projectId}`); - ProjectService.updateProjectStatus(projectId, statusToBe, statusChangeReason); + 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); } } @@ -88,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); }); } diff --git a/consumer/src/services/ProjectService.js b/consumer/src/services/ProjectService.js index 1f5ab65..cc72e1e 100644 --- a/consumer/src/services/ProjectService.js +++ b/consumer/src/services/ProjectService.js @@ -52,27 +52,23 @@ const getProject = (projectId) => { * * @return {Promise} promise resolved to the updated project */ -const updateProjectStatus = (projectId, status = 'active', changeReason) => { +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)}`); - const updatedProject = { status }; - if (changeReason) { - updatedProject.cancelReason = changeReason; - } return M2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET) .then((token) => ( request .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 status ${status}`); + debug(`Successfully updated the project ${projectId} with delta ${JSON.stringify(delta)}`); } 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..1ccb592 100644 --- a/consumer/test/salesforce-worker.spec.js +++ b/consumer/test/salesforce-worker.spec.js @@ -8,15 +8,15 @@ 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 }', }, }; 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(); }); });