Skip to content

Fix up the sortBy parameter - (Top 2364) #727

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 7 commits into from
Jul 30, 2024
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ workflows:
only:
- dev
- feature/top-262-projectid-non-mandatory
- TOP-2364

- "build-qa":
context: org-global
Expand Down
97 changes: 14 additions & 83 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ async function searchChallenges(currentUser, criteria) {

const _hasAdminRole = hasAdminRole(currentUser);

const includeSelfService =
currentUser &&
(currentUser.isMachine ||
_hasAdminRole ||
_.includes(config.SELF_SERVICE_WHITELIST_HANDLES, currentUser.handle.toLowerCase()));

const includedTrackIds = _.isArray(criteria.trackIds) ? criteria.trackIds : [];
const includedTypeIds = _.isArray(criteria.typeIds) ? criteria.typeIds : [];

Expand Down Expand Up @@ -463,6 +457,18 @@ async function searchChallenges(currentUser, criteria) {
}

let sortByProp = criteria.sortBy ? criteria.sortBy : "created";

// Add '.keyword' to the end of the sort by prop for certain fields
// (TOP-2364)
if(sortByProp == "updatedBy" ||
sortByProp == "createdBy" ||
sortByProp == "name" ||
sortByProp == "type" ||
sortByProp == "status") {

sortByProp = sortByProp + ".keyword";
}

const sortOrderProp = criteria.sortOrder ? criteria.sortOrder : "desc";

const mustQuery = [];
Expand Down Expand Up @@ -647,83 +653,6 @@ async function searchChallenges(currentUser, criteria) {
});
}

if (!includeSelfService) {
mustQuery.push({
bool: {
should: [
{ bool: { must_not: { exists: { field: "legacy.selfService" } } } },
...(currentUser
? [
{
bool: {
must: [
{
bool: {
must_not: {
match_phrase: {
status: constants.challengeStatuses.New,
},
},
},
},
{
bool: {
must_not: {
match_phrase: {
status: constants.challengeStatuses.Draft,
},
},
},
},
{
bool: {
must_not: {
match_phrase: {
status: constants.challengeStatuses.Approved,
},
},
},
},
],
},
},
{
bool: {
must: { match_phrase: { createdBy: currentUser.handle } },
},
},
]
: [
{
bool: {
should: [
{
bool: {
must: {
match_phrase: {
status: constants.challengeStatuses.Active,
},
},
},
},
{
bool: {
must: {
match_phrase: {
status: constants.challengeStatuses.Completed,
},
},
},
},
],
},
},
]),
],
},
});
}

if (groupsQuery.length > 0) {
mustQuery.push({
bool: {
Expand Down Expand Up @@ -787,6 +716,7 @@ async function searchChallenges(currentUser, criteria) {
],
},
};

// Search with constructed query
let docs;
try {
Expand All @@ -795,6 +725,7 @@ async function searchChallenges(currentUser, criteria) {
? await esClient.search(esQuery)
: (await esClient.search(esQuery)).body;
} catch (e) {
logger.error(JSON.stringify(e));
// Catch error when the ES is fresh and has no data
docs = {
hits: {
Expand Down