diff --git a/packages/server/__tests__/api/grants.test.js b/packages/server/__tests__/api/grants.test.js index 2cc284a5e..36200f76b 100644 --- a/packages/server/__tests__/api/grants.test.js +++ b/packages/server/__tests__/api/grants.test.js @@ -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'); diff --git a/packages/server/src/db/index.js b/packages/server/src/db/index.js index 60148afde..6ca4560aa 100755 --- a/packages/server/src/db/index.js +++ b/packages/server/src/db/index.js @@ -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}`); } }); }