Skip to content

Commit 233ae68

Browse files
committed
use paymentStatus constants
1 parent 8bb17e5 commit 233ae68

11 files changed

+68
-47
lines changed

app-constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ const ChallengeStatus = {
8484
COMPLETED: 'Completed'
8585
}
8686

87+
const PaymentStatus = {
88+
PENDING: 'pending',
89+
IN_PROGRESS: 'in-progress',
90+
PARTIALLY_COMPLETED: 'partially-completed',
91+
COMPLETED: 'completed',
92+
FAILED: 'failed',
93+
NO_DAYS: 'no-days'
94+
}
95+
8796
const WorkPeriodPaymentStatus = {
8897
COMPLETED: 'completed',
8998
SCHEDULED: 'scheduled',
@@ -117,6 +126,7 @@ module.exports = {
117126
Scopes,
118127
Interviews,
119128
ChallengeStatus,
129+
PaymentStatus,
120130
WorkPeriodPaymentStatus,
121131
WorkPeriodPaymentUpdateStatus,
122132
PaymentSchedulerStatus,

data/demo-data.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@
15331533
"daysWorked": 0,
15341534
"daysPaid": 0,
15351535
"paymentTotal": 0,
1536-
"paymentStatus": "noDays",
1536+
"paymentStatus": "no-days",
15371537
"createdBy": "00000000-0000-0000-0000-000000000000",
15381538
"updatedBy": "57646ff9-1cd3-4d3c-88ba-eb09a395366c",
15391539
"createdAt": "2021-05-09T21:27:12.794Z",
@@ -2430,7 +2430,7 @@
24302430
"daysWorked": 0,
24312431
"daysPaid": 0,
24322432
"paymentTotal": 0,
2433-
"paymentStatus": "noDays",
2433+
"paymentStatus": "no-days",
24342434
"createdBy": "00000000-0000-0000-0000-000000000000",
24352435
"updatedBy": "00000000-0000-0000-0000-000000000000",
24362436
"createdAt": "2021-05-30T11:48:57.210Z",
@@ -2551,7 +2551,7 @@
25512551
"daysWorked": 0,
25522552
"daysPaid": 0,
25532553
"paymentTotal": 0,
2554-
"paymentStatus": "noDays",
2554+
"paymentStatus": "no-days",
25552555
"createdBy": "00000000-0000-0000-0000-000000000000",
25562556
"updatedBy": "00000000-0000-0000-0000-000000000000",
25572557
"createdAt": "2021-05-30T11:49:18.405Z",
@@ -2914,7 +2914,7 @@
29142914
"daysWorked": 0,
29152915
"daysPaid": 0,
29162916
"paymentTotal": 0,
2917-
"paymentStatus": "noDays",
2917+
"paymentStatus": "no-days",
29182918
"createdBy": "00000000-0000-0000-0000-000000000000",
29192919
"updatedBy": "00000000-0000-0000-0000-000000000000",
29202920
"createdAt": "2021-05-30T11:49:01.820Z",
@@ -4644,7 +4644,7 @@
46444644
"daysWorked": 0,
46454645
"daysPaid": 0,
46464646
"paymentTotal": 0,
4647-
"paymentStatus": "noDays",
4647+
"paymentStatus": "no-days",
46484648
"createdBy": "00000000-0000-0000-0000-000000000000",
46494649
"updatedBy": "00000000-0000-0000-0000-000000000000",
46504650
"createdAt": "2021-05-30T11:49:20.193Z",

docs/swagger.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ paths:
19231923
"completed",
19241924
"in-progress",
19251925
"failed",
1926-
"noDays",
1926+
"no-days",
19271927
]
19281928
- type: string
19291929
enum:
@@ -1933,7 +1933,7 @@ paths:
19331933
"completed",
19341934
"in-progress",
19351935
"failed",
1936-
"noDays",
1936+
"no-days",
19371937
]
19381938
description: comma separated payment status.
19391939
- in: query
@@ -4542,7 +4542,7 @@ components:
45424542
"completed",
45434543
"in-progress",
45444544
"failed",
4545-
"noDays",
4545+
"no-days",
45464546
]
45474547
description: "The payment status."
45484548
payments:

