@@ -264,6 +264,17 @@ async function searchChallenges (currentUser, criteria) {
264
264
} )
265
265
}
266
266
267
+ if ( criteria . totalPrizesFrom || criteria . totalPrizesTo ) {
268
+ const prizeRangeQuery = { }
269
+ if ( criteria . totalPrizesFrom ) {
270
+ prizeRangeQuery . gte = criteria . totalPrizesFrom
271
+ }
272
+ if ( criteria . totalPrizesTo ) {
273
+ prizeRangeQuery . lte = criteria . totalPrizesTo
274
+ }
275
+ boolQuery . push ( { range : { 'overview.totalPrizes' : prizeRangeQuery } } )
276
+ }
277
+
267
278
if ( criteria . useSchedulingAPI ) {
268
279
boolQuery . push ( { match_phrase : { 'legacy.useSchedulingAPI' : criteria . useSchedulingAPI } } )
269
280
}
@@ -557,8 +568,7 @@ async function searchChallenges (currentUser, criteria) {
557
568
}
558
569
}
559
570
560
- // logger.debug(`es Query ${JSON.stringify(esQuery)}`)
561
-
571
+ logger . debug ( `es Query ${ JSON . stringify ( esQuery ) } ` )
562
572
// Search with constructed query
563
573
let docs
564
574
try {
@@ -580,6 +590,7 @@ async function searchChallenges (currentUser, criteria) {
580
590
// Hide privateDescription for non-register challenges
581
591
if ( currentUser ) {
582
592
if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
593
+ result = _ . each ( result , ( val ) => _ . unset ( val , 'billing' ) )
583
594
const ids = await helper . listChallengesByMember ( currentUser . userId )
584
595
result = _ . each ( result , ( val ) => {
585
596
if ( ! _ . includes ( ids , val . id ) ) {
@@ -588,7 +599,11 @@ async function searchChallenges (currentUser, criteria) {
588
599
} )
589
600
}
590
601
} else {
591
- result = _ . each ( result , val => _ . unset ( val , 'privateDescription' ) )
602
+ result = _ . each ( result , val => {
603
+ _ . unset ( val , 'billing' )
604
+ _ . unset ( val , 'privateDescription' )
605
+ return val
606
+ } )
592
607
}
593
608
594
609
if ( criteria . isLightweight === 'true' ) {
@@ -677,7 +692,9 @@ searchChallenges.schema = {
677
692
taskMemberId : Joi . string ( ) ,
678
693
events : Joi . array ( ) . items ( Joi . string ( ) ) ,
679
694
includeAllEvents : Joi . boolean ( ) . default ( true ) ,
680
- useSchedulingAPI : Joi . boolean ( )
695
+ useSchedulingAPI : Joi . boolean ( ) ,
696
+ totalPrizesFrom : Joi . number ( ) . min ( 0 ) ,
697
+ totalPrizesTo : Joi . number ( ) . min ( 0 )
681
698
} ) . unknown ( true )
682
699
}
683
700
@@ -829,6 +846,12 @@ async function createChallenge (currentUser, challenge) {
829
846
}
830
847
challenge . name = xss ( challenge . name )
831
848
challenge . description = xss ( challenge . description )
849
+ if ( ! challenge . status ) {
850
+ challenge . status = constants . challengeStatuses . New
851
+ }
852
+ if ( ! challenge . startDate ) {
853
+ challenge . startDate = new Date ( )
854
+ }
832
855
if ( challenge . status === constants . challengeStatuses . Active ) {
833
856
throw new errors . BadRequestError ( 'You cannot create an Active challenge. Please create a Draft challenge and then change the status to Active.' )
834
857
}
@@ -837,6 +860,13 @@ async function createChallenge (currentUser, challenge) {
837
860
_ . set ( challenge , 'legacy.directProjectId' , directProjectId )
838
861
}
839
862
const { track, type } = await validateChallengeData ( challenge )
863
+ const { billingAccountId, markup } = await helper . getProjectBillingInformation ( _ . get ( challenge , 'projectId' ) )
864
+ if ( billingAccountId && _ . isUndefined ( _ . get ( challenge , 'billing.billingAccountId' ) ) ) {
865
+ _ . set ( challenge , 'billing.billingAccountId' , billingAccountId )
866
+ }
867
+ if ( markup && _ . isUndefined ( _ . get ( challenge , 'billing.markup' ) ) ) {
868
+ _ . set ( challenge , 'billing.markup' , markup )
869
+ }
840
870
if ( _ . get ( type , 'isTask' ) ) {
841
871
_ . set ( challenge , 'task.isTask' , true )
842
872
if ( _ . isUndefined ( _ . get ( challenge , 'task.isAssigned' ) ) ) {
@@ -851,6 +881,7 @@ async function createChallenge (currentUser, challenge) {
851
881
if ( challenge . discussions && challenge . discussions . length > 0 ) {
852
882
for ( let i = 0 ; i < challenge . discussions . length ; i += 1 ) {
853
883
challenge . discussions [ i ] . id = uuid ( )
884
+ challenge . discussions [ i ] . name = challenge . discussions [ i ] . name . substring ( 0 , config . FORUM_TITLE_LENGTH_LIMIT )
854
885
}
855
886
}
856
887
if ( challenge . phases && challenge . phases . length > 0 ) {
@@ -982,6 +1013,10 @@ createChallenge.schema = {
982
1013
useSchedulingAPI : Joi . boolean ( ) ,
983
1014
pureV5Task : Joi . boolean ( )
984
1015
} ) ,
1016
+ billing : Joi . object ( ) . keys ( {
1017
+ billingAccountId : Joi . string ( ) ,
1018
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1019
+ } ) . unknown ( true ) ,
985
1020
task : Joi . object ( ) . keys ( {
986
1021
isTask : Joi . boolean ( ) . default ( false ) ,
987
1022
isAssigned : Joi . boolean ( ) . default ( false ) ,
@@ -1026,7 +1061,7 @@ createChallenge.schema = {
1026
1061
projectId : Joi . number ( ) . integer ( ) . positive ( ) . required ( ) ,
1027
1062
legacyId : Joi . number ( ) . integer ( ) . positive ( ) ,
1028
1063
startDate : Joi . date ( ) ,
1029
- status : Joi . string ( ) . valid ( _ . values ( constants . challengeStatuses ) ) . required ( ) ,
1064
+ status : Joi . string ( ) . valid ( _ . values ( constants . challengeStatuses ) ) ,
1030
1065
groups : Joi . array ( ) . items ( Joi . optionalId ( ) ) . unique ( ) ,
1031
1066
// gitRepoURLs: Joi.array().items(Joi.string().uri()),
1032
1067
terms : Joi . array ( ) . items ( Joi . object ( ) . keys ( {
@@ -1097,12 +1132,14 @@ async function getChallenge (currentUser, id) {
1097
1132
let memberChallengeIds
1098
1133
if ( currentUser ) {
1099
1134
if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
1135
+ _ . unset ( challenge , 'billing' )
1100
1136
memberChallengeIds = await helper . listChallengesByMember ( currentUser . userId )
1101
1137
if ( ! _ . includes ( memberChallengeIds , challenge . id ) ) {
1102
1138
_ . unset ( challenge , 'privateDescription' )
1103
1139
}
1104
1140
}
1105
1141
} else {
1142
+ _ . unset ( challenge , 'billing' )
1106
1143
_ . unset ( challenge , 'privateDescription' )
1107
1144
}
1108
1145
@@ -1203,25 +1240,28 @@ async function update (currentUser, challengeId, data, isFull) {
1203
1240
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
1204
1241
1205
1242
const challenge = await helper . getById ( 'Challenge' , challengeId )
1206
- // FIXME: Tech Debt
1207
- let billingAccountId
1243
+ const { billingAccountId, markup } = await helper . getProjectBillingInformation ( _ . get ( challenge , 'projectId' ) )
1244
+ if ( billingAccountId && _ . isUndefined ( _ . get ( challenge , 'billing.billingAccountId' ) ) ) {
1245
+ _ . set ( data , 'billing.billingAccountId' , billingAccountId )
1246
+ }
1247
+ if ( markup && _ . isUndefined ( _ . get ( challenge , 'billing.markup' ) ) ) {
1248
+ _ . set ( data , 'billing.markup' , markup )
1249
+ }
1208
1250
if ( data . status ) {
1209
1251
if ( data . status === constants . challengeStatuses . Active ) {
1210
- if ( ! _ . get ( challenge , 'legacy.pureV5Task' ) && _ . isUndefined ( _ . get ( challenge , 'legacy.directProjectId ' ) ) ) {
1252
+ if ( ! _ . get ( challenge , 'legacy.pureV5Task' ) && _ . isUndefined ( _ . get ( challenge , 'legacyId ' ) ) ) {
1211
1253
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.' )
1212
1254
}
1213
- billingAccountId = await helper . getProjectBillingAccount ( _ . get ( challenge , 'legacy.directProjectId' ) )
1214
1255
// if activating a challenge, the challenge must have a billing account id
1215
1256
if ( ( ! billingAccountId || billingAccountId === null ) &&
1216
1257
challenge . status === constants . challengeStatuses . Draft ) {
1217
- throw new errors . BadRequestError ( 'Cannot Activate this project, it has no active billing accounts .' )
1258
+ throw new errors . BadRequestError ( 'Cannot Activate this project, it has no active billing account .' )
1218
1259
}
1219
1260
}
1220
1261
if ( data . status === constants . challengeStatuses . Completed ) {
1221
1262
if ( challenge . status !== constants . challengeStatuses . Active ) {
1222
1263
throw new errors . BadRequestError ( 'You cannot mark a Draft challenge as Completed' )
1223
1264
}
1224
- billingAccountId = await helper . getProjectBillingAccount ( _ . get ( challenge , 'legacy.directProjectId' ) )
1225
1265
}
1226
1266
}
1227
1267
@@ -1246,6 +1286,12 @@ async function update (currentUser, challengeId, data, isFull) {
1246
1286
_ . extend ( challenge . legacy , data . legacy )
1247
1287
}
1248
1288
1289
+ if ( ! _ . isUndefined ( challenge . billing ) && ! _ . isUndefined ( data . billing ) ) {
1290
+ _ . extend ( challenge . billing , data . billing )
1291
+ } else if ( _ . isUndefined ( challenge . billing ) && ! _ . isUndefined ( data . billing ) ) {
1292
+ challenge . billing = data . billing
1293
+ }
1294
+
1249
1295
await helper . ensureUserCanModifyChallenge ( currentUser , challenge )
1250
1296
1251
1297
// check groups access to be updated group values
@@ -1414,6 +1460,9 @@ async function update (currentUser, challengeId, data, isFull) {
1414
1460
_ . intersection ( oldIds , newIds ) . length !== value . length ) {
1415
1461
op = '$PUT'
1416
1462
}
1463
+ } else if ( key === 'billing' || key === 'legacy' ) {
1464
+ // make sure that's always being udpated
1465
+ op = '$PUT'
1417
1466
} else if ( _ . isUndefined ( challenge [ key ] ) || challenge [ key ] !== value ) {
1418
1467
op = '$PUT'
1419
1468
}
@@ -1609,11 +1658,7 @@ async function update (currentUser, challengeId, data, isFull) {
1609
1658
1610
1659
// post bus event
1611
1660
logger . debug ( `Post Bus Event: ${ constants . Topics . ChallengeUpdated } ${ JSON . stringify ( challenge ) } ` )
1612
- const busEventPayload = { ...challenge }
1613
- if ( billingAccountId ) {
1614
- busEventPayload . billingAccountId = billingAccountId
1615
- }
1616
- await helper . postBusEvent ( constants . Topics . ChallengeUpdated , busEventPayload )
1661
+ await helper . postBusEvent ( constants . Topics . ChallengeUpdated , challenge )
1617
1662
if ( phasesHaveBeenModified === true && _ . get ( challenge , 'legacy.useSchedulingAPI' ) ) {
1618
1663
await helper . postBusEvent ( config . SCHEDULING_TOPIC , { id : challengeId } )
1619
1664
}
@@ -1687,6 +1732,12 @@ function sanitizeChallenge (challenge) {
1687
1732
'pureV5Task'
1688
1733
] )
1689
1734
}
1735
+ if ( challenge . billing ) {
1736
+ sanitized . billing = _ . pick ( challenge . billing , [
1737
+ 'billingAccountId' ,
1738
+ 'markup'
1739
+ ] )
1740
+ }
1690
1741
if ( challenge . metadata ) {
1691
1742
sanitized . metadata = _ . map ( challenge . metadata , meta => _ . pick ( meta , [ 'name' , 'value' ] ) )
1692
1743
}
@@ -1706,7 +1757,10 @@ function sanitizeChallenge (challenge) {
1706
1757
sanitized . winners = _ . map ( challenge . winners , winner => _ . pick ( winner , [ 'userId' , 'handle' , 'placement' ] ) )
1707
1758
}
1708
1759
if ( challenge . discussions ) {
1709
- sanitized . discussions = _ . map ( challenge . discussions , discussion => _ . pick ( discussion , [ 'id' , 'provider' , 'name' , 'type' , 'url' , 'options' ] ) )
1760
+ sanitized . discussions = _ . map ( challenge . discussions , discussion => ( {
1761
+ ..._ . pick ( discussion , [ 'id' , 'provider' , 'name' , 'type' , 'url' , 'options' ] ) ,
1762
+ name : _ . get ( discussion , 'name' , '' ) . substring ( 0 , config . FORUM_TITLE_LENGTH_LIMIT )
1763
+ } ) )
1710
1764
}
1711
1765
if ( challenge . terms ) {
1712
1766
sanitized . terms = _ . map ( challenge . terms , term => _ . pick ( term , [ 'id' , 'roleId' ] ) )
@@ -1743,6 +1797,10 @@ fullyUpdateChallenge.schema = {
1743
1797
useSchedulingAPI : Joi . boolean ( ) ,
1744
1798
pureV5Task : Joi . boolean ( )
1745
1799
} ) . unknown ( true ) ,
1800
+ billing : Joi . object ( ) . keys ( {
1801
+ billingAccountId : Joi . string ( ) ,
1802
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1803
+ } ) . unknown ( true ) ,
1746
1804
task : Joi . object ( ) . keys ( {
1747
1805
isTask : Joi . boolean ( ) . default ( false ) ,
1748
1806
isAssigned : Joi . boolean ( ) . default ( false ) ,
@@ -1845,6 +1903,10 @@ partiallyUpdateChallenge.schema = {
1845
1903
isAssigned : Joi . boolean ( ) . default ( false ) ,
1846
1904
memberId : Joi . string ( ) . allow ( null )
1847
1905
} ) ,
1906
+ billing : Joi . object ( ) . keys ( {
1907
+ billingAccountId : Joi . string ( ) ,
1908
+ markup : Joi . number ( ) . min ( 0 ) . max ( 100 )
1909
+ } ) . unknown ( true ) ,
1848
1910
trackId : Joi . optionalId ( ) ,
1849
1911
typeId : Joi . optionalId ( ) ,
1850
1912
name : Joi . string ( ) ,
0 commit comments