Skip to content

Commit edc9135

Browse files
Add support for searching by multiple types/tracks and type/track IDs
1 parent 5fb05a6 commit edc9135

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

docs/swagger.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ paths:
8080
will be ignored
8181
required: false
8282
type: string
83+
- name: types
84+
in: query
85+
description: >-
86+
Filter by multiple type abbreviation, exact match. If types is provided, typeIds will be
87+
ignored
88+
required: false
89+
type: array
90+
items:
91+
type: string
92+
- name: typeIds
93+
in: query
94+
description: >-
95+
Filter by multiple type IDs, exact match.
96+
required: false
97+
type: array
98+
items:
99+
type: string
100+
- name: tracks
101+
in: query
102+
description: >-
103+
Filter by multiple track abbreviation, exact match. If types is provided, trackIds will be
104+
ignored
105+
required: false
106+
type: array
107+
items:
108+
type: string
109+
- name: trackIds
110+
in: query
111+
description: >-
112+
Filter by multiple track IDs, exact match.
113+
required: false
114+
type: array
115+
items:
116+
type: string
83117
- name: typeId
84118
in: query
85119
description: >-

src/services/ChallengeService.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ async function searchChallenges (currentUser, criteria) {
110110
const perPage = criteria.perPage || 20
111111
const boolQuery = []
112112

113+
const includedTrackIds = []
114+
const includedTypeIds = []
115+
113116
if (criteria.type) {
114117
const typeSearchRes = await ChallengeTypeService.searchChallengeTypes({ abbreviation: criteria.type })
115118
if (typeSearchRes.total > 0) {
@@ -123,8 +126,32 @@ async function searchChallenges (currentUser, criteria) {
123126
}
124127
}
125128

126-
_.forIn(_.omit(criteria, ['type', 'name', 'description', 'page', 'perPage', 'tag', 'group', 'groups', 'memberId', 'ids', 'createdDateStart', 'createdDateEnd',
127-
'updatedDateStart', 'updatedDateEnd', 'startDateStart', 'startDateEnd', 'endDateStart', 'endDateEnd',
129+
if (criteria.types) {
130+
for (const t of criteria.types) {
131+
const typeSearchRes = await ChallengeTypeService.searchChallengeTypes({ abbreviation: t })
132+
if (typeSearchRes.total > 0) {
133+
includedTypeIds.push(_.get(typeSearchRes, 'result[0].id'))
134+
}
135+
}
136+
}
137+
if (criteria.tracks) {
138+
for (const t of criteria.types) {
139+
const trackSearchRes = await ChallengeTrackService.searchChallengeTracks({ abbreviation: t })
140+
if (trackSearchRes.total > 0) {
141+
includedTrackIds.push(_.get(trackSearchRes, 'result[0].id'))
142+
}
143+
}
144+
}
145+
146+
if (criteria.typeId) {
147+
includedTypeIds.push(criteria.typeId)
148+
}
149+
if (criteria.trackId) {
150+
includedTrackIds.push(criteria.trackId)
151+
}
152+
153+
_.forIn(_.omit(criteria, ['types', 'tracks', 'typeIds', 'trackIds', 'type', 'name', 'trackId', 'typeId', 'description', 'page', 'perPage', 'tag',
154+
'group', 'groups', 'memberId', 'ids', 'createdDateStart', 'createdDateEnd', 'updatedDateStart', 'updatedDateEnd', 'startDateStart', 'startDateEnd', 'endDateStart', 'endDateEnd',
128155
'tags', 'registrationStartDateStart', 'registrationStartDateEnd', 'currentPhaseName', 'submissionStartDateStart', 'submissionStartDateEnd',
129156
'registrationEndDateStart', 'registrationEndDateEnd', 'submissionEndDateStart', 'submissionEndDateEnd',
130157
'forumId', 'track', 'reviewType', 'confidentialityType', 'directProjectId', 'sortBy', 'sortOrder', 'isLightweight', 'isTask', 'taskIsAssigned', 'taskMemberId']), (value, key) => {
@@ -135,6 +162,22 @@ async function searchChallenges (currentUser, criteria) {
135162
}
136163
})
137164

165+
if (includedTypeIds.length > 0) {
166+
boolQuery.push({
167+
bool: {
168+
should: _.map(includedTypeIds, t => ({ match_phrase: { typeId: t } }))
169+
}
170+
})
171+
}
172+
173+
if (includedTrackIds.length > 0) {
174+
boolQuery.push({
175+
bool: {
176+
should: _.map(includedTrackIds, t => ({ match_phrase: { trackId: t } }))
177+
}
178+
})
179+
}
180+
138181
if (criteria.name) {
139182
boolQuery.push({ match: { name: `.*${criteria.name}.*` } })
140183
}
@@ -306,7 +349,7 @@ async function searchChallenges (currentUser, criteria) {
306349
// FIXME: Tech Debt
307350
let excludeTasks = false
308351
// If you're not looking for a particular type or a specific challenge, exclude tasks
309-
if (_.isUndefined(criteria.type) && _.isUndefined(criteria.typeId) && _.isUndefined(criteria.legacyId)) {
352+
if (_.isUndefined(criteria.type) && includedTypeIds.length === 0 && _.isUndefined(criteria.legacyId)) {
310353
excludeTasks = true
311354
}
312355
if (!_.isUndefined(criteria.memberId)) {
@@ -504,6 +547,10 @@ searchChallenges.schema = {
504547
id: Joi.optionalId(),
505548
confidentialityType: Joi.string(),
506549
directProjectId: Joi.number(),
550+
typeIds: Joi.array().items(Joi.optionalId()),
551+
trackIds: Joi.array().items(Joi.optionalId()),
552+
types: Joi.array().items(Joi.string()),
553+
tracks: Joi.array().items(Joi.string()),
507554
typeId: Joi.optionalId(),
508555
trackId: Joi.optionalId(),
509556
type: Joi.string(),

0 commit comments

Comments
 (0)