Skip to content

Commit 3f131e0

Browse files
authored
Merge pull request #333 from xxcxy/dev
fix #332
2 parents f7706b7 + 66b97a5 commit 3f131e0

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

docs/swagger.yaml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,9 +1505,13 @@ paths:
15051505
name: workPeriods.paymentStatus
15061506
required: false
15071507
schema:
1508-
type: string
1509-
enum: ["pending", "partially-completed", "completed", "cancelled"]
1510-
description: The payment status.
1508+
oneOf:
1509+
- type: array
1510+
items:
1511+
type: string
1512+
enum: ["pending", "partially-completed", "completed", "cancelled"]
1513+
- type: string
1514+
description: comma separated payment status.
15111515
- in: query
15121516
name: workPeriods.startDate
15131517
required: false
@@ -1956,9 +1960,13 @@ paths:
19561960
name: paymentStatus
19571961
required: false
19581962
schema:
1959-
type: string
1960-
enum: ["pending", "partially-completed", "completed", "cancelled"]
1961-
description: The payment status.
1963+
oneOf:
1964+
- type: array
1965+
items:
1966+
type: string
1967+
enum: ["pending", "partially-completed", "completed", "cancelled"]
1968+
- type: string
1969+
description: comma separated payment status.
19621970
- in: query
19631971
name: startDate
19641972
required: false

