Skip to content

Commit

Permalink
feat: case insensitive name (#76)
Browse files Browse the repository at this point in the history
* fix: use case insensitive filter names

* fix: refactor filter count

* fix: update FilterPlanter test
  • Loading branch information
VLuisa authored Apr 30, 2021
1 parent 6305ddb commit e6577e0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 59 deletions.
42 changes: 23 additions & 19 deletions src/api/planters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
26 changes: 13 additions & 13 deletions src/models/FilterPlanter.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
35 changes: 8 additions & 27 deletions src/models/FilterPlanter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
});
});

0 comments on commit e6577e0

Please sign in to comment.