Skip to content

Commit 23ac85f

Browse files
authored
Merge pull request #435 from narekcat/issue-430
fix: additional payments
2 parents f559204 + 3fe30ad commit 23ac85f

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/services/WorkPeriodPaymentService.js

Lines changed: 30 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 = _.round(workPeriodPayment.memberRate * workPeriodPayment.days / 5, 2)
86+
workPeriodPayment.memberRate = _.defaultTo(workPeriodPayment.memberRate, 0)
8487
workPeriodPayment.customerRate = _.defaultTo(correspondingResourceBooking.customerRate, null)
8588
workPeriodPayment.id = uuid.v4()
8689
workPeriodPayment.status = WorkPeriodPaymentStatus.SCHEDULED
@@ -185,7 +188,14 @@ async function createWorkPeriodPayment (currentUser, workPeriodPayment) {
185188

186189
const singleCreateWorkPeriodPaymentSchema = Joi.object().keys({
187190
workPeriodId: Joi.string().uuid().required(),
188-
days: Joi.number().integer().min(1).max(5)
191+
days: Joi.number().integer().min(0).max(5),
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+
})
189199
})
190200
createWorkPeriodPayment.schema = Joi.object().keys({
191201
currentUser: Joi.object().required(),

0 commit comments

Comments
 (0)