src/services/ResourceBookingService.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ async function searchResourceBookings (currentUser, criteria, options = { return
454454
return projectId
455455
})
456456
}
457+
// `criteria[workPeriods.paymentStatus]` could be array of paymentStatus, or comma separated string of paymentStatus
458+
// in case it's comma separated string of paymentStatus we have to convert it to an array of paymentStatus
459+
if ((typeof criteria['workPeriods.paymentStatus']) === 'string') {
460+
criteria['workPeriods.paymentStatus'] = criteria['workPeriods.paymentStatus'].trim().split(',').map(ps => Joi.attempt({ paymentStatus: ps.trim() }, Joi.object().keys({ paymentStatus: Joi.paymentStatus() })).paymentStatus)
461+
}
457462
const page = criteria.page
458463
let perPage
459464
if (options.returnAll) {
@@ -535,13 +540,21 @@ async function searchResourceBookings (currentUser, criteria, options = { return
535540
if (!_.isEmpty(workPeriodFilters)) {
536541
const workPeriodsMust = []
537542
_.each(workPeriodFilters, (value, key) => {
538-
workPeriodsMust.push({
539-
term: {
540-
[key]: {
541-
value
543+
if (key === 'workPeriods.paymentStatus') {
544+
workPeriodsMust.push({
545+
terms: {
546+
[key]: value
542547
}
543-
}
544-
})
548+
})
549+
} else {
550+
workPeriodsMust.push({
551+
term: {
552+
[key]: {
553+
value
554+
}
555+
}
556+
})
557+
}
545558
})
546559

547560
esQuery.body.query.bool.must.push({
@@ -560,7 +573,13 @@ async function searchResourceBookings (currentUser, criteria, options = { return
560573
_.each(_.omit(workPeriodFilters, 'workPeriods.userHandle'), (value, key) => {
561574
key = key.split('.')[1]
562575
_.each(resourceBookings, r => {
563-
r.workPeriods = _.filter(r.workPeriods, { [key]: value })
576+
r.workPeriods = _.filter(r.workPeriods, wp => {
577+
if (key === 'paymentStatus') {
578+
return _.includes(value, wp[key])
579+
} else {
580+
return wp[key] === value
581+
}
582+
})
564583
})
565584
})
566585

@@ -664,7 +683,10 @@ searchResourceBookings.schema = Joi.object().keys({
664683
Joi.string(),
665684
Joi.array().items(Joi.number().integer())
666685
),
667-
'workPeriods.paymentStatus': Joi.paymentStatus(),
686+
'workPeriods.paymentStatus': Joi.alternatives(
687+
Joi.string(),
688+
Joi.array().items(Joi.paymentStatus())
689+
),
668690
'workPeriods.startDate': Joi.date().format('YYYY-MM-DD'),
669691
'workPeriods.endDate': Joi.date().format('YYYY-MM-DD'),
670692
'workPeriods.userHandle': Joi.string()

src/services/WorkPeriodService.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ async function updateWorkPeriod (currentUser, id, data) {
289289
}
290290
}
291291

292-
// await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue })
293292
await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue, key: `resourceBooking.id:${data.resourceBookingId}` })
294293
return updated.dataValues
295294
}
@@ -401,6 +400,11 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
401400
return resourceBookingId
402401
})
403402
}
403+
// `criteria.paymentStatus` could be array of paymentStatus, or comma separated string of paymentStatus
404+
// in case it's comma separated string of paymentStatus we have to convert it to an array of paymentStatus
405+
if ((typeof criteria.paymentStatus) === 'string') {
406+
criteria.paymentStatus = criteria.paymentStatus.trim().split(',').map(ps => Joi.attempt({ paymentStatus: ps.trim() }, Joi.object().keys({ paymentStatus: Joi.paymentStatus() })).paymentStatus)
407+
}
404408
const page = criteria.page
405409
const perPage = criteria.perPage
406410
if (!criteria.sortBy) {
@@ -436,7 +440,7 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
436440
criteria.endDate = moment(criteria.endDate).format('YYYY-MM-DD')
437441
}
438442
// Apply filters
439-
_.each(_.pick(criteria, ['resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate', 'paymentStatus']), (value, key) => {
443+
_.each(_.pick(criteria, ['resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate']), (value, key) => {
440444
esQuery.body.query.nested.query.bool.must.push({
441445
term: {
442446
[`workPeriods.${key}`]: {
@@ -445,6 +449,13 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
445449
}
446450
})
447451
})
452+
if (criteria.paymentStatus) {
453+
esQuery.body.query.nested.query.bool.must.push({
454+
terms: {
455+
'workPeriods.paymentStatus': criteria.paymentStatus
456+
}
457+
})
458+
}
448459
// if criteria contains resourceBookingIds, filter resourceBookingId with this value
449460
if (criteria.resourceBookingIds) {
450461
esQuery.body.query.nested.query.bool.filter = [{
@@ -459,9 +470,12 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
459470
let workPeriods = _.reduce(body.hits.hits, (acc, resourceBooking) => _.concat(acc, resourceBooking._source.workPeriods), [])
460471
// ESClient will return ResourceBookings with it's all nested WorkPeriods
461472
// We re-apply WorkPeriod filters
462-
_.each(_.pick(criteria, ['startDate', 'endDate', 'paymentStatus']), (value, key) => {
473+
_.each(_.pick(criteria, ['startDate', 'endDate']), (value, key) => {
463474
workPeriods = _.filter(workPeriods, { [key]: value })
464475
})
476+
if (criteria.paymentStatus) {
477+
workPeriods = _.filter(workPeriods, wp => _.includes(criteria.paymentStatus, wp.paymentStatus))
478+
}
465479
workPeriods = _.sortBy(workPeriods, [criteria.sortBy])
466480
if (criteria.sortOrder === 'desc') {
467481
workPeriods = _.reverse(workPeriods)
@@ -522,7 +536,10 @@ searchWorkPeriods.schema = Joi.object().keys({
522536
perPage: Joi.number().integer().min(1).max(10000).default(20),
523537
sortBy: Joi.string().valid('id', 'resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate', 'daysWorked', 'customerRate', 'memberRate', 'paymentStatus'),
524538
sortOrder: Joi.string().valid('desc', 'asc'),
525-
paymentStatus: Joi.paymentStatus(),
539+
paymentStatus: Joi.alternatives(
540+
Joi.string(),
541+
Joi.array().items(Joi.paymentStatus())
542+
),
526543
startDate: Joi.date().format('YYYY-MM-DD'),
527544
endDate: Joi.date().format('YYYY-MM-DD'),
528545
userHandle: Joi.string(),

0 commit comments

Comments
 (0)