Skip to content

Commit 0253c23

Browse files
authored
Merge pull request #159 from topcoder-platform/issue-158
Add billing account checks
2 parents 4e5d565 + ed22d34 commit 0253c23

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The following parameters can be set in config files or in env variables:
6060
- ES.ES_REFRESH: Elasticsearch refresh method. Default to string `true`(i.e. refresh immediately)
6161
- FILE_UPLOAD_SIZE_LIMIT: the file upload size limit in bytes
6262
- RESOURCES_API_URL: TC resources API base URL
63+
- V3_PROJECTS_API_URL: TC direct projects API base URL
6364
- GROUPS_API_URL: TC groups API base URL
6465
- PROJECTS_API_URL: TC projects API base URL
6566
- TERMS_API_URL: TC Terms API Base URL

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
GROUPS_API_URL: process.env.GROUPS_API_URL || 'http://localhost:4000/v5/groups',
5151
PROJECTS_API_URL: process.env.PROJECTS_API_URL || 'http://localhost:4000/v5/projects',
5252
TERMS_API_URL: process.env.TERMS_API_URL || 'http://localhost:4000/v5/terms',
53+
V3_PROJECTS_API_URL: process.env.V3_PROJECTS_API_URL || 'http://localhost:4000/v3/direct/projects',
5354
// copilot resource role ids allowed to upload attachment
5455
COPILOT_RESOURCE_ROLE_IDS: process.env.COPILOT_RESOURCE_ROLE_IDS
5556
? process.env.COPILOT_RESOURCE_ROLE_IDS.split(',') : ['10ba038e-48da-487b-96e8-8d3b99b6d18b'],

src/common/helper.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,19 @@ async function getProjectDefaultTerms (projectId) {
587587
return res.data.terms || []
588588
}
589589

590+
/**
591+
* This functions gets the default billing account for a given project id
592+
*
593+
* @param {Number} projectId The id of the project for which to get the default terms of use
594+
* @returns {Promise<Number>} The billing account ID
595+
*/
596+
async function getProjectBillingAccount (projectId) {
597+
const token = await getM2MToken()
598+
const projectUrl = `${config.V3_PROJECTS_API_URL}/${projectId}`
599+
const res = await axios.get(projectUrl, { headers: { Authorization: `Bearer ${token}` } })
600+
return _.get(res, 'data.result.content.billingAccountIds[0]', null)
601+
}
602+
590603
/**
591604
* This function gets the challenge terms array with the terms data
592605
* The terms data is retrieved from the terms API using the specified terms ids
@@ -642,5 +655,6 @@ module.exports = {
642655
listChallengesByMember,
643656
validateESRefreshMethod,
644657
getProjectDefaultTerms,
645-
validateChallengeTerms
658+
validateChallengeTerms,
659+
getProjectBillingAccount
646660
}

src/services/ChallengeService.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ async function populatePhases (phases, startDate, timelineTemplateId) {
443443
* @returns {Object} the created challenge
444444
*/
445445
async function createChallenge (currentUser, challenge, userToken) {
446+
if (challenge.status === constants.challengeStatuses.Active) {
447+
throw new errors.BadRequestError('You cannot create an Active challenge. Please create a Draft challenge and then change the status to Active.')
448+
}
446449
await helper.ensureProjectExist(challenge.projectId, userToken)
447450
await validateChallengeData(challenge)
448451
if (challenge.phases && challenge.phases.length > 0) {
@@ -755,6 +758,18 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
755758
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
756759

757760
const challenge = await helper.getById('Challenge', challengeId)
761+
let billingAccountId
762+
if (data.status) {
763+
if (data.status === constants.challengeStatuses.Active && _.isUndefined(challenge.legacy.directProjectId)) {
764+
throw new errors.BadRequestError('You cannot activate the challenge as it has not been created on legacy yet. Please try again later or contact support.')
765+
}
766+
if (data.status === constants.challengeStatuses.Completed) {
767+
if (challenge.status !== constants.challengeStatuses.Active) {
768+
throw new errors.BadRequestError('You cannot mark a Draft challenge as Completed')
769+
}
770+
billingAccountId = helper.getProjectBillingAccount(challenge.legacy.directProjectId)
771+
}
772+
}
758773

759774
// FIXME: Tech Debt
760775
if (_.get(challenge, 'legacy.track') && _.get(data, 'legacy.track') && _.get(challenge, 'legacy.track') !== _.get(data, 'legacy.track')) {
@@ -1091,7 +1106,11 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
10911106

10921107
// post bus event
10931108
logger.debug(`Post Bus Event: ${constants.Topics.ChallengeUpdated} ${JSON.stringify(challenge)}`)
1094-
await helper.postBusEvent(constants.Topics.ChallengeUpdated, challenge)
1109+
const busEventPayload = { ...challenge }
1110+
if (billingAccountId) {
1111+
busEventPayload.billingAccountId = billingAccountId
1112+
}
1113+
await helper.postBusEvent(constants.Topics.ChallengeUpdated, busEventPayload)
10951114

10961115
if (challenge.phases && challenge.phases.length > 0) {
10971116
challenge.currentPhase = challenge.phases.slice().reverse().find(phase => phase.isOpen)

0 commit comments

Comments
 (0)