migrations/2021-06-14-create-and-populate-work-periods-for-resource-bookings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const ResourceBooking = require('../src/models').ResourceBooking
33
const _ = require('lodash')
44
const helper = require('../src/common/helper')
55
const { v4: uuid } = require('uuid')
6+
const { PaymentStatus } = require('../app-constants')
67

78
// maximum start date of resource bookings when populating work periods from existing resource bookings in migration script
89
const MAX_START_DATE = process.env.MAX_START_DATE || '2100-12-31'
@@ -84,7 +85,7 @@ module.exports = {
8485
days_worked: period.daysWorked,
8586
days_paid: 0,
8687
payment_total: 0,
87-
payment_status: period.daysWorked === 0 ? 'noDays' : 'pending',
88+
payment_status: period.daysWorked === 0 ? PaymentStatus.NO_DAYS : PaymentStatus.PENDING,
8889
created_by: config.m2m.M2M_AUDIT_USER_ID,
8990
created_at: new Date()
9091
})

src/bootstrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Joi = require('joi')
33
const config = require('config')
44
const path = require('path')
55
const _ = require('lodash')
6-
const { Interviews, WorkPeriodPaymentStatus, WorkPeriodPaymentUpdateStatus, PaymentProcessingSwitch } = require('../app-constants')
6+
const { Interviews, PaymentStatus, WorkPeriodPaymentStatus, WorkPeriodPaymentUpdateStatus, PaymentProcessingSwitch } = require('../app-constants')
77
const logger = require('./common/logger')
88

99
const allowedInterviewStatuses = _.values(Interviews.Status)
@@ -17,7 +17,7 @@ Joi.resourceBookingStatus = () => Joi.string().valid('placed', 'closed', 'cancel
1717
Joi.workload = () => Joi.string().valid('full-time', 'fractional')
1818
Joi.jobCandidateStatus = () => Joi.string().valid('open', 'placed', 'selected', 'client rejected - screening', 'client rejected - interview', 'rejected - other', 'cancelled', 'interview', 'topcoder-rejected', 'applied', 'rejected-pre-screen', 'skills-test', 'skills-test', 'phone-screen', 'job-closed', 'offered')
1919
Joi.title = () => Joi.string().max(128)
20-
Joi.paymentStatus = () => Joi.string().valid('pending', 'in-progress', 'partially-completed', 'completed', 'failed', 'noDays')
20+
Joi.paymentStatus = () => Joi.string().valid(..._.values(PaymentStatus))
2121
Joi.xaiTemplate = () => Joi.string().valid(...allowedXAITemplate)
2222
Joi.interviewStatus = () => Joi.string().valid(...allowedInterviewStatuses)
2323
Joi.workPeriodPaymentStatus = () => Joi.string().valid(..._.values(WorkPeriodPaymentStatus))

src/eventHandlers/ResourceBookingEventHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const _ = require('lodash')
77
const models = require('../models')
88
const logger = require('../common/logger')
99
const helper = require('../common/helper')
10+
const { PaymentStatus } = require('../../app-constants')
1011
const JobService = require('../services/JobService')
1112
const JobCandidateService = require('../services/JobCandidateService')
1213
const WorkPeriodService = require('../services/WorkPeriodService')
@@ -295,7 +296,7 @@ async function _createWorkPeriods (periods, resourceBookingId) {
295296
startDate: period.startDate,
296297
endDate: period.endDate,
297298
daysWorked: period.daysWorked,
298-
paymentStatus: period.daysWorked === 0 ? 'noDays' : 'pending'
299+
paymentStatus: period.daysWorked === 0 ? PaymentStatus.NO_DAYS : PaymentStatus.PENDING
299300
})
300301
}
301302
}

src/eventHandlers/WorkPeriodPaymentEventHandler.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = require('config')
77
const models = require('../models')
88
const logger = require('../common/logger')
99
const helper = require('../common/helper')
10+
const { PaymentStatus, WorkPeriodPaymentStatus } = require('../../app-constants')
1011
const WorkPeriod = models.WorkPeriod
1112

