@@ -41,7 +41,7 @@ import ChallengeScheduler from "../util/ChallengeScheduler";
41
41
import { BAValidation , lockConsumeAmount } from "../api/BillingAccount" ;
42
42
import { ChallengeEstimator } from "../util/ChallengeEstimator" ;
43
43
import { V5_TRACK_IDS_TO_NAMES , V5_TYPE_IDS_TO_NAMES } from "../common/ConversionMap" ;
44
- import PaymentCreator , { PaymentDetail } from "../util/PaymentCreator " ;
44
+ import WalletApi , { PaymentDetail } from "../util/WalletApi " ;
45
45
import { getChallengeResources } from "../api/v5Api" ;
46
46
import m2mToken from "../helpers/MachineToMachineToken" ;
47
47
@@ -909,11 +909,21 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
909
909
title : string ,
910
910
payments : UpdateChallengeInputForACL_PaymentACL [ ]
911
911
) : Promise < number > {
912
+ const token = await m2mToken . getM2MToken ( ) ;
913
+
912
914
console . log (
913
915
`Generating payments for challenge ${ challengeId } , ${ title } with payments ${ JSON . stringify (
914
916
payments
915
917
) } for challenge type ${ challengeType } `
916
918
) ;
919
+
920
+ // Check if payment already exists
921
+ const existingPayments = await WalletApi . getPaymentsByChallengeId ( challengeId , token ) ;
922
+ if ( existingPayments . length > 0 ) {
923
+ console . log ( `Payments already exist for challenge ${ challengeId } , skipping payment generation` ) ;
924
+ return 0 ;
925
+ }
926
+
917
927
let totalAmount = 0 ;
918
928
// TODO: Make this list exhaustive
919
929
const mapType = ( type : string ) => {
@@ -934,43 +944,22 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
934
944
const nPayments = payments . length ;
935
945
for ( let i = 0 ; i < nPayments ; i ++ ) {
936
946
const payment = payments [ i ] ;
937
- let details : PaymentDetail [ ] = [ ] ;
947
+ let details : PaymentDetail [ ] = [
948
+ {
949
+ totalAmount : payment . amount ,
950
+ grossAmount : payment . amount ,
951
+ installmentNumber : 1 ,
952
+ currency : "USD" ,
953
+ } ,
954
+ ] ;
938
955
939
956
let description = title ;
940
957
941
- // TODO: Make this a more dynamic calculation
942
- // TODO: splitRatio should be from challenge data
943
958
if ( payment . type === "placement" ) {
944
- const grossAmount1 = payment . amount * 0.75 ;
945
- const grossAmount2 = payment . amount * 0.25 ;
946
- details = [
947
- {
948
- totalAmount : payment . amount ,
949
- grossAmount : grossAmount1 ,
950
- installmentNumber : 1 ,
951
- currency : "USD" ,
952
- } ,
953
- {
954
- totalAmount : payment . amount ,
955
- grossAmount : grossAmount2 ,
956
- installmentNumber : 2 ,
957
- currency : "USD" ,
958
- } ,
959
- ] ;
960
-
961
959
description =
962
960
challengeType != "Task"
963
961
? `${ title } - ${ this . placeToOrdinal ( placementMap [ payment . handle ] ) } Place`
964
962
: title ;
965
- } else {
966
- details = [
967
- {
968
- totalAmount : payment . amount ,
969
- grossAmount : payment . amount ,
970
- installmentNumber : 1 ,
971
- currency : "USD" ,
972
- } ,
973
- ] ;
974
963
}
975
964
976
965
totalAmount += payment . amount ;
@@ -995,7 +984,7 @@ class ChallengeDomain extends CoreOperations<Challenge, CreateChallengeInput> {
995
984
}
996
985
997
986
console . log ( "Generate payment with payload" , payload ) ;
998
- await PaymentCreator . createPayment ( payload , await m2mToken . getM2MToken ( ) ) ;
987
+ await WalletApi . createPayment ( payload , token ) ;
999
988
}
1000
989
1001
990
return totalAmount ;
0 commit comments