@@ -42,6 +42,7 @@ const {
42
42
convertToISOString,
43
43
} = require ( "../common/challenge-helper" ) ;
44
44
const deepEqual = require ( "deep-equal" ) ;
45
+ const { cloneDeep } = require ( "lodash" ) ;
45
46
46
47
const challengeDomain = new ChallengeDomain ( GRPC_CHALLENGE_SERVER_HOST , GRPC_CHALLENGE_SERVER_PORT ) ;
47
48
@@ -1492,23 +1493,8 @@ async function validateWinners(winners, challengeId) {
1492
1493
async function updateChallenge ( currentUser , challengeId , data ) {
1493
1494
const challenge = await challengeDomain . lookup ( getLookupCriteria ( "id" , challengeId ) ) ;
1494
1495
1495
- // Remove fields from data that are not allowed to be updated and that match the existing challenge
1496
- data = sanitizeData ( sanitizeChallenge ( data ) , challenge ) ;
1497
- console . debug ( "Sanitized Data:" , data ) ;
1498
-
1499
- if ( data . phases != null && data . startDate == null ) {
1500
- data . startDate = challenge . startDate ;
1501
- }
1502
-
1503
- validateChallengeUpdateRequest ( currentUser , challenge , data ) ;
1504
-
1505
1496
const projectId = _ . get ( challenge , "projectId" ) ;
1506
1497
1507
- let sendActivationEmail = false ;
1508
- let sendSubmittedEmail = false ;
1509
- let sendCompletedEmail = false ;
1510
- let sendRejectedEmail = false ;
1511
-
1512
1498
const { billingAccountId, markup } = await projectHelper . getProjectBillingInformation ( projectId ) ;
1513
1499
1514
1500
if ( billingAccountId && _ . isUndefined ( _ . get ( challenge , "billing.billingAccountId" ) ) ) {
@@ -1517,10 +1503,22 @@ async function updateChallenge(currentUser, challengeId, data) {
1517
1503
}
1518
1504
1519
1505
// Make sure the user cannot change the direct project ID
1520
- if ( data . legacy && data . legacy . directProjectId ) {
1521
- _ . unset ( data , "legacy.directProjectId" ) ;
1506
+ if ( data . legacy ) {
1507
+ data . legacy = _ . assign ( { } , challenge . legacy , data . legacy )
1508
+ _ . set ( data , "legacy.directProjectId" , challenge . legacy . directProjectId ) ;
1522
1509
}
1523
1510
1511
+ // Remove fields from data that are not allowed to be updated and that match the existing challenge
1512
+ data = sanitizeData ( sanitizeChallenge ( data ) , challenge ) ;
1513
+ console . debug ( "Sanitized Data:" , data ) ;
1514
+
1515
+ validateChallengeUpdateRequest ( currentUser , challenge , data ) ;
1516
+
1517
+ let sendActivationEmail = false ;
1518
+ let sendSubmittedEmail = false ;
1519
+ let sendCompletedEmail = false ;
1520
+ let sendRejectedEmail = false ;
1521
+
1524
1522
/* BEGIN self-service stuffs */
1525
1523
1526
1524
// TODO: At some point in the future this should be moved to a Self-Service Challenge Helper
@@ -1589,11 +1587,24 @@ async function updateChallenge(currentUser, challengeId, data) {
1589
1587
logger . debug ( `There was an error trying to update the project: ${ e . message } ` ) ;
1590
1588
}
1591
1589
}
1590
+
1591
+ if (
1592
+ data . status === constants . challengeStatuses . CancelledRequirementsInfeasible ||
1593
+ data . status === constants . challengeStatuses . CancelledPaymentFailed
1594
+ ) {
1595
+ try {
1596
+ await helper . cancelProject ( challenge . projectId , data . cancelReason , currentUser ) ;
1597
+ } catch ( e ) {
1598
+ logger . debug ( `There was an error trying to cancel the project: ${ e . message } ` ) ;
1599
+ }
1600
+ sendRejectedEmail = true ;
1601
+ }
1592
1602
}
1593
1603
1594
1604
/* END self-service stuffs */
1595
1605
1596
1606
let isChallengeBeingActivated = false ;
1607
+ let isChallengeBeingCancelled = false ;
1597
1608
if ( data . status ) {
1598
1609
if ( data . status === constants . challengeStatuses . Active ) {
1599
1610
if (
@@ -1619,16 +1630,18 @@ async function updateChallenge(currentUser, challengeId, data) {
1619
1630
}
1620
1631
}
1621
1632
1622
- if (
1623
- data . status === constants . challengeStatuses . CancelledRequirementsInfeasible ||
1624
- data . status === constants . challengeStatuses . CancelledPaymentFailed
1625
- ) {
1626
- try {
1627
- await helper . cancelProject ( challenge . projectId , cancelReason , currentUser ) ;
1628
- } catch ( e ) {
1629
- logger . debug ( `There was an error trying to cancel the project: ${ e . message } ` ) ;
1630
- }
1631
- sendRejectedEmail = true ;
1633
+ if ( _ . includes ( [
1634
+ constants . challengeStatuses . Cancelled ,
1635
+ constants . challengeStatuses . CancelledRequirementsInfeasible ,
1636
+ constants . challengeStatuses . CancelledPaymentFailed ,
1637
+ constants . challengeStatuses . CancelledFailedReview ,
1638
+ constants . challengeStatuses . CancelledFailedScreening ,
1639
+ constants . challengeStatuses . CancelledZeroSubmissions ,
1640
+ constants . challengeStatuses . CancelledWinnerUnresponsive ,
1641
+ constants . challengeStatuses . CancelledClientRequest ,
1642
+ constants . challengeStatuses . CancelledZeroRegistrations ,
1643
+ ] , data . status ) ) {
1644
+ isChallengeBeingCancelled = true ;
1632
1645
}
1633
1646
1634
1647
if ( data . status === constants . challengeStatuses . Completed ) {
@@ -1723,9 +1736,9 @@ async function updateChallenge(currentUser, challengeId, data) {
1723
1736
1724
1737
let phasesUpdated = false ;
1725
1738
if (
1726
- ( data . phases && data . phases . length > 0 ) ||
1739
+ ( ( data . phases && data . phases . length > 0 ) ||
1727
1740
isChallengeBeingActivated ||
1728
- timelineTemplateChanged
1741
+ timelineTemplateChanged ) && ! isChallengeBeingCancelled
1729
1742
) {
1730
1743
if (
1731
1744
challenge . status === constants . challengeStatuses . Completed ||
@@ -1754,6 +1767,10 @@ async function updateChallenge(currentUser, challengeId, data) {
1754
1767
phasesUpdated = true ;
1755
1768
data . phases = newPhases ;
1756
1769
}
1770
+ if ( isChallengeBeingCancelled && challenge . phases && challenge . phases . length > 0 ) {
1771
+ data . phases = phaseHelper . handlePhasesAfterCancelling ( challenge . phases ) ;
1772
+ phasesUpdated = true ;
1773
+ }
1757
1774
if ( phasesUpdated || data . startDate ) {
1758
1775
data . startDate = convertToISOString ( _ . min ( _ . map ( data . phases , "scheduledStartDate" ) ) ) ;
1759
1776
}
@@ -1855,8 +1872,7 @@ async function updateChallenge(currentUser, challengeId, data) {
1855
1872
}
1856
1873
1857
1874
try {
1858
- const updateInput = sanitizeRepeatedFieldsInUpdateRequest ( data ) ;
1859
-
1875
+ const updateInput = sanitizeRepeatedFieldsInUpdateRequest ( _ . omit ( data , [ 'cancelReason' ] ) ) ;
1860
1876
if ( ! _ . isEmpty ( updateInput ) ) {
1861
1877
const grpcMetadata = new GrpcMetadata ( ) ;
1862
1878
@@ -1969,7 +1985,7 @@ updateChallenge.schema = {
1969
1985
. valid ( _ . values ( constants . reviewTypes ) )
1970
1986
. insensitive ( )
1971
1987
. default ( constants . reviewTypes . Internal ) ,
1972
- confidentialityType : Joi . string ( ) . default ( config . DEFAULT_CONFIDENTIALITY_TYPE ) ,
1988
+ confidentialityType : Joi . string ( ) . allow ( null , '' ) . empty ( null , '' ) . default ( config . DEFAULT_CONFIDENTIALITY_TYPE ) ,
1973
1989
directProjectId : Joi . number ( ) ,
1974
1990
forumId : Joi . number ( ) . integer ( ) ,
1975
1991
isTask : Joi . boolean ( ) ,
0 commit comments