Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ workflows:
- test
filters:
branches:
only: ['dev', 'feature/kafka_event_listener']
only: ['dev']
- deployProd:
context : org-global
requires:
Expand Down
35 changes: 18 additions & 17 deletions consumer/src/salesforce-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -54,29 +55,29 @@ 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';
}
}
}
let projectId = _.get(updated, 'TC_Connect_Project_ID__c');
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);
}
}

Expand All @@ -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);
});
}

Expand Down
12 changes: 4 additions & 8 deletions consumer/src/services/ProjectService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand All @@ -93,5 +89,5 @@ const updateProjectStatus = (projectId, status = 'active', changeReason) => {

module.exports = {
getProject,
updateProjectStatus,
updateProject,
};
3 changes: 1 addition & 2 deletions consumer/test/ProjectService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
8 changes: 4 additions & 4 deletions consumer/test/salesforce-worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

/**
Expand All @@ -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();
});
});
Expand Down