Skip to content

Commit 3fe30ad

Browse files
committed
fix: additional payments, add joi validation rule for amount field
1 parent 99f97b3 commit 3fe30ad

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/services/WorkPeriodPaymentService.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,29 @@ async function _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking (w
6161
throw new errors.ConflictError(`id: ${correspondingResourceBooking.id} "ResourceBooking" Billing account is not assigned to the resource booking`)
6262
}
6363
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)
8185
}
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)
8487
workPeriodPayment.customerRate = _.defaultTo(correspondingResourceBooking.customerRate, null)
8588
workPeriodPayment.id = uuid.v4()
8689
workPeriodPayment.status = WorkPeriodPaymentStatus.SCHEDULED
@@ -186,7 +189,13 @@ async function createWorkPeriodPayment (currentUser, workPeriodPayment) {
186189
const singleCreateWorkPeriodPaymentSchema = Joi.object().keys({
187190
workPeriodId: Joi.string().uuid().required(),
188191
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+
})
190199
})
191200
createWorkPeriodPayment.schema = Joi.object().keys({
192201
currentUser: Joi.object().required(),

0 commit comments

Comments
 (0)