Skip to content

Commit 8949ec3

Browse files
committed
fix: issue #431
1 parent 66a2ec2 commit 8949ec3

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/services/WorkPeriodPaymentService.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ async function _createSingleWorkPeriodPayment (workPeriodPayment, createdBy) {
4949
}
5050

5151
/**
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
52+
* update challenge
53+
* @param {String} challengeId the challenge id
54+
* @param {Object} data the challenge update data
55+
* @returns {undefined}
5656
*/
5757
async function _updateChallenge (challengeId, data) {
5858
const body = {}
@@ -243,9 +243,10 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
243243
const oldValue = workPeriodPayment.toJSON()
244244

245245
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-
})
246+
const keys = _.keys(_.pick(data, ['amount', 'days', 'memberRate', 'customerRate', 'billingAccountId']))
247+
if (keys.length) {
248+
throw new errors.BadRequestError(`${JSON.stringify(keys)} cannot be updated when workPeriodPayment status is in-progress`)
249+
}
249250
}
250251

251252
if (data.status === 'cancelled' && oldValue.status === 'in-progress') {
@@ -265,13 +266,14 @@ async function updateWorkPeriodPayment (currentUser, id, data) {
265266

266267
if (data.days) {
267268
const correspondingWorkPeriod = await helper.ensureWorkPeriodById(workPeriodPayment.workPeriodId) // ensure work period exists
268-
const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid
269+
const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid - oldValue.days
269270
if (data.days > maxPossibleDays) {
270271
throw new errors.BadRequestError(`Days cannot be more than not paid days which is ${maxPossibleDays}`)
271272
}
272273
}
273274

274-
if (oldValue.challengeId) {
275+
// challengeId exist and skip dummy challenge
276+
if (oldValue.challengeId && oldValue.challengeId !== '00000000-0000-0000-0000-000000000000') {
275277
await _updateChallenge(workPeriodPayment.challengeId, data)
276278
}
277279

@@ -299,9 +301,9 @@ partiallyUpdateWorkPeriodPayment.schema = Joi.object().keys({
299301
status: Joi.workPeriodPaymentUpdateStatus(),
300302
amount: Joi.number().min(0),
301303
days: Joi.number().integer(),
302-
memberRate: Joi.number().positive().required(),
304+
memberRate: Joi.number().positive(),
303305
customerRate: Joi.number().positive().allow(null),
304-
billingAccountId: Joi.number().positive().integer().required()
306+
billingAccountId: Joi.number().positive().integer()
305307
}).min(1).required()
306308
}).required()
307309

0 commit comments

Comments
 (0)