1213
/**
@@ -26,23 +27,23 @@ async function updateWorkPeriod (payload) {
2627
data.paymentTotal = 0
2728
_.each(workPeriod.payments, payment => {
2829
paymentStatuses[payment.status] = true
29-
if (_.includes(['scheduled', 'in-progress', 'completed'], payment.status)) {
30+
if (_.includes([WorkPeriodPaymentStatus.SCHEDULED, WorkPeriodPaymentStatus.IN_PROGRESS, WorkPeriodPaymentStatus.COMPLETED], payment.status)) {
3031
data.daysPaid += payment.days
3132
data.paymentTotal += payment.amount
3233
}
3334
})
3435
if (workPeriod.daysWorked === 0) {
35-
data.paymentStatus = 'noDays'
36-
} else if (paymentStatuses.scheduled || paymentStatuses['in-progress']) {
37-
data.paymentStatus = 'in-progress'
36+
data.paymentStatus = PaymentStatus.NO_DAYS
37+
} else if (paymentStatuses[WorkPeriodPaymentStatus.SCHEDULED] || paymentStatuses[WorkPeriodPaymentStatus.IN_PROGRESS]) {
38+
data.paymentStatus = PaymentStatus.IN_PROGRESS
3839
} else if (workPeriod.daysWorked === data.daysPaid) {
39-
data.paymentStatus = 'completed'
40-
} else if (paymentStatuses.completed) {
41-
data.paymentStatus = 'partially-completed'
42-
} else if (paymentStatuses.failed) {
43-
data.paymentStatus = 'failed'
40+
data.paymentStatus = PaymentStatus.COMPLETED
41+
} else if (paymentStatuses[WorkPeriodPaymentStatus.COMPLETED]) {
42+
data.paymentStatus = PaymentStatus.PARTIALLY_COMPLETED
43+
} else if (paymentStatuses[WorkPeriodPaymentStatus.FAILED]) {
44+
data.paymentStatus = PaymentStatus.FAILED
4445
} else {
45-
data.paymentStatus = 'pending'
46+
data.paymentStatus = PaymentStatus.PENDING
4647
}
4748
if (workPeriod.daysPaid === data.daysPaid && workPeriod.paymentTotal === data.paymentTotal && workPeriod.paymentStatus === data.paymentStatus) {
4849
logger.debug({

src/services/ResourceBookingService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ async function _ensurePaidWorkPeriodsNotDeleted (resourceBookingId, oldValue, ne
171171
function _checkForPaidWorkPeriods (workPeriods) {
172172
const paidWorkPeriods = _.filter(workPeriods, workPeriod => {
173173
// filter by WP and WPP status
174-
return (['completed', 'partially-completed', 'in-progress'].indexOf(workPeriod.paymentStatus) !== -1 ||
175-
_.some(workPeriod.payments, payment => ['completed', 'in-progress', 'shceduled'].indexOf(payment.status) !== -1))
174+
return ([constants.PaymentStatus.COMPLETED, constants.PaymentStatus.PARTIALLY_COMPLETED, constants.PaymentStatus.IN_PROGRESS].indexOf(workPeriod.paymentStatus) !== -1 ||
175+
_.some(workPeriod.payments, payment => [constants.WorkPeriodPaymentStatus.COMPLETED, constants.WorkPeriodPaymentStatus.IN_PROGRESS, constants.WorkPeriodPaymentStatus.SCHEDULED].indexOf(payment.status) !== -1))
176176
})
177177
if (paidWorkPeriods.length > 0) {
178178
throw new errors.BadRequestError(`WorkPeriods with id of ${_.map(paidWorkPeriods, workPeriod => workPeriod.id)}
179-
has completed, partially-completed or in-progress payment status.`)
179+
has ${constants.PaymentStatus.COMPLETED}, ${constants.PaymentStatus.PARTIALLY_COMPLETED} or ${constants.PaymentStatus.IN_PROGRESS} payment status.`)
180180
}
181181
}
182182
// find related workPeriods to evaluate the changes
@@ -700,7 +700,7 @@ searchResourceBookings.schema = Joi.object().keys({
700700
page: Joi.page(),
701701
perPage: Joi.perPage(),
702702
sortBy: Joi.string().valid('id', 'rateType', 'startDate', 'endDate', 'customerRate', 'memberRate', 'status',
703-
'workPeriods.userHandle', 'workPeriods.daysWorked', 'workPeriods.customerRate', 'workPeriods.memberRate', 'workPeriods.paymentStatus'),
703+
'workPeriods.userHandle', 'workPeriods.daysWorked', 'workPeriods.daysPaid', 'workPeriods.paymentTotal', 'workPeriods.paymentStatus'),
704704
sortOrder: Joi.string().valid('desc', 'asc'),
705705
status: Joi.resourceBookingStatus(),
706706
startDate: Joi.date().format('YYYY-MM-DD'),

src/services/WorkPeriodPaymentService.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const helper = require('../common/helper')
1212
const logger = require('../common/logger')
1313
const errors = require('../common/errors')
1414
const models = require('../models')
15-
const { WorkPeriodPaymentStatus } = require('../../app-constants')
15+
const { PaymentStatus, WorkPeriodPaymentStatus } = require('../../app-constants')
1616
const { searchResourceBookings } = require('./ResourceBookingService')
1717

1818
const WorkPeriodPayment = models.WorkPeriodPayment
@@ -63,6 +63,9 @@ async function _createSingleWorkPeriodPaymentWithWorkPeriodAndResourceBooking (w
6363
if (_.isNil(correspondingResourceBooking.memberRate)) {
6464
throw new errors.ConflictError(`Can't find a member rate in ResourceBooking: ${correspondingResourceBooking.id} to calculate the amount`)
6565
}
66+
if (correspondingResourceBooking.memberRate <= 0) {
67+
throw new errors.ConflictError(`Can't process payment with member rate: ${correspondingResourceBooking.memberRate}. It must be higher than 0`)
68+
}
6669
workPeriodPayment.memberRate = correspondingResourceBooking.memberRate
6770
const maxPossibleDays = correspondingWorkPeriod.daysWorked - correspondingWorkPeriod.daysPaid
6871
if (workPeriodPayment.days > maxPossibleDays) {
@@ -380,7 +383,9 @@ async function createQueryWorkPeriodPayments (currentUser, criteria) {
380383
_checkUserPermissionForCRUWorkPeriodPayment(currentUser)
381384
const createdBy = await helper.getUserId(currentUser.userId)
382385
const query = criteria.query
383-
386+
if ((typeof criteria['workPeriods.paymentStatus']) === 'string') {
387+
criteria['workPeriods.paymentStatus'] = criteria['workPeriods.paymentStatus'].trim().split(',').map(ps => Joi.attempt({ paymentStatus: ps.trim() }, Joi.object().keys({ paymentStatus: Joi.string().valid(PaymentStatus.PENDING, PaymentStatus.PARTIALLY_COMPLETED, PaymentStatus.FAILED) })).paymentStatus)
388+
}
384389
const fields = _.join(_.uniq(_.concat(
385390
['id', 'billingAccountId', 'memberRate', 'customerRate', 'workPeriods.id', 'workPeriods.resourceBookingId', 'workPeriods.daysWorked', 'workPeriods.daysPaid'],
386391
_.map(_.keys(query), k => k === 'projectIds' ? 'projectId' : k))
@@ -418,7 +423,10 @@ createQueryWorkPeriodPayments.schema = Joi.object().keys({
418423
Joi.string(),
419424
Joi.array().items(Joi.number().integer())
420425
),
421-
'workPeriods.paymentStatus': Joi.string().valid('pending', 'partially-completed', 'failed'),
426+
'workPeriods.paymentStatus': Joi.alternatives(
427+
Joi.string(),
428+
Joi.array().items(Joi.string().valid(PaymentStatus.PENDING, PaymentStatus.PARTIALLY_COMPLETED, PaymentStatus.FAILED))
429+
),
422430
'workPeriods.startDate': Joi.date().format('YYYY-MM-DD'),
423431
'workPeriods.endDate': Joi.date().format('YYYY-MM-DD'),
424432
'workPeriods.userHandle': Joi.string()

src/services/WorkPeriodService.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ async function updateWorkPeriod (currentUser, id, data) {
279279
if (thisWeek.daysWorked < data.daysWorked) {
280280
throw new errors.BadRequestError(`Maximum allowed daysWorked is (${thisWeek.daysWorked})`)
281281
}
282-
if (data.daysWorked > oldValue.daysWorked && oldValue.paymentStatus === 'completed') {
283-
data.paymentStatus = 'partially-completed'
284-
} else if (data.daysWorked > oldValue.daysWorked && oldValue.paymentStatus === 'noDays') {
285-
data.paymentStatus = 'pending'
286-
} else if (data.daysWorked === oldValue.daysPaid && _.includes(['partially-completed', 'failed'], oldValue.paymentStatus)) {
287-
data.paymentStatus = 'completed'
282+
if (data.daysWorked > oldValue.daysWorked && oldValue.paymentStatus === constants.PaymentStatus.COMPLETED) {
283+
data.paymentStatus = constants.PaymentStatus.PARTIALLY_COMPLETED
284+
} else if (data.daysWorked > oldValue.daysWorked && oldValue.paymentStatus === constants.PaymentStatus.NO_DAYS) {
285+
data.paymentStatus = constants.PaymentStatus.PENDING
286+
} else if (data.daysWorked === oldValue.daysPaid && _.includes([constants.PaymentStatus.PARTIALLY_COMPLETED, constants.PaymentStatus.FAILED], oldValue.paymentStatus)) {
287+
data.paymentStatus = constants.PaymentStatus.COMPLETED
288288
} else if (data.daysWorked === 0) {
289-
data.paymentStatus = 'noDays'
289+
data.paymentStatus = constants.PaymentStatus.NO_DAYS
290290
}
291291
data.updatedBy = await helper.getUserId(currentUser.userId)
292292
const updated = await workPeriod.update(data)
@@ -319,11 +319,11 @@ partiallyUpdateWorkPeriod.schema = Joi.object().keys({
319319
*/
320320
async function deleteWorkPeriod (id) {
321321
const workPeriod = await WorkPeriod.findById(id, { withPayments: true })
322-
if (_.includes(['completed', 'partially-completed', 'in-progress'], workPeriod.paymentStatus)) {
323-
throw new errors.BadRequestError("Can't delete WorkPeriod with paymentStatus completed partially-completed, or in-progress")
322+
if (_.includes([constants.PaymentStatus.COMPLETED, constants.PaymentStatus.PARTIALLY_COMPLETED, constants.PaymentStatus.IN_PROGRESS], workPeriod.paymentStatus)) {
323+
throw new errors.BadRequestError(`Can't delete WorkPeriod with paymentStatus ${constants.PaymentStatus.COMPLETED}, ${constants.PaymentStatus.PARTIALLY_COMPLETED}, or ${constants.PaymentStatus.IN_PROGRESS}`)
324324
}
325-
if (_.some(workPeriod.payments, payment => ['completed', 'in-progress', 'shceduled'].indexOf(payment.status) !== -1)) {
326-
throw new errors.BadRequestError("Can't delete WorkPeriod if any associated WorkPeriodsPayment has status completed, shceduled or in-progress")
325+
if (_.some(workPeriod.payments, payment => [constants.WorkPeriodPaymentStatus.COMPLETED, constants.WorkPeriodPaymentStatus.IN_PROGRESS, constants.WorkPeriodPaymentStatus.SCHEDULED].indexOf(payment.status) !== -1)) {
326+
throw new errors.BadRequestError(`Can't delete WorkPeriod if any associated WorkPeriodsPayment has status ${constants.WorkPeriodPaymentStatus.COMPLETED}, ${constants.WorkPeriodPaymentStatus.SCHEDULED} or ${constants.WorkPeriodPaymentStatus.IN_PROGRESS}`)
327327
}
328328
await models.WorkPeriodPayment.destroy({
329329
where: {

0 commit comments

Comments
 (0)