@@ -5,15 +5,15 @@ const models = require('../models')
5
5
const { getV3MemberDetailsByHandle, getChallenge, getChallengeResource, sleep, postEvent } = require ( '../common/helper' )
6
6
const logger = require ( '../common/logger' )
7
7
const { createChallenge, addResourceToChallenge, activateChallenge, closeChallenge } = require ( './PaymentService' )
8
- const { ChallengeStatus, PaymentProcessingSwitch } = require ( '../../app-constants' )
8
+ const { ChallengeStatus, PaymentSchedulerStatus , PaymentProcessingSwitch } = require ( '../../app-constants' )
9
9
10
10
const WorkPeriodPayment = models . WorkPeriodPayment
11
11
const WorkPeriod = models . WorkPeriod
12
12
const PaymentScheduler = models . PaymentScheduler
13
13
const {
14
14
SWITCH , BATCH_SIZE , IN_PROGRESS_EXPIRED , MAX_RETRY_COUNT , RETRY_BASE_DELAY , RETRY_MAX_DELAY , PER_REQUEST_MAX_TIME , PER_PAYMENT_MAX_TIME ,
15
15
PER_MINUTE_PAYMENT_MAX_COUNT , PER_MINUTE_CHALLENGE_REQUEST_MAX_COUNT , PER_MINUTE_RESOURCE_REQUEST_MAX_COUNT ,
16
- FIX_DELAY_STEP_1_2 , FIX_DELAY_STEP_2_3 , FIX_DELAY_STEP_3_4
16
+ FIX_DELAY_STEP_CREATE_CHALLENGE , FIX_DELAY_STEP_ASSIGN_MEMBER , FIX_DELAY_STEP_ACTIVATE_CHALLENGE
17
17
} = config . PAYMENT_PROCESSING
18
18
const processStatus = {
19
19
perMin : {
@@ -30,7 +30,6 @@ const processStatus = {
30
30
paymentStartTime : 0 ,
31
31
requestStartTime : 0
32
32
}
33
- const stepEnum = [ 'start-process' , 'create-challenge' , 'assign-member' , 'activate-challenge' , 'get-userId' , 'close-challenge' ]
34
33
const processResult = {
35
34
SUCCESS : 'success' ,
36
35
FAIL : 'fail' ,
@@ -94,21 +93,21 @@ async function processPayment (workPeriodPayment) {
94
93
await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue } )
95
94
}
96
95
// Check whether the number of processed records per minute exceeds the specified number, if it exceeds, wait for the next minute before processing
97
- await checkWait ( stepEnum [ 0 ] )
96
+ await checkWait ( PaymentSchedulerStatus . START_PROCESS )
98
97
localLogger . info ( `Processing workPeriodPayment ${ workPeriodPayment . id } ` , 'processPayment' )
99
98
100
99
const workPeriod = await WorkPeriod . findById ( workPeriodPayment . workPeriodId )
101
100
try {
102
101
if ( ! paymentScheduler ) {
103
102
// 1. create challenge
104
- const challengeId = await withRetry ( createChallenge , [ getCreateChallengeParam ( workPeriod , workPeriodPayment ) ] , validateError , stepEnum [ 1 ] )
105
- paymentScheduler = await PaymentScheduler . create ( { challengeId, step : 1 , workPeriodPaymentId : workPeriodPayment . id , userHandle : workPeriod . userHandle , status : 'in-progress' } )
103
+ const challengeId = await withRetry ( createChallenge , [ getCreateChallengeParam ( workPeriod , workPeriodPayment ) ] , validateError , PaymentSchedulerStatus . CREATE_CHALLENGE )
104
+ paymentScheduler = await PaymentScheduler . create ( { challengeId, step : PaymentSchedulerStatus . CREATE_CHALLENGE , workPeriodPaymentId : workPeriodPayment . id , userHandle : workPeriod . userHandle , status : 'in-progress' } )
106
105
} else {
107
106
// If the paymentScheduler already exists, it means that this is a record caused by an abnormal shutdown
108
107
await setPaymentSchedulerStep ( paymentScheduler )
109
108
}
110
109
// Start from unprocessed step, perform the process step by step
111
- while ( paymentScheduler . step < 5 ) {
110
+ while ( paymentScheduler . step !== PaymentSchedulerStatus . CLOSE_CHALLENGE ) {
112
111
await processStep ( paymentScheduler )
113
112
}
114
113
@@ -118,7 +117,7 @@ async function processPayment (workPeriodPayment) {
118
117
// Update the modified status to es
119
118
await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue } )
120
119
121
- await paymentScheduler . update ( { step : 5 , userId : paymentScheduler . userId , status : 'completed' } )
120
+ await paymentScheduler . update ( { step : PaymentSchedulerStatus . CLOSE_CHALLENGE , userId : paymentScheduler . userId , status : 'completed' } )
122
121
123
122
localLogger . info ( `Processed workPeriodPayment ${ workPeriodPayment . id } successfully` , 'processPayment' )
124
123
return processResult . SUCCESS
@@ -132,7 +131,7 @@ async function processPayment (workPeriodPayment) {
132
131
await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue } )
133
132
134
133
if ( paymentScheduler ) {
135
- await paymentScheduler . update ( { step : 5 , userId : paymentScheduler . userId , status : 'failed' } )
134
+ await paymentScheduler . update ( { step : PaymentSchedulerStatus . CLOSE_CHALLENGE , userId : paymentScheduler . userId , status : 'failed' } )
136
135
}
137
136
localLogger . error ( `Processed workPeriodPayment ${ workPeriodPayment . id } failed` , 'processPayment' )
138
137
return processResult . FAIL
@@ -144,23 +143,23 @@ async function processPayment (workPeriodPayment) {
144
143
* @param {Object } paymentScheduler the payment scheduler
145
144
*/
146
145
async function processStep ( paymentScheduler ) {
147
- if ( paymentScheduler . step === 1 ) {
146
+ if ( paymentScheduler . step === PaymentSchedulerStatus . CREATE_CHALLENGE ) {
148
147
// 2. assign member to the challenge
149
- await withRetry ( addResourceToChallenge , [ paymentScheduler . challengeId , paymentScheduler . userHandle ] , validateError , stepEnum [ 2 ] )
150
- paymentScheduler . step = 2
151
- } else if ( paymentScheduler . step === 2 ) {
148
+ await withRetry ( addResourceToChallenge , [ paymentScheduler . challengeId , paymentScheduler . userHandle ] , validateError , PaymentSchedulerStatus . ASSIGN_MEMBER )
149
+ paymentScheduler . step = PaymentSchedulerStatus . ASSIGN_MEMBER
150
+ } else if ( paymentScheduler . step === PaymentSchedulerStatus . ASSIGN_MEMBER ) {
152
151
// 3. active the challenge
153
- await withRetry ( activateChallenge , [ paymentScheduler . challengeId ] , validateError , stepEnum [ 3 ] )
154
- paymentScheduler . step = 3
155
- } else if ( paymentScheduler . step === 3 ) {
152
+ await withRetry ( activateChallenge , [ paymentScheduler . challengeId ] , validateError , PaymentSchedulerStatus . ACTIVATE_CHALLENGE )
153
+ paymentScheduler . step = PaymentSchedulerStatus . ACTIVATE_CHALLENGE
154
+ } else if ( paymentScheduler . step === PaymentSchedulerStatus . ACTIVATE_CHALLENGE ) {
156
155
// 4.1. get user id
157
- const { userId } = await withRetry ( getV3MemberDetailsByHandle , [ paymentScheduler . userHandle ] , validateError , stepEnum [ 4 ] )
156
+ const { userId } = await withRetry ( getV3MemberDetailsByHandle , [ paymentScheduler . userHandle ] , validateError , PaymentSchedulerStatus . GET_USER_ID )
158
157
paymentScheduler . userId = userId
159
- paymentScheduler . step = 4
160
- } else if ( paymentScheduler . step === 4 ) {
158
+ paymentScheduler . step = PaymentSchedulerStatus . GET_USER_ID
159
+ } else if ( paymentScheduler . step === PaymentSchedulerStatus . GET_USER_ID ) {
161
160
// 4.2. close the challenge
162
- await withRetry ( closeChallenge , [ paymentScheduler . challengeId , paymentScheduler . userId , paymentScheduler . userHandle ] , validateError , stepEnum [ 5 ] )
163
- paymentScheduler . step = 5
161
+ await withRetry ( closeChallenge , [ paymentScheduler . challengeId , paymentScheduler . userId , paymentScheduler . userHandle ] , validateError , PaymentSchedulerStatus . CLOSE_CHALLENGE )
162
+ paymentScheduler . step = PaymentSchedulerStatus . CLOSE_CHALLENGE
164
163
}
165
164
}
166
165
@@ -171,17 +170,17 @@ async function processStep (paymentScheduler) {
171
170
async function setPaymentSchedulerStep ( paymentScheduler ) {
172
171
const challenge = await getChallenge ( paymentScheduler . challengeId )
173
172
if ( SWITCH === PaymentProcessingSwitch . OFF ) {
174
- paymentScheduler . step = 5
173
+ paymentScheduler . step = PaymentSchedulerStatus . CLOSE_CHALLENGE
175
174
} else if ( challenge . status === ChallengeStatus . COMPLETED ) {
176
- paymentScheduler . step = 5
175
+ paymentScheduler . step = PaymentSchedulerStatus . CLOSE_CHALLENGE
177
176
} else if ( challenge . status === ChallengeStatus . ACTIVE ) {
178
- paymentScheduler . step = 3
177
+ paymentScheduler . step = PaymentSchedulerStatus . ACTIVATE_CHALLENGE
179
178
} else {
180
179
const resource = await getChallengeResource ( paymentScheduler . challengeId , paymentScheduler . userHandle , config . ROLE_ID_SUBMITTER )
181
180
if ( resource ) {
182
- paymentScheduler . step = 2
181
+ paymentScheduler . step = PaymentSchedulerStatus . ASSIGN_MEMBER
183
182
} else {
184
- paymentScheduler . step = 1
183
+ paymentScheduler . step = PaymentSchedulerStatus . CREATE_CHALLENGE
185
184
}
186
185
}
187
186
// The main purpose is updating the updatedAt of payment scheduler to avoid simultaneous processing
@@ -213,26 +212,26 @@ function getCreateChallengeParam (workPeriod, workPeriodPayment) {
213
212
async function checkWait ( step , tryCount ) {
214
213
// When calculating the retry time later, we need to subtract the time that has been waited before
215
214
let lapse = 0
216
- if ( step === stepEnum [ 0 ] ) {
215
+ if ( step === PaymentSchedulerStatus . START_PROCESS ) {
217
216
lapse += await checkPerMinThreshold ( 'paymentsProcessed' )
218
- } else if ( step === stepEnum [ 1 ] ) {
217
+ } else if ( step === PaymentSchedulerStatus . CREATE_CHALLENGE ) {
219
218
await checkPerMinThreshold ( 'challengeRequested' )
220
- } else if ( step === stepEnum [ 2 ] ) {
219
+ } else if ( step === PaymentSchedulerStatus . ASSIGN_MEMBER ) {
221
220
// Only when tryCount = 0, it comes from the previous step, and it is necessary to wait for a fixed time
222
- if ( FIX_DELAY_STEP_1_2 > 0 && tryCount === 0 ) {
223
- await sleep ( FIX_DELAY_STEP_1_2 )
221
+ if ( FIX_DELAY_STEP_CREATE_CHALLENGE > 0 && tryCount === 0 ) {
222
+ await sleep ( FIX_DELAY_STEP_CREATE_CHALLENGE )
224
223
}
225
224
lapse += await checkPerMinThreshold ( 'resourceRequested' )
226
- } else if ( step === stepEnum [ 3 ] ) {
225
+ } else if ( step === PaymentSchedulerStatus . ACTIVATE_CHALLENGE ) {
227
226
// Only when tryCount = 0, it comes from the previous step, and it is necessary to wait for a fixed time
228
- if ( FIX_DELAY_STEP_2_3 > 0 && tryCount === 0 ) {
229
- await sleep ( FIX_DELAY_STEP_2_3 )
227
+ if ( FIX_DELAY_STEP_ASSIGN_MEMBER > 0 && tryCount === 0 ) {
228
+ await sleep ( FIX_DELAY_STEP_ASSIGN_MEMBER )
230
229
}
231
230
lapse += await checkPerMinThreshold ( 'challengeRequested' )
232
- } else if ( step === stepEnum [ 5 ] ) {
231
+ } else if ( step === PaymentSchedulerStatus . CLOSE_CHALLENGE ) {
233
232
// Only when tryCount = 0, it comes from the previous step, and it is necessary to wait for a fixed time
234
- if ( FIX_DELAY_STEP_3_4 > 0 && tryCount === 0 ) {
235
- await sleep ( FIX_DELAY_STEP_3_4 )
233
+ if ( FIX_DELAY_STEP_ACTIVATE_CHALLENGE > 0 && tryCount === 0 ) {
234
+ await sleep ( FIX_DELAY_STEP_ACTIVATE_CHALLENGE )
236
235
}
237
236
lapse += await checkPerMinThreshold ( 'challengeRequested' )
238
237
}
@@ -305,9 +304,9 @@ async function withRetry (func, argArr, predictFunc, step) {
305
304
if ( SWITCH === PaymentProcessingSwitch . OFF ) {
306
305
// without actual API calls by adding delay (for example 1 second for each step), to simulate the act
307
306
sleep ( 1000 )
308
- if ( step === stepEnum [ 1 ] ) {
307
+ if ( step === PaymentSchedulerStatus . CREATE_CHALLENGE ) {
309
308
return '00000000-0000-0000-0000-000000000000'
310
- } else if ( step === stepEnum [ 4 ] ) {
309
+ } else if ( step === PaymentSchedulerStatus . GET_USER_ID ) {
311
310
return { userId : 100001 }
312
311
}
313
312
return
0 commit comments