Skip to content

Commit 9003598

Browse files
author
James Cori
committed
Merge remote-tracking branch 'origin/feature/v5-ba' into develop
2 parents 75f7961 + 30285ea commit 9003598

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ The following parameters can be set in config files or in env variables:
6262
- ES.ES_REFRESH: Elasticsearch refresh method. Default to string `true`(i.e. refresh immediately)
6363
- FILE_UPLOAD_SIZE_LIMIT: the file upload size limit in bytes
6464
- RESOURCES_API_URL: TC resources API base URL
65-
- V3_PROJECTS_API_URL: TC direct projects API base URL
6665
- GROUPS_API_URL: TC groups API base URL
6766
- PROJECTS_API_URL: TC projects API base URL
6867
- TERMS_API_URL: TC Terms API Base URL

config/default.js

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ module.exports = {
5454
GROUPS_API_URL: process.env.GROUPS_API_URL || 'http://localhost:4000/v5/groups',
5555
PROJECTS_API_URL: process.env.PROJECTS_API_URL || 'http://localhost:4000/v5/projects',
5656
TERMS_API_URL: process.env.TERMS_API_URL || 'http://localhost:4000/v5/terms',
57-
V3_PROJECTS_API_URL: process.env.V3_PROJECTS_API_URL || 'http://localhost:4000/v3/direct/projects',
5857
// copilot resource role ids allowed to upload attachment
5958
COPILOT_RESOURCE_ROLE_IDS: process.env.COPILOT_RESOURCE_ROLE_IDS
6059
? process.env.COPILOT_RESOURCE_ROLE_IDS.split(',') : ['10ba038e-48da-487b-96e8-8d3b99b6d18b'],

src/common/helper.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,15 @@ async function getProjectDefaultTerms (projectId) {
757757
* @param {Number} projectId The id of the project for which to get the default terms of use
758758
* @returns {Promise<Number>} The billing account ID
759759
*/
760-
async function getProjectBillingAccount (projectId) {
760+
async function getProjectBillingInformation (projectId) {
761761
const token = await getM2MToken()
762-
const projectUrl = `${config.V3_PROJECTS_API_URL}/${projectId}`
762+
const projectUrl = `${config.PROJECTS_API_URL}/${projectId}`
763763
try {
764764
const res = await axios.get(projectUrl, { headers: { Authorization: `Bearer ${token}` } })
765-
return _.get(res, 'data.result.content.billingAccountIds[0]', null)
765+
return {
766+
billingAccountId: _.get(res, 'data.billingAccountId', null),
767+
markup: _.get(res, 'data.markup', null) ? _.toNumber(_.get(res, 'data.markup', null)) : null
768+
}
766769
} catch (err) {
767770
if (_.get(err, 'response.status') === HttpStatus.NOT_FOUND) {
768771
throw new errors.BadRequestError(`Project with id: ${projectId} doesn't exist`)
@@ -978,7 +981,7 @@ module.exports = {
978981
validateESRefreshMethod,
979982
getProjectDefaultTerms,
980983
validateChallengeTerms,
981-
getProjectBillingAccount,
984+
getProjectBillingInformation,
982985
expandWithSubGroups,
983986
getCompleteUserGroupTreeIds,
984987
expandWithParentGroups,

src/models/Challenge.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const schema = new Schema({
2828
type: Object,
2929
required: false
3030
},
31+
billing: {
32+
type: Object,
33+
required: false
34+
},
3135
name: {
3236
type: String,
3337
required: true

src/services/ChallengeService.js

+51-13
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ async function searchChallenges (currentUser, criteria) {
580580
// Hide privateDescription for non-register challenges
581581
if (currentUser) {
582582
if (!currentUser.isMachine && !helper.hasAdminRole(currentUser)) {
583+
result = _.each(result, (val) => _.unset(val, 'billing'))
583584
const ids = await helper.listChallengesByMember(currentUser.userId)
584585
result = _.each(result, (val) => {
585586
if (!_.includes(ids, val.id)) {
@@ -588,7 +589,11 @@ async function searchChallenges (currentUser, criteria) {
588589
})
589590
}
590591
} else {
591-
result = _.each(result, val => _.unset(val, 'privateDescription'))
592+
result = _.each(result, val => {
593+
_.unset(val, 'billing')
594+
_.unset(val, 'privateDescription')
595+
return val
596+
})
592597
}
593598

594599
if (criteria.isLightweight === 'true') {
@@ -837,6 +842,13 @@ async function createChallenge (currentUser, challenge) {
837842
_.set(challenge, 'legacy.directProjectId', directProjectId)
838843
}
839844
const { track, type } = await validateChallengeData(challenge)
845+
const { billingAccountId, markup } = await helper.getProjectBillingInformation(_.get(challenge, 'projectId'))
846+
if (_.isUndefined(_.get(challenge, 'billing.billingAccountId'))) {
847+
_.set(challenge, 'billing.billingAccountId', billingAccountId)
848+
}
849+
if (_.isUndefined(_.get(challenge, 'billing.markup'))) {
850+
_.set(challenge, 'billing.markup', markup)
851+
}
840852
if (_.get(type, 'isTask')) {
841853
_.set(challenge, 'task.isTask', true)
842854
if (_.isUndefined(_.get(challenge, 'task.isAssigned'))) {
@@ -983,6 +995,10 @@ createChallenge.schema = {
983995
useSchedulingAPI: Joi.boolean(),
984996
pureV5Task: Joi.boolean()
985997
}),
998+
billing: Joi.object().keys({
999+
billingAccountId: Joi.string(),
1000+
markup: Joi.number().min(0).max(100)
1001+
}).unknown(true),
9861002
task: Joi.object().keys({
9871003
isTask: Joi.boolean().default(false),
9881004
isAssigned: Joi.boolean().default(false),
@@ -1098,12 +1114,14 @@ async function getChallenge (currentUser, id) {
10981114
let memberChallengeIds
10991115
if (currentUser) {
11001116
if (!currentUser.isMachine && !helper.hasAdminRole(currentUser)) {
1117+
_.unset(challenge, 'billing')
11011118
memberChallengeIds = await helper.listChallengesByMember(currentUser.userId)
11021119
if (!_.includes(memberChallengeIds, challenge.id)) {
11031120
_.unset(challenge, 'privateDescription')
11041121
}
11051122
}
11061123
} else {
1124+
_.unset(challenge, 'billing')
11071125
_.unset(challenge, 'privateDescription')
11081126
}
11091127

@@ -1204,25 +1222,28 @@ async function update (currentUser, challengeId, data, isFull) {
12041222
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
12051223

12061224
const challenge = await helper.getById('Challenge', challengeId)
1207-
// FIXME: Tech Debt
1208-
let billingAccountId
1225+
const { billingAccountId, markup } = await helper.getProjectBillingInformation(_.get(challenge, 'projectId'))
1226+
if (_.isUndefined(_.get(challenge, 'billing.billingAccountId'))) {
1227+
_.set(data, 'billing.billingAccountId', billingAccountId)
1228+
}
1229+
if (_.isUndefined(_.get(challenge, 'billing.markup'))) {
1230+
_.set(data, 'billing.markup', markup)
1231+
}
12091232
if (data.status) {
12101233
if (data.status === constants.challengeStatuses.Active) {
1211-
if (!_.get(challenge, 'legacy.pureV5Task') && _.isUndefined(_.get(challenge, 'legacy.directProjectId'))) {
1234+
if (!_.get(challenge, 'legacy.pureV5Task') && _.isUndefined(_.get(challenge, 'legacyId'))) {
12121235
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.')
12131236
}
1214-
billingAccountId = await helper.getProjectBillingAccount(_.get(challenge, 'legacy.directProjectId'))
12151237
// if activating a challenge, the challenge must have a billing account id
1216-
if ((!billingAccountId || billingAccountId === null) &&
1238+
if ((!_.get(challenge, 'billing.billingAccountId') || _.get(challenge, 'billing.billingAccountId') === null) &&
12171239
challenge.status === constants.challengeStatuses.Draft) {
1218-
throw new errors.BadRequestError('Cannot Activate this project, it has no active billing accounts.')
1240+
throw new errors.BadRequestError('Cannot Activate this project, it has no active billing account.')
12191241
}
12201242
}
12211243
if (data.status === constants.challengeStatuses.Completed) {
12221244
if (challenge.status !== constants.challengeStatuses.Active) {
12231245
throw new errors.BadRequestError('You cannot mark a Draft challenge as Completed')
12241246
}
1225-
billingAccountId = await helper.getProjectBillingAccount(_.get(challenge, 'legacy.directProjectId'))
12261247
}
12271248
}
12281249

@@ -1247,6 +1268,10 @@ async function update (currentUser, challengeId, data, isFull) {
12471268
_.extend(challenge.legacy, data.legacy)
12481269
}
12491270

1271+
if (!_.isUndefined(challenge.billing) && !_.isUndefined(data.billing)) {
1272+
_.extend(challenge.billing, data.billing)
1273+
}
1274+
12501275
await helper.ensureUserCanModifyChallenge(currentUser, challenge)
12511276

12521277
// check groups access to be updated group values
@@ -1415,6 +1440,9 @@ async function update (currentUser, challengeId, data, isFull) {
14151440
_.intersection(oldIds, newIds).length !== value.length) {
14161441
op = '$PUT'
14171442
}
1443+
} else if (key === 'billing' || key === 'legacy') {
1444+
// make sure that's always being udpated
1445+
op = '$PUT'
14181446
} else if (_.isUndefined(challenge[key]) || challenge[key] !== value) {
14191447
op = '$PUT'
14201448
}
@@ -1610,11 +1638,7 @@ async function update (currentUser, challengeId, data, isFull) {
16101638

16111639
// post bus event
16121640
logger.debug(`Post Bus Event: ${constants.Topics.ChallengeUpdated} ${JSON.stringify(challenge)}`)
1613-
const busEventPayload = { ...challenge }
1614-
if (billingAccountId) {
1615-
busEventPayload.billingAccountId = billingAccountId
1616-
}
1617-
await helper.postBusEvent(constants.Topics.ChallengeUpdated, busEventPayload)
1641+
await helper.postBusEvent(constants.Topics.ChallengeUpdated, challenge)
16181642
if (phasesHaveBeenModified === true && _.get(challenge, 'legacy.useSchedulingAPI')) {
16191643
await helper.postBusEvent(config.SCHEDULING_TOPIC, { id: challengeId })
16201644
}
@@ -1688,6 +1712,12 @@ function sanitizeChallenge (challenge) {
16881712
'pureV5Task'
16891713
])
16901714
}
1715+
if (challenge.billing) {
1716+
sanitized.billing = _.pick(challenge.billing, [
1717+
'billingAccountId',
1718+
'markup'
1719+
])
1720+
}
16911721
if (challenge.metadata) {
16921722
sanitized.metadata = _.map(challenge.metadata, meta => _.pick(meta, ['name', 'value']))
16931723
}
@@ -1747,6 +1777,10 @@ fullyUpdateChallenge.schema = {
17471777
useSchedulingAPI: Joi.boolean(),
17481778
pureV5Task: Joi.boolean()
17491779
}).unknown(true),
1780+
billing: Joi.object().keys({
1781+
billingAccountId: Joi.string(),
1782+
markup: Joi.number().min(0).max(100)
1783+
}).unknown(true),
17501784
task: Joi.object().keys({
17511785
isTask: Joi.boolean().default(false),
17521786
isAssigned: Joi.boolean().default(false),
@@ -1849,6 +1883,10 @@ partiallyUpdateChallenge.schema = {
18491883
isAssigned: Joi.boolean().default(false),
18501884
memberId: Joi.string().allow(null)
18511885
}),
1886+
billing: Joi.object().keys({
1887+
billingAccountId: Joi.string(),
1888+
markup: Joi.number().min(0).max(100)
1889+
}).unknown(true),
18521890
trackId: Joi.optionalId(),
18531891
typeId: Joi.optionalId(),
18541892
name: Joi.string(),

0 commit comments

Comments
 (0)