Skip to content

[DEV] New API endpoint to get Billing Accounts from SFDC #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2021
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
8 changes: 7 additions & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,11 @@
"EMBED_REPORTS_MAPPING": "EMBED_REPORTS_MAPPING",
"ALLOWED_USERS": "REPORTS_ALLOWED_USERS"
},
"DEFAULT_M2M_USERID": "DEFAULT_M2M_USERID"
"DEFAULT_M2M_USERID": "DEFAULT_M2M_USERID",
"salesforce": {
"CLIENT_AUDIENCE": "SALESFORCE_AUDIENCE",
"CLIENT_KEY": "SALESFORCE_CLIENT_KEY",
"SUBJECT": "SALESFORCE_SUBJECT",
"CLIENT_ID": "SALESFORCE_CLIENT_ID"
}
}
8 changes: 7 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,11 @@
"ALLOWED_USERS": "[]"
},
"DEFAULT_M2M_USERID": -101,
"taasJobApiUrl": "https://api.topcoder.com/v5/jobs"
"taasJobApiUrl": "https://api.topcoder.com/v5/jobs",
"salesforce": {
"CLIENT_KEY": "",
"CLIENT_AUDIENCE": "",
"SUBJECT": "",
"CLIENT_ID": ""
}
}
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ export const M2M_SCOPES = {
ALL: 'all:projects',
READ: 'read:projects',
WRITE: 'write:projects',
WRITE_BILLING_ACCOUNTS: 'write:projects-billing-accounts',
READ_USER_BILLING_ACCOUNTS: 'read:user-billing-accounts',
WRITE_PROJECTS_BILLING_ACCOUNTS: 'write:projects-billing-accounts',
},
PROJECT_MEMBERS: {
ALL: 'all:project-members',
Expand Down
31 changes: 28 additions & 3 deletions src/permissions/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,20 @@ const SCOPES_PROJECTS_WRITE = [
M2M_SCOPES.PROJECTS.WRITE,
];

/**
* M2M scopes to "read" available Billing Accounts for the project
*/
const SCOPES_PROJECTS_READ_AVL_BILLING_ACCOUNTS = [
M2M_SCOPES.CONNECT_PROJECT_ADMIN,
M2M_SCOPES.READ_USER_BILLING_ACCOUNTS,
];

/**
* M2M scopes to "write" billingAccountId property
*/
const SCOPES_PROJECTS_WRITE_BILLING_ACCOUNTS = [
const SCOPES_PROJECTS_WRITE_PROJECTS_BILLING_ACCOUNTS = [
M2M_SCOPES.CONNECT_PROJECT_ADMIN,
M2M_SCOPES.PROJECTS.WRITE_BILLING_ACCOUNTS,
M2M_SCOPES.PROJECTS.WRITE_PROJECTS_BILLING_ACCOUNTS,
];

/**
Expand Down Expand Up @@ -231,7 +239,7 @@ export const PERMISSION = { // eslint-disable-line import/prefer-default-export
USER_ROLE.MANAGER,
USER_ROLE.TOPCODER_ADMIN,
],
scopes: SCOPES_PROJECTS_WRITE_BILLING_ACCOUNTS,
scopes: SCOPES_PROJECTS_WRITE_PROJECTS_BILLING_ACCOUNTS,
},

DELETE_PROJECT: {
Expand All @@ -252,6 +260,23 @@ export const PERMISSION = { // eslint-disable-line import/prefer-default-export
scopes: SCOPES_PROJECTS_WRITE,
},

/*
* Project Invite
*/
READ_AVL_PROJECT_BILLING_ACCOUNTS: {
meta: {
title: 'Read Available Project Billing Accounts',
group: 'Project Billing Accounts',
description: 'Who can view the Billing Accounts available for the project',
},
projectRoles: [
...PROJECT_ROLES_MANAGEMENT,
PROJECT_MEMBER_ROLE.COPILOT,
],
topcoderRoles: TOPCODER_ROLES_ADMINS,
scopes: SCOPES_PROJECTS_READ_AVL_BILLING_ACCOUNTS,
},

/*
* Project Member
*/
Expand Down
4 changes: 4 additions & 0 deletions src/permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = () => {
Authorizer.setPolicy('project.edit', generalPermission(PERMISSION.UPDATE_PROJECT));
Authorizer.setPolicy('project.delete', generalPermission(PERMISSION.DELETE_PROJECT));

Authorizer.setPolicy('projectBillingAccounts.view', generalPermission([
PERMISSION.READ_AVL_PROJECT_BILLING_ACCOUNTS,
]));

Authorizer.setPolicy('projectMember.create', generalPermission([
PERMISSION.CREATE_PROJECT_MEMBER_OWN,
PERMISSION.CREATE_PROJECT_MEMBER_NOT_OWN,
Expand Down
39 changes: 39 additions & 0 deletions src/routes/billingAccounts/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// import _ from 'lodash';
import validate from 'express-validation';
import Joi from 'joi';
import { middleware as tcMiddleware } from 'tc-core-library-js';
import SalesforceService from '../../services/salesforceService';

/**
* API to get project attachments.
*
*/

const permissions = tcMiddleware.permissions;

const schema = {
params: {
projectId: Joi.number().integer().positive().required(),
},
};

module.exports = [
validate(schema),
permissions('projectBillingAccounts.view'),
async (req, res, next) => {
// const projectId = _.parseInt(req.params.projectId);
const userId = req.authUser.userId;
try {
const { accessToken, instanceUrl } = await SalesforceService.authenticate();
// eslint-disable-next-line
const sql = `SELECT Topcoder_Billing_Account__r.id, Topcoder_Billing_Account__r.TopCoder_Billing_Account_Id__c, Topcoder_Billing_Account__r.Billing_Account_Name__c, Topcoder_Billing_Account__r.Start_Date__c, Topcoder_Billing_Account__r.End_Date__c from Topcoder_Billing_Account_Resource__c tbar where UserID__c='${userId}'`;
// and Topcoder_Billing_Account__r.TC_Connect_Project_ID__c='${projectId}'
req.log.debug(sql);
const billingAccounts = await SalesforceService.query(sql, accessToken, instanceUrl, req.log);
res.json(billingAccounts);
} catch (error) {
req.log.error(error);
next(error);
}
},
];
182 changes: 182 additions & 0 deletions src/routes/billingAccounts/list.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* eslint-disable no-unused-expressions */
import chai from 'chai';
import request from 'supertest';
import sinon from 'sinon';

import models from '../../models';
import server from '../../app';
import testUtil from '../../tests/util';
import SalesforceService from '../../services/salesforceService';

chai.should();

// demo data which might be returned by the `SalesforceService.query`
const billingAccountsData = [
{
sfBillingAccountId: 123,
tcBillingAccountId: 123123,
name: 'Billing Account 1',
startDate: '2021-02-10T18:51:27Z',
endDate: '2021-03-10T18:51:27Z',
}, {
sfBillingAccountId: 456,
tcBillingAccountId: 456456,
name: 'Billing Account 2',
startDate: '2011-02-10T18:51:27Z',
endDate: '2011-03-10T18:51:27Z',
},
];

describe('Project Billing Accounts list', () => {
let project1;
let salesforceAuthenticate;
let salesforceQuery;

beforeEach((done) => {
testUtil.clearDb()
.then(() => testUtil.clearES())
.then(() => {
models.Project.create({
type: 'generic',
directProjectId: 1,
billingAccountId: 1,
name: 'test1',
description: 'test project1',
status: 'draft',
details: {},
createdBy: 1,
updatedBy: 1,
lastActivityAt: 1,
lastActivityUserId: '1',
}).then((p) => {
project1 = p;
// create members
return models.ProjectMember.create({
userId: testUtil.userIds.copilot,
projectId: project1.id,
role: 'copilot',
isPrimary: true,
createdBy: 1,
updatedBy: 1,
}).then(() => models.ProjectMember.create({
userId: testUtil.userIds.member,
projectId: project1.id,
role: 'customer',
isPrimary: false,
createdBy: 1,
updatedBy: 1,
}));
}).then(() => {
salesforceAuthenticate = sinon.stub(SalesforceService, 'authenticate', () => Promise.resolve({
accessToken: 'mock',
instanceUrl: 'mock_url',
}));
salesforceQuery = sinon.stub(SalesforceService, 'query', () => Promise.resolve(billingAccountsData));
done();
});
});
});

afterEach((done) => {
salesforceAuthenticate.restore();
salesforceQuery.restore();
done();
});

after((done) => {
testUtil.clearDb(done);
});

describe('List /projects/{id}/billingAccounts', () => {
it('should return 403 for anonymous user', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.expect(403, done);
});

it('should return 403 for a customer user who is a member of the project', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.set({
Authorization: `Bearer ${testUtil.jwts.member}`,
})
.send()
.expect(403, done);
});

it('should return 403 for a topcoder user who is not a member of the project', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.set({
Authorization: `Bearer ${testUtil.jwts.copilotManager}`,
})
.send()
.expect(403, done);
});

it('should return all billing accounts for a topcoder user who is a member of the project', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.set({
Authorization: `Bearer ${testUtil.jwts.copilot}`,
})
.send()
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
resJson.should.have.length(2);
resJson.should.include(billingAccountsData[0]);
resJson.should.include(billingAccountsData[1]);
done();
}
});
});

it('should return all billing accounts to admin', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.set({
Authorization: `Bearer ${testUtil.jwts.admin}`,
})
.send()
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
resJson.should.have.length(2);
resJson.should.have.length(2);
resJson.should.include(billingAccountsData[0]);
resJson.should.include(billingAccountsData[1]);
done();
}
});
});

it('should return all billing accounts using M2M token with "read:user-billing-accounts" scope', (done) => {
request(server)
.get(`/v5/projects/${project1.id}/billingAccounts`)
.set({
Authorization: `Bearer ${testUtil.m2m['read:user-billing-accounts']}`,
})
.send()
.expect(200)
.end((err, res) => {
if (err) {
done(err);
} else {
const resJson = res.body;
resJson.should.have.length(2);
resJson.should.have.length(2);
resJson.should.include(billingAccountsData[0]);
resJson.should.include(billingAccountsData[1]);
done();
}
});
});
});
});
3 changes: 3 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ router.route('/v5/projects/:projectId(\\d+)/scopeChangeRequests/:requestId(\\d+)
.patch(require('./scopeChangeRequests/update'));
// .delete(require('./scopeChangeRequests/delete'));

router.route('/v5/projects/:projectId(\\d+)/billingAccounts')
.get(require('./billingAccounts/list'));

router.route('/v5/projects/:projectId(\\d+)/members')
.get(require('./projectMembers/list'))
.post(require('./projectMembers/create'));
Expand Down
Loading