@@ -443,6 +443,9 @@ async function populatePhases (phases, startDate, timelineTemplateId) {
443
443
* @returns {Object } the created challenge
444
444
*/
445
445
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
+ }
446
449
await helper . ensureProjectExist ( challenge . projectId , userToken )
447
450
await validateChallengeData ( challenge )
448
451
if ( challenge . phases && challenge . phases . length > 0 ) {
@@ -466,7 +469,7 @@ async function createChallenge (currentUser, challenge, userToken) {
466
469
// challenge.terms = await helper.validateChallengeTerms(_.union(projectTerms, challenge.terms))
467
470
// TODO - challenge terms returned from projects api don't have a role associated
468
471
// this will need to be updated to associate project terms with a roleId
469
- challenge . terms = await helper . validateChallengeTerms ( challenge . terms )
472
+ challenge . terms = await helper . validateChallengeTerms ( challenge . terms || [ ] )
470
473
471
474
if ( challenge . phases && challenge . phases . length > 0 ) {
472
475
challenge . endDate = helper . calculateChallengeEndDate ( challenge )
@@ -755,6 +758,18 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
755
758
// helper.ensureNoDuplicateOrNullElements(data.gitRepoURLs, 'gitRepoURLs')
756
759
757
760
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
+ }
758
773
759
774
// FIXME: Tech Debt
760
775
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) {
1091
1106
1092
1107
// post bus event
1093
1108
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 )
1095
1114
1096
1115
if ( challenge . phases && challenge . phases . length > 0 ) {
1097
1116
challenge . currentPhase = challenge . phases . slice ( ) . reverse ( ) . find ( phase => phase . isOpen )
0 commit comments