Skip to content

Commit 896f811

Browse files
authored
Merge pull request #74 from imcaizheng/allow-search-all-jobs
fix and update the calculation of `totalPositions`
2 parents e29d842 + a921a92 commit 896f811

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/services/JobService.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,23 @@ deleteJob.schema = Joi.object().keys({
260260
/**
261261
* List jobs
262262
* @params {Object} criteria the search criteria
263+
* @params {Object} options the extra options to control the function
263264
* @returns {Object} the search result, contain total/page/perPage and result array
264265
*/
265-
async function searchJobs (criteria) {
266+
async function searchJobs (criteria, options = { returnAll: false }) {
266267
const page = criteria.page > 0 ? criteria.page : 1
267-
const perPage = criteria.perPage > 0 ? criteria.perPage : 20
268+
let perPage
269+
if (options.returnAll) {
270+
// To simplify the logic we are use a very large number for perPage
271+
// because in practice there could hardly be so many records to be returned.(also consider we are using filters in the meantime)
272+
// the number is limited by `index.max_result_window`, its default value is 10000, see
273+
// https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-max-result-window
274+
//
275+
// also see `ResourceBookingService.searchResourceBookings()`
276+
perPage = 10000
277+
} else {
278+
perPage = criteria.perPage > 0 ? criteria.perPage : 20
279+
}
268280
if (!criteria.sortBy) {
269281
criteria.sortBy = 'id'
270282
}
@@ -428,7 +440,8 @@ searchJobs.schema = Joi.object().keys({
428440
workload: Joi.workload(),
429441
status: Joi.jobStatus(),
430442
projectIds: Joi.array().items(Joi.number().integer()).single()
431-
}).required()
443+
}).required(),
444+
options: Joi.object()
432445
}).required()
433446

434447
module.exports = {

src/services/TeamService.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function _getAssignedResourceBookingsByProjectIds (projectIds) {
2828
* @returns the request result
2929
*/
3030
async function _getJobsByProjectIds (projectIds) {
31-
const { result } = await JobService.searchJobs({ projectIds })
31+
const { result } = await JobService.searchJobs({ projectIds }, { returnAll: true })
3232
return result
3333
}
3434

@@ -171,6 +171,10 @@ async function getTeamDetail (projects, isSearch = true) {
171171
// Count total positions
172172
res.totalPositions = 0
173173
for (const item of jobsTmp) {
174+
// only sum numPositions of jobs whose status is NOT cancelled or closed
175+
if (['cancelled', 'closed'].includes(item.status)) {
176+
continue
177+
}
174178
res.totalPositions += item.numPositions
175179
}
176180
} else {

0 commit comments

Comments
 (0)