Skip to content

fix #332 #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1505,9 +1505,13 @@ paths:
name: workPeriods.paymentStatus
required: false
schema:
type: string
enum: ["pending", "partially-completed", "completed", "cancelled"]
description: The payment status.
oneOf:
- type: array
items:
type: string
enum: ["pending", "partially-completed", "completed", "cancelled"]
- type: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also defined enum: ["pending", "partially-completed", "completed", "cancelled"] for the type: string as it was before.

description: comma separated payment status.
- in: query
name: workPeriods.startDate
required: false
Expand Down Expand Up @@ -1956,9 +1960,13 @@ paths:
name: paymentStatus
required: false
schema:
type: string
enum: ["pending", "partially-completed", "completed", "cancelled"]
description: The payment status.
oneOf:
- type: array
items:
type: string
enum: ["pending", "partially-completed", "completed", "cancelled"]
- type: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

description: comma separated payment status.
- in: query
name: startDate
required: false
Expand Down
38 changes: 30 additions & 8 deletions src/services/ResourceBookingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ async function searchResourceBookings (currentUser, criteria, options = { return
return projectId
})
}
// `criteria[workPeriods.paymentStatus]` could be array of paymentStatus, or comma separated string of paymentStatus
// in case it's comma separated string of paymentStatus we have to convert it to an array of paymentStatus
if ((typeof criteria['workPeriods.paymentStatus']) === 'string') {
criteria['workPeriods.paymentStatus'] = criteria['workPeriods.paymentStatus'].trim().split(',').map(ps => Joi.attempt({ paymentStatus: ps.trim() }, Joi.object().keys({ paymentStatus: Joi.paymentStatus() })).paymentStatus)
}
const page = criteria.page
let perPage
if (options.returnAll) {
Expand Down Expand Up @@ -535,13 +540,21 @@ async function searchResourceBookings (currentUser, criteria, options = { return
if (!_.isEmpty(workPeriodFilters)) {
const workPeriodsMust = []
_.each(workPeriodFilters, (value, key) => {
workPeriodsMust.push({
term: {
[key]: {
value
if (key === 'workPeriods.paymentStatus') {
workPeriodsMust.push({
terms: {
[key]: value
}
}
})
})
} else {
workPeriodsMust.push({
term: {
[key]: {
value
}
}
})
}
})

esQuery.body.query.bool.must.push({
Expand All @@ -560,7 +573,13 @@ async function searchResourceBookings (currentUser, criteria, options = { return
_.each(_.omit(workPeriodFilters, 'workPeriods.userHandle'), (value, key) => {
key = key.split('.')[1]
_.each(resourceBookings, r => {
r.workPeriods = _.filter(r.workPeriods, { [key]: value })
r.workPeriods = _.filter(r.workPeriods, wp => {
if (key === 'paymentStatus') {
return _.includes(value, wp[key])
} else {
return wp[key] === value
}
})
})
})

Expand Down Expand Up @@ -664,7 +683,10 @@ searchResourceBookings.schema = Joi.object().keys({
Joi.string(),
Joi.array().items(Joi.number().integer())
),
'workPeriods.paymentStatus': Joi.paymentStatus(),
'workPeriods.paymentStatus': Joi.alternatives(
Joi.string(),
Joi.array().items(Joi.paymentStatus())
),
'workPeriods.startDate': Joi.date().format('YYYY-MM-DD'),
'workPeriods.endDate': Joi.date().format('YYYY-MM-DD'),
'workPeriods.userHandle': Joi.string()
Expand Down
25 changes: 21 additions & 4 deletions src/services/WorkPeriodService.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ async function updateWorkPeriod (currentUser, id, data) {
}
}

// await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue })
await helper.postEvent(config.TAAS_WORK_PERIOD_UPDATE_TOPIC, updated.toJSON(), { oldValue: oldValue, key: `resourceBooking.id:${data.resourceBookingId}` })
return updated.dataValues
}
Expand Down Expand Up @@ -401,6 +400,11 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
return resourceBookingId
})
}
// `criteria.paymentStatus` could be array of paymentStatus, or comma separated string of paymentStatus
// in case it's comma separated string of paymentStatus we have to convert it to an array of paymentStatus
if ((typeof criteria.paymentStatus) === 'string') {
criteria.paymentStatus = criteria.paymentStatus.trim().split(',').map(ps => Joi.attempt({ paymentStatus: ps.trim() }, Joi.object().keys({ paymentStatus: Joi.paymentStatus() })).paymentStatus)
}
const page = criteria.page
const perPage = criteria.perPage
if (!criteria.sortBy) {
Expand Down Expand Up @@ -436,7 +440,7 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
criteria.endDate = moment(criteria.endDate).format('YYYY-MM-DD')
}
// Apply filters
_.each(_.pick(criteria, ['resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate', 'paymentStatus']), (value, key) => {
_.each(_.pick(criteria, ['resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate']), (value, key) => {
esQuery.body.query.nested.query.bool.must.push({
term: {
[`workPeriods.${key}`]: {
Expand All @@ -445,6 +449,13 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
}
})
})
if (criteria.paymentStatus) {
esQuery.body.query.nested.query.bool.must.push({
terms: {
'workPeriods.paymentStatus': criteria.paymentStatus
}
})
}
// if criteria contains resourceBookingIds, filter resourceBookingId with this value
if (criteria.resourceBookingIds) {
esQuery.body.query.nested.query.bool.filter = [{
Expand All @@ -459,9 +470,12 @@ async function searchWorkPeriods (currentUser, criteria, options = { returnAll:
let workPeriods = _.reduce(body.hits.hits, (acc, resourceBooking) => _.concat(acc, resourceBooking._source.workPeriods), [])
// ESClient will return ResourceBookings with it's all nested WorkPeriods
// We re-apply WorkPeriod filters
_.each(_.pick(criteria, ['startDate', 'endDate', 'paymentStatus']), (value, key) => {
_.each(_.pick(criteria, ['startDate', 'endDate']), (value, key) => {
workPeriods = _.filter(workPeriods, { [key]: value })
})
if (criteria.paymentStatus) {
workPeriods = _.filter(workPeriods, wp => _.includes(criteria.paymentStatus, wp.paymentStatus))
}
workPeriods = _.sortBy(workPeriods, [criteria.sortBy])
if (criteria.sortOrder === 'desc') {
workPeriods = _.reverse(workPeriods)
Expand Down Expand Up @@ -522,7 +536,10 @@ searchWorkPeriods.schema = Joi.object().keys({
perPage: Joi.number().integer().min(1).max(10000).default(20),
sortBy: Joi.string().valid('id', 'resourceBookingId', 'userHandle', 'projectId', 'startDate', 'endDate', 'daysWorked', 'customerRate', 'memberRate', 'paymentStatus'),
sortOrder: Joi.string().valid('desc', 'asc'),
paymentStatus: Joi.paymentStatus(),
paymentStatus: Joi.alternatives(
Joi.string(),
Joi.array().items(Joi.paymentStatus())
),
startDate: Joi.date().format('YYYY-MM-DD'),
endDate: Joi.date().format('YYYY-MM-DD'),
userHandle: Joi.string(),
Expand Down