@@ -2,11 +2,16 @@ const _ = require('lodash')
2
2
const config = require ( 'config' )
3
3
const moment = require ( 'moment' )
4
4
const models = require ( '../models' )
5
- const { getMemberDetailsByHandle, getChallenge, getChallengeResource, sleep, postEvent } = require ( '../common/helper' )
5
+ const { getMemberDetailsByHandle, getChallenge, getChallengeResource, sleep, postEvent, postErrorEvent } = require ( '../common/helper' )
6
6
const logger = require ( '../common/logger' )
7
7
const { createChallenge, addResourceToChallenge, activateChallenge, closeChallenge } = require ( './PaymentService' )
8
8
const { ChallengeStatus, PaymentSchedulerStatus, PaymentProcessingSwitch } = require ( '../../app-constants' )
9
9
10
+ const {
11
+ processUpdate
12
+ } = require ( '../esProcessors/WorkPeriodPaymentProcessor' )
13
+
14
+ const sequelize = models . sequelize
10
15
const WorkPeriodPayment = models . WorkPeriodPayment
11
16
const WorkPeriod = models . WorkPeriod
12
17
const PaymentScheduler = models . PaymentScheduler
@@ -88,9 +93,22 @@ async function processPayment (workPeriodPayment) {
88
93
}
89
94
} else {
90
95
const oldValue = workPeriodPayment . toJSON ( )
91
- const updated = await workPeriodPayment . update ( { status : 'in-progress' } )
92
- // Update the modified status to es
93
- await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue, key : `workPeriodPayment.billingAccountId:${ updated . billingAccountId } ` } )
96
+ let entity
97
+ let key
98
+ try {
99
+ await sequelize . transaction ( async ( t ) => {
100
+ const updated = await workPeriodPayment . update ( { status : 'in-progress' } , { transaction : t } )
101
+ key = `workPeriodPayment.billingAccountId:${ updated . billingAccountId } `
102
+ entity = updated . toJSON ( )
103
+ await processUpdate ( { ...entity , key } )
104
+ } )
105
+ } catch ( e ) {
106
+ if ( entity ) {
107
+ postErrorEvent ( config . TAAS_ERROR_TOPIC , entity , 'workperiodpayment.update' )
108
+ }
109
+ throw e
110
+ }
111
+ await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , entity , { oldValue : oldValue , key } )
94
112
}
95
113
// Check whether the number of processed records per minute exceeds the specified number, if it exceeds, wait for the next minute before processing
96
114
await checkWait ( PaymentSchedulerStatus . START_PROCESS )
@@ -112,11 +130,24 @@ async function processPayment (workPeriodPayment) {
112
130
}
113
131
114
132
const oldValue = workPeriodPayment . toJSON ( )
115
- // 5. update wp and save it should only update already existent Work Period Payment record with created "challengeId" and "status=completed".
116
- const updated = await workPeriodPayment . update ( { challengeId : paymentScheduler . challengeId , status : 'completed' } )
117
- // Update the modified status to es
118
- await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue, key : `workPeriodPayment.billingAccountId:${ updated . billingAccountId } ` } )
119
133
134
+ let key
135
+ let entity
136
+ try {
137
+ await sequelize . transaction ( async ( t ) => {
138
+ // 5. update wp and save it should only update already existent Work Period Payment record with created "challengeId" and "status=completed".
139
+ const updated = await workPeriodPayment . update ( { challengeId : paymentScheduler . challengeId , status : 'completed' } , { transaction : t } )
140
+ entity = updated . toJSON ( )
141
+ await processUpdate ( { ...entity , key } )
142
+ } )
143
+ } catch ( e ) {
144
+ if ( entity ) {
145
+ postErrorEvent ( config . TAAS_ERROR_TOPIC , entity , 'workperiodpayment.update' )
146
+ }
147
+ throw e
148
+ }
149
+ // Update the modified status to es
150
+ await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , entity , { oldValue : oldValue , key } )
120
151
await paymentScheduler . update ( { step : PaymentSchedulerStatus . CLOSE_CHALLENGE , userId : paymentScheduler . userId , status : 'completed' } )
121
152
122
153
localLogger . info ( `Processed workPeriodPayment ${ workPeriodPayment . id } successfully` , 'processPayment' )
@@ -125,10 +156,24 @@ async function processPayment (workPeriodPayment) {
125
156
logger . logFullError ( err , { component : 'PaymentSchedulerService' , context : 'processPayment' } )
126
157
const statusDetails = { errorMessage : extractErrorMessage ( err ) , errorCode : _ . get ( err , 'status' , - 1 ) , retry : _ . get ( err , 'retry' , - 1 ) , step : _ . get ( err , 'step' ) , challengeId : paymentScheduler ? paymentScheduler . challengeId : null }
127
158
const oldValue = workPeriodPayment . toJSON ( )
128
- // If payment processing failed Work Periods Payment "status" should be changed to "failed" and populate "statusDetails" field with error details in JSON format.
129
- const updated = await workPeriodPayment . update ( { statusDetails, status : 'failed' } )
130
- // Update the modified status to es
131
- await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue, key : `workPeriodPayment.billingAccountId:${ updated . billingAccountId } ` } )
159
+
160
+ let entity
161
+ let key
162
+ try {
163
+ await sequelize . transaction ( async ( t ) => {
164
+ // If payment processing failed Work Periods Payment "status" should be changed to "failed" and populate "statusDetails" field with error details in JSON format.
165
+ const updated = await workPeriodPayment . update ( { statusDetails, status : 'failed' } , { transaction : t } )
166
+ key = `workPeriodPayment.billingAccountId:${ updated . billingAccountId } `
167
+ entity = updated . toJSON ( )
168
+ await processUpdate ( { ...entity , key } )
169
+ } )
170
+ } catch ( e ) {
171
+ if ( entity ) {
172
+ postErrorEvent ( config . TAAS_ERROR_TOPIC , entity , 'workperiodpayment.update' )
173
+ }
174
+ throw e
175
+ }
176
+ await postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , entity , { oldValue : oldValue , key } )
132
177
133
178
if ( paymentScheduler ) {
134
179
await paymentScheduler . update ( { step : _ . get ( err , 'step' ) , userId : paymentScheduler . userId , status : 'failed' } )
0 commit comments