Skip to content

Commit

Permalink
fix: array checking
Browse files Browse the repository at this point in the history
  • Loading branch information
as1729 committed Sep 18, 2023
1 parent 7b6b302 commit 2a216e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/server/__tests__/api/grants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe('`/api/grants` endpoint', () => {
context('GET /api/grants/exportCSVNew', () => {
it('produces correct column format', async () => {
// We constrain the result to a single grant that's listed in seeds/dev/ref/grants.js
const query = '?criteria[includeKeywords]=Community Health Aide Program Tribal ';
const query = '?criteria[includeKeywords]=Community Health Aide Program Tribal';
const response = await fetchApi(`/grants/exportCSVNew${query}`, agencies.own, fetchOptions.staff);

expect(response.statusText).to.equal('OK');
Expand Down
10 changes: 7 additions & 3 deletions packages/server/src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,18 +381,22 @@ async function buildPaginationParams(args) {
return { currentPage, perPage, isLengthAware };
}

function isValidArray(value) {
return Array.isArray(value) && value.length > 0;
}

function buildTsqExpression(includeKeywords, excludeKeywords) {
if (!(includeKeywords?.length && excludeKeywords?.length)) {
if (!isValidArray(includeKeywords) && !isValidArray(excludeKeywords)) {
return null;
}

const signedKeywords = { include: [], exclude: [] };

// wrap phrases in double quotes and ensure keywords have the correct operator
if (includeKeywords?.length > 0) {
if (isValidArray(includeKeywords)) {
includeKeywords.forEach((ik) => { if (ik.indexOf(' ') > 0) { signedKeywords.include.push(`"${ik}"`); } else { signedKeywords.include.push(ik); } });
}
if (excludeKeywords?.length > 0) {
if (isValidArray(excludeKeywords)) {
excludeKeywords.forEach((ek) => { if (ek.indexOf(' ') > 0) { signedKeywords.exclude.push(`-"${ek}"`); } else { signedKeywords.exclude.push(`-${ek}`); } });
}

Expand Down

0 comments on commit 2a216e4

Please sign in to comment.