@@ -48,6 +48,38 @@ async function _createSingleWorkPeriodPayment (workPeriodPayment, createdBy) {
48
48
return _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking ( workPeriodPayment , createdBy , correspondingWorkPeriod . toJSON ( ) , correspondingResourceBooking . toJSON ( ) )
49
49
}
50
50
51
+ /**
52
+ * update challenge
53
+ * @param {String } challengeId the challenge id
54
+ * @param {Object } data the challenge update data
55
+ * @returns {undefined }
56
+ */
57
+ async function _updateChallenge ( challengeId , data ) {
58
+ const body = { }
59
+ if ( data . billingAccountId ) {
60
+ body . billing = {
61
+ billingAccountId : _ . toString ( data . billingAccountId ) ,
62
+ markup : 0 // for TaaS payments we always use 0 markup
63
+ }
64
+ }
65
+ if ( data . amount ) {
66
+ body . prizeSets = [ {
67
+ type : 'placement' ,
68
+ prizes : [ { type : 'USD' , value : data . amount } ]
69
+ } ]
70
+ }
71
+
72
+ if ( data . billingAccountId || data . amount ) {
73
+ try {
74
+ await helper . updateChallenge ( challengeId , body )
75
+ logger . debug ( { component : 'WorkPeriodPaymentService' , context : 'updateChallenge' , message : `Challenge with id ${ challengeId } is updated` } )
76
+ } catch ( err ) {
77
+ logger . error ( { component : 'WorkPeriodPaymentService' , context : 'updateChallenge' , message : err . response . text } )
78
+ throw new errors . BadRequestError ( `Cannot update the the challenge: ${ err . response . text } ` )
79
+ }
80
+ }
81
+ }
82
+
51
83
/**
52
84
* Create single workPeriodPayment
53
85
* @param {Object } workPeriodPayment the workPeriodPayment to be created
@@ -220,7 +252,16 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
220
252
_checkUserPermissionForCRUWorkPeriodPayment ( currentUser )
221
253
222
254
const workPeriodPayment = await WorkPeriodPayment . findById ( id )
255
+
223
256
const oldValue = workPeriodPayment . toJSON ( )
257
+
258
+ if ( oldValue . status === 'in-progress' ) {
259
+ const keys = _ . keys ( _ . pick ( data , [ 'amount' , 'days' , 'memberRate' , 'customerRate' , 'billingAccountId' ] ) )
260
+ if ( keys . length ) {
261
+ throw new errors . BadRequestError ( `${ JSON . stringify ( keys ) } cannot be updated when workPeriodPayment status is in-progress` )
262
+ }
263
+ }
264
+
224
265
if ( data . status === 'cancelled' && oldValue . status === 'in-progress' ) {
225
266
throw new errors . BadRequestError ( 'You cannot cancel a WorkPeriodPayment which is in-progress' )
226
267
}
@@ -235,6 +276,20 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
235
276
throw new errors . BadRequestError ( 'There is no available daysWorked to schedule a payment' )
236
277
}
237
278
}
279
+
280
+ if ( data . days ) {
281
+ const correspondingWorkPeriod = await helper . ensureWorkPeriodById ( workPeriodPayment . workPeriodId ) // ensure work period exists
282
+ const maxPossibleDays = correspondingWorkPeriod . daysWorked - correspondingWorkPeriod . daysPaid - oldValue . days
283
+ if ( data . days > maxPossibleDays ) {
284
+ throw new errors . BadRequestError ( `Days cannot be more than not paid days which is ${ maxPossibleDays } ` )
285
+ }
286
+ }
287
+
288
+ // challengeId exist and skip dummy challenge
289
+ if ( oldValue . challengeId && oldValue . challengeId !== '00000000-0000-0000-0000-000000000000' ) {
290
+ await _updateChallenge ( workPeriodPayment . challengeId , data )
291
+ }
292
+
238
293
data . updatedBy = await helper . getUserId ( currentUser . userId )
239
294
const updated = await workPeriodPayment . update ( data )
240
295
await helper . postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue : oldValue , key : `workPeriodPayment.billingAccountId:${ updated . billingAccountId } ` } )
@@ -256,7 +311,12 @@ partiallyUpdateWorkPeriodPayment.schema = Joi.object().keys({
256
311
currentUser : Joi . object ( ) . required ( ) ,
257
312
id : Joi . string ( ) . uuid ( ) . required ( ) ,
258
313
data : Joi . object ( ) . keys ( {
259
- status : Joi . workPeriodPaymentUpdateStatus ( )
314
+ status : Joi . workPeriodPaymentUpdateStatus ( ) ,
315
+ amount : Joi . number ( ) . min ( 0 ) ,
316
+ days : Joi . number ( ) . integer ( ) ,
317
+ memberRate : Joi . number ( ) . positive ( ) ,
318
+ customerRate : Joi . number ( ) . positive ( ) . allow ( null ) ,
319
+ billingAccountId : Joi . number ( ) . positive ( ) . integer ( )
260
320
} ) . min ( 1 ) . required ( )
261
321
} ) . required ( )
262
322
0 commit comments