From e6577e0ab4b5a7a82796048b0b0c9fed13b30846 Mon Sep 17 00:00:00 2001 From: Luisa Vasquez Date: Fri, 30 Apr 2021 15:45:44 -0400 Subject: [PATCH] feat: case insensitive name (#76) * fix: use case insensitive filter names * fix: refactor filter count * fix: update FilterPlanter test --- src/api/planters.js | 42 +++++++++++++++++--------------- src/models/FilterPlanter.js | 26 ++++++++++---------- src/models/FilterPlanter.test.js | 35 ++++++-------------------- 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/src/api/planters.js b/src/api/planters.js index 9ab22eb41..17aee2c2b 100644 --- a/src/api/planters.js +++ b/src/api/planters.js @@ -19,22 +19,27 @@ export default { }, getPlanters({ skip, rowsPerPage, orderBy = 'id', order = 'desc', filter }) { - const query = - `${process.env.REACT_APP_API_ROOT}/api/${getOrganization()}planter?` + - `filter[order]=${orderBy} ${order}&` + - `filter[limit]=${rowsPerPage}&` + - `filter[skip]=${skip}&` + - `filter[fields][firstName]=true&` + - `filter[fields][lastName]=true&` + - `filter[fields][imageUrl]=true&` + - `filter[fields][email]=true&` + - `filter[fields][phone]=true&` + - `filter[fields][personId]=true&` + - `filter[fields][organization]=true&` + - `filter[fields][organizationId]=true&` + - `filter[fields][id]=true&` + - //the filter query - (filter ? filter.getBackloopString() : ''); + const where = filter ? filter.getWhereObj() : {}; + const planterFilter = { + where: { ...where, active: true }, + order: [`${orderBy} ${order}`], + limit: rowsPerPage, + skip: skip, + fields: { + firstName: true, + lastName: true, + imageUrl: true, + email: true, + phone: true, + personId: true, + organization: true, + organizationId: true, + id: true, + }, + }; + const query = `${ + process.env.REACT_APP_API_ROOT + }/api/${getOrganization()}planter?filter=${JSON.stringify(planterFilter)}`; return fetch(query, { headers: { 'content-type': 'application/json', @@ -46,11 +51,10 @@ export default { }, getCount({ filter }) { + const filterObj = filter ? filter.getWhereObj() : {}; const query = `${ process.env.REACT_APP_API_ROOT - }/api/${getOrganization()}planter/count?${ - filter && filter.getBackloopString(false) - }`; + }/api/${getOrganization()}planter/count?where=${JSON.stringify(filterObj)}`; return fetch(query, { headers: { 'content-type': 'application/json', diff --git a/src/models/FilterPlanter.js b/src/models/FilterPlanter.js index c0d9eb496..6237ae349 100644 --- a/src/models/FilterPlanter.js +++ b/src/models/FilterPlanter.js @@ -1,38 +1,38 @@ /* - * A simple model for tree filter + * A simple model for planter filter */ export default class Filter { constructor(options) { Object.assign(this, options); } - - getBackloopString(includeFilterString = true) { - //{{{ - let result = ''; - const prefix = includeFilterString ? '&filter[where]' : '&where'; + getWhereObj() { + let where = {}; if (this.personId) { - result += `${prefix}[personId]=${this.personId}`; + where.personId = this.personId; } if (this.id) { - result += `${prefix}[id]=${this.id}`; + where.id = this.id; } if (this.firstName) { - result += `${prefix}[firstName]=${this.firstName}`; + where.firstName = { + ilike: this.firstName, + }; } if (this.lastName) { - result += `${prefix}[lastName]=${this.lastName}`; + where.lastName = { + ilike: this.lastName, + }; } if (this.organizationId) { - result += `${prefix}[organizationId]=${this.organizationId}`; + where.organizationId = this.organizationId; } - return result; - //}}} + return where; } } diff --git a/src/models/FilterPlanter.test.js b/src/models/FilterPlanter.test.js index edeba2e3c..1873e429b 100644 --- a/src/models/FilterPlanter.test.js +++ b/src/models/FilterPlanter.test.js @@ -12,33 +12,14 @@ describe('Filter', () => { }); }); - it('getBackloopString() should be: ', () => { - expect(filterPlanter.getBackloopString()).toEqual( - expect.stringContaining('&filter[where][personId]=1'), - ); - expect(filterPlanter.getBackloopString()).toEqual( - expect.stringContaining('&filter[where][firstName]=fn'), - ); - expect(filterPlanter.getBackloopString()).toEqual( - expect.stringContaining('&filter[where][lastName]=ln'), - ); - expect(filterPlanter.getBackloopString()).toEqual( - expect.stringContaining('&filter[where][id]=1'), - ); - }); - - it('getBackloopString(false) should be: ', () => { - expect(filterPlanter.getBackloopString(false)).toEqual( - expect.stringContaining('&where[personId]=1'), - ); - expect(filterPlanter.getBackloopString(false)).toEqual( - expect.stringContaining('&where[firstName]=fn'), - ); - expect(filterPlanter.getBackloopString(false)).toEqual( - expect.stringContaining('&where[lastName]=ln'), - ); - expect(filterPlanter.getBackloopString(false)).toEqual( - expect.stringContaining('&where[id]=1'), + it('getWhereObj() should be: ', () => { + expect(filterPlanter.getWhereObj()).toEqual( + expect.objectContaining({ + firstName: { ilike: 'fn' }, + id: 1, + lastName: { ilike: 'ln' }, + personId: 1, + }), ); }); });