@@ -941,7 +941,7 @@ async function createChallenge(currentUser, challenge, userToken) {
941
941
console . log ( "TYPE" , prizeTypeTmp ) ;
942
942
if ( challenge . legacy . selfService ) {
943
943
// if self-service, create a new project (what about if projectId is provided in the payload? confirm with business!)
944
- if ( ! challenge . projectId ) {
944
+ if ( ! challenge . projectId && challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId ) ) {
945
945
const selfServiceProjectName = `Self service - ${ currentUser . handle } - ${ challenge . name } ` ;
946
946
challenge . projectId = await helper . createSelfServiceProject (
947
947
selfServiceProjectName ,
@@ -963,14 +963,18 @@ async function createChallenge(currentUser, challenge, userToken) {
963
963
}
964
964
965
965
/** Ensure project exists, and set direct project id, billing account id & markup */
966
- const { projectId } = challenge ;
966
+ if ( challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId ) ) {
967
+ const { projectId } = challenge ;
967
968
968
- const { directProjectId } = await projectHelper . getProject ( projectId , currentUser ) ;
969
- const { billingAccountId, markup } = await projectHelper . getProjectBillingInformation ( projectId ) ;
969
+ const { directProjectId } = await projectHelper . getProject ( projectId , currentUser ) ;
970
+ const { billingAccountId, markup } = await projectHelper . getProjectBillingInformation (
971
+ projectId
972
+ ) ;
970
973
971
- _ . set ( challenge , "legacy.directProjectId" , directProjectId ) ;
972
- _ . set ( challenge , "billing.billingAccountId" , billingAccountId ) ;
973
- _ . set ( challenge , "billing.markup" , markup || 0 ) ;
974
+ _ . set ( challenge , "legacy.directProjectId" , directProjectId ) ;
975
+ _ . set ( challenge , "billing.billingAccountId" , billingAccountId ) ;
976
+ _ . set ( challenge , "billing.markup" , markup || 0 ) ;
977
+ }
974
978
975
979
if ( ! _ . isUndefined ( _ . get ( challenge , "legacy.reviewType" ) ) ) {
976
980
_ . set ( challenge , "legacy.reviewType" , _ . toUpper ( _ . get ( challenge , "legacy.reviewType" ) ) ) ;
@@ -1510,19 +1514,22 @@ async function updateChallenge(currentUser, challengeId, data) {
1510
1514
convertPrizeSetValuesToDollars ( challenge . prizeSets , challenge . overview ) ;
1511
1515
}
1512
1516
1513
- const projectId = _ . get ( challenge , "projectId" ) ;
1517
+ let projectId , billingAccountId , markup ;
1518
+ if ( challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId ) ) {
1519
+ projectId = _ . get ( challenge , "projectId" ) ;
1514
1520
1515
- const { billingAccountId, markup } = await projectHelper . getProjectBillingInformation ( projectId ) ;
1521
+ ( { billingAccountId, markup } = await projectHelper . getProjectBillingInformation ( projectId ) ) ;
1516
1522
1517
- if ( billingAccountId && _ . isUndefined ( _ . get ( challenge , "billing.billingAccountId" ) ) ) {
1518
- _ . set ( data , "billing.billingAccountId" , billingAccountId ) ;
1519
- _ . set ( data , "billing.markup" , markup || 0 ) ;
1520
- }
1523
+ if ( billingAccountId && _ . isUndefined ( _ . get ( challenge , "billing.billingAccountId" ) ) ) {
1524
+ _ . set ( data , "billing.billingAccountId" , billingAccountId ) ;
1525
+ _ . set ( data , "billing.markup" , markup || 0 ) ;
1526
+ }
1521
1527
1522
- // Make sure the user cannot change the direct project ID
1523
- if ( data . legacy ) {
1524
- data . legacy = _ . assign ( { } , challenge . legacy , data . legacy ) ;
1525
- _ . set ( data , "legacy.directProjectId" , challenge . legacy . directProjectId ) ;
1528
+ // Make sure the user cannot change the direct project ID
1529
+ if ( data . legacy ) {
1530
+ data . legacy = _ . assign ( { } , challenge . legacy , data . legacy ) ;
1531
+ _ . set ( data , "legacy.directProjectId" , challenge . legacy . directProjectId ) ;
1532
+ }
1526
1533
}
1527
1534
1528
1535
// Remove fields from data that are not allowed to be updated and that match the existing challenge
@@ -1565,7 +1572,8 @@ async function updateChallenge(currentUser, challengeId, data) {
1565
1572
if (
1566
1573
( data . status === constants . challengeStatuses . Approved ||
1567
1574
data . status === constants . challengeStatuses . Active ) &&
1568
- challenge . status !== constants . challengeStatuses . Active
1575
+ challenge . status !== constants . challengeStatuses . Active &&
1576
+ challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId )
1569
1577
) {
1570
1578
try {
1571
1579
const selfServiceProjectName = `Self service - ${ challenge . createdBy } - ${ challenge . name } ` ;
@@ -1601,7 +1609,10 @@ async function updateChallenge(currentUser, challengeId, data) {
1601
1609
}
1602
1610
}
1603
1611
1604
- if ( data . status === constants . challengeStatuses . Draft ) {
1612
+ if (
1613
+ data . status === constants . challengeStatuses . Draft &&
1614
+ challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId )
1615
+ ) {
1605
1616
try {
1606
1617
await helper . updateSelfServiceProjectInfo (
1607
1618
projectId ,
@@ -1614,8 +1625,9 @@ async function updateChallenge(currentUser, challengeId, data) {
1614
1625
}
1615
1626
1616
1627
if (
1617
- data . status === constants . challengeStatuses . CancelledRequirementsInfeasible ||
1618
- data . status === constants . challengeStatuses . CancelledPaymentFailed
1628
+ ( data . status === constants . challengeStatuses . CancelledRequirementsInfeasible ||
1629
+ data . status === constants . challengeStatuses . CancelledPaymentFailed ) &&
1630
+ challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId )
1619
1631
) {
1620
1632
try {
1621
1633
await helper . cancelProject ( challenge . projectId , data . cancelReason , currentUser ) ;
@@ -1635,7 +1647,8 @@ async function updateChallenge(currentUser, challengeId, data) {
1635
1647
// if activating a challenge, the challenge must have a billing account id
1636
1648
if (
1637
1649
( ! billingAccountId || billingAccountId === null ) &&
1638
- challenge . status === constants . challengeStatuses . Draft
1650
+ challenge . status === constants . challengeStatuses . Draft &&
1651
+ challengeHelper . isProjectIdRequired ( challenge . timelineTemplateId )
1639
1652
) {
1640
1653
throw new errors . BadRequestError (
1641
1654
"Cannot Activate this project, it has no active billing account."
@@ -2152,7 +2165,7 @@ updateChallenge.schema = {
2152
2165
. unknown ( true )
2153
2166
)
2154
2167
. optional ( ) ,
2155
- overview : Joi . any ( ) . forbidden ( ) ,
2168
+ overview : Joi . any ( ) . forbidden ( )
2156
2169
} )
2157
2170
. unknown ( true )
2158
2171
. required ( ) ,
@@ -2479,6 +2492,128 @@ async function indexChallengeAndPostToKafka(updatedChallenge, track, type) {
2479
2492
} ) ;
2480
2493
}
2481
2494
2495
+ async function updateLegacyPayout ( currentUser , challengeId , data ) {
2496
+ console . log ( `Update legacy payment data for challenge: ${ challengeId } with data: ` , data ) ;
2497
+ const challenge = await challengeDomain . lookup ( getLookupCriteria ( "id" , challengeId ) ) ;
2498
+
2499
+ // SQL qurey to fetch the payment and payment_detail record
2500
+ let sql = `SELECT * FROM informixoltp:payment p
2501
+ INNER JOIN informixoltp:payment_detail pd ON p.most_recent_detail_id = pd.payment_detail_id
2502
+ WHERE p.user_id = ${ data . userId } AND` ;
2503
+
2504
+ if ( challenge . legacyId != null ) {
2505
+ sql += ` pd.component_project_id = ${ challenge . legacyId } ` ;
2506
+ } else {
2507
+ sql += ` pd.jira_issue_id = \'${ challengeId } \'` ;
2508
+ }
2509
+
2510
+ sql += " ORDER BY pd.payment_detail_id ASC" ;
2511
+
2512
+ console . log ( "Fetch legacy payment detail: " , sql ) ;
2513
+
2514
+ const result = await aclQueryDomain . rawQuery ( { sql } ) ;
2515
+ let updateClauses = [ `date_modified = current` ] ;
2516
+
2517
+ const statusMap = {
2518
+ Paid : 53 ,
2519
+ OnHold : 55 ,
2520
+ OnHoldAdmin : 55 ,
2521
+ Owed : 56 ,
2522
+ Cancelled : 65 ,
2523
+ EnteredIntoPaymentSystem : 70 ,
2524
+ } ;
2525
+
2526
+ if ( data . status != null ) {
2527
+ updateClauses . push ( `payment_status_id = ${ statusMap [ data . status ] } ` ) ;
2528
+ if ( data . status === "Paid" ) {
2529
+ updateClauses . push ( `date_paid = '${ data . datePaid } '` ) ;
2530
+ } else {
2531
+ updateClauses . push ( "date_paid = null" ) ;
2532
+ }
2533
+ }
2534
+
2535
+ if ( data . releaseDate != null ) {
2536
+ updateClauses . push ( `date_due = '${ data . releaseDate } '` ) ;
2537
+ }
2538
+
2539
+ const paymentDetailIds = result . rows . map (
2540
+ ( row ) => row . fields . find ( ( field ) => field . key === "payment_detail_id" ) . value
2541
+ ) ;
2542
+
2543
+ if ( data . amount != null ) {
2544
+ updateClauses . push ( `total_amount = ${ data . amount } ` ) ;
2545
+ if ( paymentDetailIds . length === 1 ) {
2546
+ updateClauses . push ( `net_amount = ${ data . amount } ` ) ;
2547
+ updateClauses . push ( `gross_amount = ${ data . amount } ` ) ;
2548
+ }
2549
+ }
2550
+
2551
+ if ( paymentDetailIds . length === 0 ) {
2552
+ return {
2553
+ success : false ,
2554
+ message : "No payment detail record found" ,
2555
+ } ;
2556
+ }
2557
+
2558
+ const whereClause = [ `payment_detail_id IN (${ paymentDetailIds . join ( "," ) } )` ] ;
2559
+
2560
+ const updateQuery = `UPDATE informixoltp:payment_detail SET ${ updateClauses . join (
2561
+ ", "
2562
+ ) } WHERE ${ whereClause . join ( " AND " ) } `;
2563
+
2564
+ console . log ( "Update Clauses" , updateClauses ) ;
2565
+ console . log ( "Update Query" , updateQuery ) ;
2566
+
2567
+ await aclQueryDomain . rawQuery ( { sql : updateQuery } ) ;
2568
+
2569
+ if ( data . amount != null ) {
2570
+ if ( paymentDetailIds . length > 1 ) {
2571
+ const amountInCents = data . amount * 100 ;
2572
+
2573
+ const split1Cents = Math . round ( amountInCents * 0.75 ) ;
2574
+ const split2Cents = amountInCents - split1Cents ;
2575
+
2576
+ const split1Dollars = Number ( ( split1Cents / 100 ) . toFixed ( 2 ) ) ;
2577
+ const split2Dollars = Number ( ( split2Cents / 100 ) . toFixed ( 2 ) ) ;
2578
+
2579
+ const paymentUpdateQueries = paymentDetailIds . map ( ( paymentDetailId , index ) => {
2580
+ let amt = 0 ;
2581
+ if ( index === 0 ) {
2582
+ amt = split1Dollars ;
2583
+ }
2584
+ if ( index === 1 ) {
2585
+ amt = split2Dollars ;
2586
+ }
2587
+
2588
+ return `UPDATE informixoltp:payment_detail SET date_modified = CURRENT, net_amount = ${ amt } , gross_amount = ${ amt } WHERE payment_detail_id = ${ paymentDetailId } ` ;
2589
+ } ) ;
2590
+
2591
+ console . log ( "Payment Update Queries" , paymentUpdateQueries ) ;
2592
+
2593
+ await Promise . all (
2594
+ paymentUpdateQueries . map ( ( query ) => aclQueryDomain . rawQuery ( { sql : query } ) )
2595
+ ) ;
2596
+ }
2597
+ }
2598
+
2599
+ return {
2600
+ success : true ,
2601
+ message : "Successfully updated legacy payout" ,
2602
+ } ;
2603
+ }
2604
+ updateLegacyPayout . schema = {
2605
+ currentUser : Joi . any ( ) ,
2606
+ challengeId : Joi . id ( ) ,
2607
+ data : Joi . object ( )
2608
+ . keys ( {
2609
+ userId : Joi . number ( ) . integer ( ) . positive ( ) . required ( ) ,
2610
+ amount : Joi . number ( ) . allow ( null ) ,
2611
+ status : Joi . string ( ) . allow ( null ) ,
2612
+ datePaid : Joi . string ( ) . allow ( null ) ,
2613
+ releaseDate : Joi . string ( ) . allow ( null ) ,
2614
+ } )
2615
+ } ;
2616
+
2482
2617
/**
2483
2618
* Get SRM Schedule
2484
2619
* @param {Object } criteria the criteria
@@ -2543,6 +2678,7 @@ module.exports = {
2543
2678
getChallenge,
2544
2679
updateChallenge,
2545
2680
deleteChallenge,
2681
+ updateLegacyPayout,
2546
2682
getChallengeStatistics,
2547
2683
sendNotifications,
2548
2684
advancePhase,
0 commit comments