@@ -61,26 +61,29 @@ async function _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking (w
61
61
throw new errors . ConflictError ( `id: ${ correspondingResourceBooking . id } "ResourceBooking" Billing account is not assigned to the resource booking` )
62
62
}
63
63
workPeriodPayment . billingAccountId = correspondingResourceBooking . billingAccountId
64
- if ( _ . isNil ( correspondingResourceBooking . memberRate ) ) {
65
- throw new errors . ConflictError ( `Can't find a member rate in ResourceBooking: ${ correspondingResourceBooking . id } to calculate the amount` )
66
- }
67
- if ( correspondingResourceBooking . memberRate <= 0 ) {
68
- throw new errors . ConflictError ( `Can't process payment with member rate: ${ correspondingResourceBooking . memberRate } . It must be higher than 0` )
69
- }
70
- workPeriodPayment . memberRate = correspondingResourceBooking . memberRate
71
- const maxPossibleDays = correspondingWorkPeriod . daysWorked - correspondingWorkPeriod . daysPaid
72
- if ( workPeriodPayment . days > maxPossibleDays ) {
73
- throw new errors . BadRequestError ( `Days cannot be more than not paid days which is ${ maxPossibleDays } ` )
74
- }
75
- if ( maxPossibleDays <= 0 ) {
76
- throw new errors . ConflictError ( `There are no days to pay for WorkPeriod: ${ correspondingWorkPeriod . id } ` )
77
- }
78
- const workPeriodStartTime = moment ( `${ correspondingWorkPeriod . startDate } T00:00:00.000+12` )
79
- if ( workPeriodStartTime . isAfter ( moment ( ) ) ) {
80
- throw new errors . BadRequestError ( `Cannot process payments for the future WorkPeriods. You can process after ${ workPeriodStartTime . diff ( moment ( ) , 'hours' ) } hours` )
64
+ if ( ! _ . has ( workPeriodPayment , 'days' ) || workPeriodPayment . days > 0 ) {
65
+ if ( _ . isNil ( correspondingResourceBooking . memberRate ) ) {
66
+ throw new errors . ConflictError ( `Can't find a member rate in ResourceBooking: ${ correspondingResourceBooking . id } to calculate the amount` )
67
+ }
68
+ if ( correspondingResourceBooking . memberRate <= 0 ) {
69
+ throw new errors . ConflictError ( `Can't process payment with member rate: ${ correspondingResourceBooking . memberRate } . It must be higher than 0` )
70
+ }
71
+ workPeriodPayment . memberRate = correspondingResourceBooking . memberRate
72
+ const maxPossibleDays = correspondingWorkPeriod . daysWorked - correspondingWorkPeriod . daysPaid
73
+ if ( workPeriodPayment . days > maxPossibleDays ) {
74
+ throw new errors . BadRequestError ( `Days cannot be more than not paid days which is ${ maxPossibleDays } ` )
75
+ }
76
+ if ( maxPossibleDays <= 0 ) {
77
+ throw new errors . ConflictError ( `There are no days to pay for WorkPeriod: ${ correspondingWorkPeriod . id } ` )
78
+ }
79
+ const workPeriodStartTime = moment ( `${ correspondingWorkPeriod . startDate } T00:00:00.000+12` )
80
+ if ( workPeriodStartTime . isAfter ( moment ( ) ) ) {
81
+ throw new errors . BadRequestError ( `Cannot process payments for the future WorkPeriods. You can process after ${ workPeriodStartTime . diff ( moment ( ) , 'hours' ) } hours` )
82
+ }
83
+ workPeriodPayment . days = _ . defaultTo ( workPeriodPayment . days , maxPossibleDays )
84
+ workPeriodPayment . amount = _ . round ( workPeriodPayment . memberRate * workPeriodPayment . days / 5 , 2 )
81
85
}
82
- workPeriodPayment . days = _ . defaultTo ( workPeriodPayment . days , maxPossibleDays )
83
- workPeriodPayment . amount = workPeriodPayment . days > 0 ? _ . round ( workPeriodPayment . memberRate * workPeriodPayment . days / 5 , 2 ) : workPeriodPayment . amount
86
+ workPeriodPayment . memberRate = _ . defaultTo ( workPeriodPayment . memberRate , 0 )
84
87
workPeriodPayment . customerRate = _ . defaultTo ( correspondingResourceBooking . customerRate , null )
85
88
workPeriodPayment . id = uuid . v4 ( )
86
89
workPeriodPayment . status = WorkPeriodPaymentStatus . SCHEDULED
@@ -186,7 +189,13 @@ async function createWorkPeriodPayment (currentUser, workPeriodPayment) {
186
189
const singleCreateWorkPeriodPaymentSchema = Joi . object ( ) . keys ( {
187
190
workPeriodId : Joi . string ( ) . uuid ( ) . required ( ) ,
188
191
days : Joi . number ( ) . integer ( ) . min ( 0 ) . max ( 5 ) ,
189
- amount : Joi . number ( )
192
+ amount : Joi . when ( 'days' , {
193
+ is : Joi . number ( ) . integer ( ) . valid ( 0 ) . exist ( ) ,
194
+ then : Joi . number ( ) . greater ( 0 ) . required ( ) . messages ( {
195
+ 'any.required' : '"amount" has to be provided when processing additional payment for 0 days'
196
+ } ) ,
197
+ otherwise : Joi . forbidden ( )
198
+ } )
190
199
} )
191
200
createWorkPeriodPayment . schema = Joi . object ( ) . keys ( {
192
201
currentUser : Joi . object ( ) . required ( ) ,
0 commit comments