@@ -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
+ * Create single workPeriodPayment
53
+ * @param {Object } workPeriodPayment the workPeriodPayment to be created
54
+ * @param {String } createdBy the authUser id
55
+ * @returns {Object } the created workPeriodPayment
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
@@ -207,7 +239,15 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
207
239
_checkUserPermissionForCRUWorkPeriodPayment ( currentUser )
208
240
209
241
const workPeriodPayment = await WorkPeriodPayment . findById ( id )
242
+
210
243
const oldValue = workPeriodPayment . toJSON ( )
244
+
245
+ if ( oldValue . status === 'in-progress' ) {
246
+ _ . each ( _ . pick ( data , [ 'amount' , 'days' , 'memberRate' , 'customerRate' , 'billingAccountId' ] ) , ( value , key ) => {
247
+ throw new errors . BadRequestError ( `${ key } cannot be updated when workPeriodPayment status is in-progress` )
248
+ } )
249
+ }
250
+
211
251
if ( data . status === 'cancelled' && oldValue . status === 'in-progress' ) {
212
252
throw new errors . BadRequestError ( 'You cannot cancel a WorkPeriodPayment which is in-progress' )
213
253
}
@@ -222,6 +262,19 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
222
262
throw new errors . BadRequestError ( 'There is no available daysWorked to schedule a payment' )
223
263
}
224
264
}
265
+
266
+ if ( data . days ) {
267
+ const correspondingWorkPeriod = await helper . ensureWorkPeriodById ( workPeriodPayment . workPeriodId ) // ensure work period exists
268
+ const maxPossibleDays = correspondingWorkPeriod . daysWorked - correspondingWorkPeriod . daysPaid
269
+ if ( data . days > maxPossibleDays ) {
270
+ throw new errors . BadRequestError ( `Days cannot be more than not paid days which is ${ maxPossibleDays } ` )
271
+ }
272
+ }
273
+
274
+ if ( oldValue . challengeId ) {
275
+ await _updateChallenge ( workPeriodPayment . challengeId , data )
276
+ }
277
+
225
278
data . updatedBy = await helper . getUserId ( currentUser . userId )
226
279
const updated = await workPeriodPayment . update ( data )
227
280
await helper . postEvent ( config . TAAS_WORK_PERIOD_PAYMENT_UPDATE_TOPIC , updated . toJSON ( ) , { oldValue : oldValue , key : `workPeriodPayment.billingAccountId:${ updated . billingAccountId } ` } )
@@ -243,7 +296,12 @@ partiallyUpdateWorkPeriodPayment.schema = Joi.object().keys({
243
296
currentUser : Joi . object ( ) . required ( ) ,
244
297
id : Joi . string ( ) . uuid ( ) . required ( ) ,
245
298
data : Joi . object ( ) . keys ( {
246
- status : Joi . workPeriodPaymentUpdateStatus ( )
299
+ status : Joi . workPeriodPaymentUpdateStatus ( ) ,
300
+ amount : Joi . number ( ) . min ( 0 ) ,
301
+ days : Joi . number ( ) . integer ( ) ,
302
+ memberRate : Joi . number ( ) . positive ( ) . required ( ) ,
303
+ customerRate : Joi . number ( ) . positive ( ) . allow ( null ) ,
304
+ billingAccountId : Joi . number ( ) . positive ( ) . integer ( ) . required ( )
247
305
} ) . min ( 1 ) . required ( )
248
306
} ) . required ( )
249
307
0 commit comments