From 99f97b360eb06f266ee4f65d4c7b4ec458bb4f38 Mon Sep 17 00:00:00 2001 From: narekcat Date: Wed, 28 Jul 2021 18:27:02 +0400 Subject: [PATCH 1/2] fix: additional payments --- src/services/WorkPeriodPaymentService.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/services/WorkPeriodPaymentService.js b/src/services/WorkPeriodPaymentService.js index 9a7a7780..82e82934 100644 --- a/src/services/WorkPeriodPaymentService.js +++ b/src/services/WorkPeriodPaymentService.js @@ -80,7 +80,7 @@ async function _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking (w throw new errors.BadRequestError(`Cannot process payments for the future WorkPeriods. You can process after ${workPeriodStartTime.diff(moment(), 'hours')} hours`) } workPeriodPayment.days = _.defaultTo(workPeriodPayment.days, maxPossibleDays) - workPeriodPayment.amount = _.round(workPeriodPayment.memberRate * workPeriodPayment.days / 5, 2) + workPeriodPayment.amount = workPeriodPayment.days > 0 ? _.round(workPeriodPayment.memberRate * workPeriodPayment.days / 5, 2) : workPeriodPayment.amount workPeriodPayment.customerRate = _.defaultTo(correspondingResourceBooking.customerRate, null) workPeriodPayment.id = uuid.v4() workPeriodPayment.status = WorkPeriodPaymentStatus.SCHEDULED @@ -185,7 +185,8 @@ async function createWorkPeriodPayment (currentUser, workPeriodPayment) { const singleCreateWorkPeriodPaymentSchema = Joi.object().keys({ workPeriodId: Joi.string().uuid().required(), - days: Joi.number().integer().min(1).max(5) + days: Joi.number().integer().min(0).max(5), + amount: Joi.number() }) createWorkPeriodPayment.schema = Joi.object().keys({ currentUser: Joi.object().required(), From 3fe30ad9bc6036df96b5bba07fbb51c8c629ae3b Mon Sep 17 00:00:00 2001 From: narekcat Date: Thu, 29 Jul 2021 18:28:51 +0400 Subject: [PATCH 2/2] fix: additional payments, add joi validation rule for amount field --- src/services/WorkPeriodPaymentService.js | 49 ++++++++++++++---------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/services/WorkPeriodPaymentService.js b/src/services/WorkPeriodPaymentService.js index 82e82934..5e0f6a3e 100644 --- a/src/services/WorkPeriodPaymentService.js +++ b/src/services/WorkPeriodPaymentService.js @@ -61,26 +61,29 @@ async function _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking (w throw new errors.ConflictError(`id: ${correspondingResourceBooking.id} "ResourceBooking" Billing account is not assigned to the resource booking`) } workPeriodPayment.billingAccountId = correspondingResourceBooking.billingAccountId - if (_.isNil(correspondingResourceBooking.memberRate)) { - throw new errors.ConflictError(`Can't find a member rate in ResourceBooking: ${correspondingResourceBooking.id} to calculate the amount`) - } - if (correspondingResourceBooking.memberRate <= 0) { - throw new errors.ConflictError(`Can't process payment with member rate: ${correspondingResourceBooking.memberRate}. It must be higher than 0`) - } - workPeriodPayment.memberRate = correspondingResourceBooking.memberRate - const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid - if (workPeriodPayment.days > maxPossibleDays) { - throw new errors.BadRequestError(`Days cannot be more than not paid days which is ${maxPossibleDays}`) - } - if (maxPossibleDays <= 0) { - throw new errors.ConflictError(`There are no days to pay for WorkPeriod: ${correspondingWorkPeriod.id}`) - } - const workPeriodStartTime = moment(`${correspondingWorkPeriod.startDate}T00:00:00.000+12`) - if (workPeriodStartTime.isAfter(moment())) { - throw new errors.BadRequestError(`Cannot process payments for the future WorkPeriods. You can process after ${workPeriodStartTime.diff(moment(), 'hours')} hours`) + if (!_.has(workPeriodPayment, 'days') || workPeriodPayment.days > 0) { + if (_.isNil(correspondingResourceBooking.memberRate)) { + throw new errors.ConflictError(`Can't find a member rate in ResourceBooking: ${correspondingResourceBooking.id} to calculate the amount`) + } + if (correspondingResourceBooking.memberRate <= 0) { + throw new errors.ConflictError(`Can't process payment with member rate: ${correspondingResourceBooking.memberRate}. It must be higher than 0`) + } + workPeriodPayment.memberRate = correspondingResourceBooking.memberRate + const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid + if (workPeriodPayment.days > maxPossibleDays) { + throw new errors.BadRequestError(`Days cannot be more than not paid days which is ${maxPossibleDays}`) + } + if (maxPossibleDays <= 0) { + throw new errors.ConflictError(`There are no days to pay for WorkPeriod: ${correspondingWorkPeriod.id}`) + } + const workPeriodStartTime = moment(`${correspondingWorkPeriod.startDate}T00:00:00.000+12`) + if (workPeriodStartTime.isAfter(moment())) { + throw new errors.BadRequestError(`Cannot process payments for the future WorkPeriods. You can process after ${workPeriodStartTime.diff(moment(), 'hours')} hours`) + } + workPeriodPayment.days = _.defaultTo(workPeriodPayment.days, maxPossibleDays) + workPeriodPayment.amount = _.round(workPeriodPayment.memberRate * workPeriodPayment.days / 5, 2) } - workPeriodPayment.days = _.defaultTo(workPeriodPayment.days, maxPossibleDays) - workPeriodPayment.amount = workPeriodPayment.days > 0 ? _.round(workPeriodPayment.memberRate * workPeriodPayment.days / 5, 2) : workPeriodPayment.amount + workPeriodPayment.memberRate = _.defaultTo(workPeriodPayment.memberRate, 0) workPeriodPayment.customerRate = _.defaultTo(correspondingResourceBooking.customerRate, null) workPeriodPayment.id = uuid.v4() workPeriodPayment.status = WorkPeriodPaymentStatus.SCHEDULED @@ -186,7 +189,13 @@ async function createWorkPeriodPayment (currentUser, workPeriodPayment) { const singleCreateWorkPeriodPaymentSchema = Joi.object().keys({ workPeriodId: Joi.string().uuid().required(), days: Joi.number().integer().min(0).max(5), - amount: Joi.number() + amount: Joi.when('days', { + is: Joi.number().integer().valid(0).exist(), + then: Joi.number().greater(0).required().messages({ + 'any.required': '"amount" has to be provided when processing additional payment for 0 days' + }), + otherwise: Joi.forbidden() + }) }) createWorkPeriodPayment.schema = Joi.object().keys({ currentUser: Joi.object().required(),