@@ -580,6 +580,7 @@ async function searchChallenges (currentUser, criteria) {
580
580
// Hide privateDescription for non-register challenges
581
581
if ( currentUser ) {
582
582
if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
583
+ result = _ . each ( result , ( val ) => _ . unset ( val , 'billing' ) )
583
584
const ids = await helper . listChallengesByMember ( currentUser . userId )
584
585
result = _ . each ( result , ( val ) => {
585
586
if ( ! _ . includes ( ids , val . id ) ) {
@@ -588,7 +589,11 @@ async function searchChallenges (currentUser, criteria) {
588
589
} )
589
590
}
590
591
} 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
+ } )
592
597
}
593
598
594
599
if ( criteria . isLightweight === 'true' ) {
@@ -837,6 +842,13 @@ async function createChallenge (currentUser, challenge) {
837
842
_ . set ( challenge , 'legacy.directProjectId' , directProjectId )
838
843
}
839
844
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
+ }
840
852
if ( _ . get ( type , 'isTask' ) ) {
841
853
_ . set ( challenge , 'task.isTask' , true )
842
854
if ( _ . isUndefined ( _ . get ( challenge , 'task.isAssigned' ) ) ) {
@@ -983,6 +995,10 @@ createChallenge.schema = {
983
995
useSchedulingAPI : Joi . boolean ( ) ,
984
996
pureV5Task : Joi . boolean ( )
985
997
} ) ,
998
+ billing : Joi . object ( ) . keys ( {
999
+ billingAccountId : Joi . string ( ) ,
1000
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1001
+ } ) . unknown ( true ) ,
986
1002
task : Joi . object ( ) . keys ( {
987
1003
isTask : Joi . boolean ( ) . default ( false ) ,
988
1004
isAssigned : Joi . boolean ( ) . default ( false ) ,
@@ -1098,12 +1114,14 @@ async function getChallenge (currentUser, id) {
1098
1114
let memberChallengeIds
1099
1115
if ( currentUser ) {
1100
1116
if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
1117
+ _ . unset ( challenge , 'billing' )
1101
1118
memberChallengeIds = await helper . listChallengesByMember ( currentUser . userId )
1102
1119
if ( ! _ . includes ( memberChallengeIds , challenge . id ) ) {
1103
1120
_ . unset ( challenge , 'privateDescription' )
1104
1121
}
1105
1122
}
1106
1123
} else {
1124
+ _ . unset ( challenge , 'billing' )
1107
1125
_ . unset ( challenge , 'privateDescription' )
1108
1126
}
1109
1127
@@ -1204,25 +1222,28 @@ async function update (currentUser, challengeId, data, isFull) {
1204
1222
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
1205
1223
1206
1224
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
+ }
1209
1232
if ( data . status ) {
1210
1233
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 ' ) ) ) {
1212
1235
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.' )
1213
1236
}
1214
- billingAccountId = await helper . getProjectBillingAccount ( _ . get ( challenge , 'legacy.directProjectId' ) )
1215
1237
// 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 ) &&
1217
1239
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 .' )
1219
1241
}
1220
1242
}
1221
1243
if ( data . status === constants . challengeStatuses . Completed ) {
1222
1244
if ( challenge . status !== constants . challengeStatuses . Active ) {
1223
1245
throw new errors . BadRequestError ( 'You cannot mark a Draft challenge as Completed' )
1224
1246
}
1225
- billingAccountId = await helper . getProjectBillingAccount ( _ . get ( challenge , 'legacy.directProjectId' ) )
1226
1247
}
1227
1248
}
1228
1249
@@ -1247,6 +1268,10 @@ async function update (currentUser, challengeId, data, isFull) {
1247
1268
_ . extend ( challenge . legacy , data . legacy )
1248
1269
}
1249
1270
1271
+ if ( ! _ . isUndefined ( challenge . billing ) && ! _ . isUndefined ( data . billing ) ) {
1272
+ _ . extend ( challenge . billing , data . billing )
1273
+ }
1274
+
1250
1275
await helper . ensureUserCanModifyChallenge ( currentUser , challenge )
1251
1276
1252
1277
// check groups access to be updated group values
@@ -1415,6 +1440,9 @@ async function update (currentUser, challengeId, data, isFull) {
1415
1440
_ . intersection ( oldIds , newIds ) . length !== value . length ) {
1416
1441
op = '$PUT'
1417
1442
}
1443
+ } else if ( key === 'billing' || key === 'legacy' ) {
1444
+ // make sure that's always being udpated
1445
+ op = '$PUT'
1418
1446
} else if ( _ . isUndefined ( challenge [ key ] ) || challenge [ key ] !== value ) {
1419
1447
op = '$PUT'
1420
1448
}
@@ -1610,11 +1638,7 @@ async function update (currentUser, challengeId, data, isFull) {
1610
1638
1611
1639
// post bus event
1612
1640
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 )
1618
1642
if ( phasesHaveBeenModified === true && _ . get ( challenge , 'legacy.useSchedulingAPI' ) ) {
1619
1643
await helper . postBusEvent ( config . SCHEDULING_TOPIC , { id : challengeId } )
1620
1644
}
@@ -1688,6 +1712,12 @@ function sanitizeChallenge (challenge) {
1688
1712
'pureV5Task'
1689
1713
] )
1690
1714
}
1715
+ if ( challenge . billing ) {
1716
+ sanitized . billing = _ . pick ( challenge . billing , [
1717
+ 'billingAccountId' ,
1718
+ 'markup'
1719
+ ] )
1720
+ }
1691
1721
if ( challenge . metadata ) {
1692
1722
sanitized . metadata = _ . map ( challenge . metadata , meta => _ . pick ( meta , [ 'name' , 'value' ] ) )
1693
1723
}
@@ -1747,6 +1777,10 @@ fullyUpdateChallenge.schema = {
1747
1777
useSchedulingAPI : Joi . boolean ( ) ,
1748
1778
pureV5Task : Joi . boolean ( )
1749
1779
} ) . unknown ( true ) ,
1780
+ billing : Joi . object ( ) . keys ( {
1781
+ billingAccountId : Joi . string ( ) ,
1782
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1783
+ } ) . unknown ( true ) ,
1750
1784
task : Joi . object ( ) . keys ( {
1751
1785
isTask : Joi . boolean ( ) . default ( false ) ,
1752
1786
isAssigned : Joi . boolean ( ) . default ( false ) ,
@@ -1849,6 +1883,10 @@ partiallyUpdateChallenge.schema = {
1849
1883
isAssigned : Joi . boolean ( ) . default ( false ) ,
1850
1884
memberId : Joi . string ( ) . allow ( null )
1851
1885
} ) ,
1886
+ billing : Joi . object ( ) . keys ( {
1887
+ billingAccountId : Joi . string ( ) ,
1888
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1889
+ } ) . unknown ( true ) ,
1852
1890
trackId : Joi . optionalId ( ) ,
1853
1891
typeId : Joi . optionalId ( ) ,
1854
1892
name : Joi . string ( ) ,
0 commit comments