Merge pull request '#6694 - Improve filter method' (!1947) from 6694_postcode_location_filter into dev
gitea/salix/pipeline/head There was a failure building this commit Details

Reviewed-on: #1947
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
Javier Segarra 2024-01-25 07:10:55 +00:00
commit 74e6e62961
2 changed files with 39 additions and 28 deletions

View File

@ -1,16 +1,11 @@
const {ParameterizedSQL} = require('loopback-connector'); const {ParameterizedSQL} = require('loopback-connector');
const {buildFilter, mergeFilters} = require('vn-loopback/util/filter'); const {buildFilter} = require('vn-loopback/util/filter');
// const {models} = require('vn-loopback/server/server');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('filter', { Self.remoteMethod('filter', {
description: description:
'Find all postcodes of the model matched by postcode, town, province or country.', 'Find all postcodes of the model matched by postcode, town, province or country.',
accessType: 'READ', accessType: 'READ',
returns: {
type: ['object'],
root: true,
},
accepts: [ accepts: [
{ {
arg: 'filter', arg: 'filter',
@ -25,6 +20,10 @@ module.exports = Self => {
http: {source: 'query'} http: {source: 'query'}
}, },
], ],
returns: {
type: ['object'],
root: true,
},
http: { http: {
path: `/filter`, path: `/filter`,
verb: 'GET', verb: 'GET',
@ -32,30 +31,34 @@ module.exports = Self => {
}); });
Self.filter = async(ctx, filter, options) => { Self.filter = async(ctx, filter, options) => {
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
filter = ctx?.filter ?? {};
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const where = buildFilter(ctx.args, (param, value) => { const where = buildFilter(filter?.where, (param, value) => {
switch (param) { switch (param) {
case 'search': case 'search':
return {or: [ return {
{'pc.code': {like: `%${value}%`}}, or: [
{'t.name': {like: `%${value}%`}}, {'pc.code': {like: `%${value}%`}},
{'p.name': {like: `%${value}%`}}, {'t.name': {like: `%${value}%`}},
{'c.country': {like: `%${value}%`}} {'p.name': {like: `%${value}%`}},
] {'c.country': {like: `%${value}%`}}
]
}; };
} }
}) ?? {}; }) ?? {};
delete ctx.filter.where;
filter = mergeFilters(ctx.args?.filter ?? {}, {where});
const stmts = []; const stmts = [];
let stmt; let stmt;
stmt = new ParameterizedSQL(` stmt = new ParameterizedSQL(`
SELECT SELECT
pc.townFk,
t.provinceFk,
p.countryFk,
pc.code, pc.code,
t.name as town, t.name as town,
p.name as province, p.name as province,
@ -67,7 +70,7 @@ module.exports = Self => {
JOIN country c on c.id = p.countryFk JOIN country c on c.id = p.countryFk
`); `);
stmt.merge(conn.makeSuffix(filter)); stmt.merge(conn.makeSuffix({where, ...ctx}));
const itemsIndex = stmts.push(stmt) - 1; const itemsIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');

View File

@ -7,13 +7,13 @@ describe('Postcode filter()', () => {
try { try {
const ctx = { const ctx = {
args: { filter: {
}, },
limit: 1
}; };
const results = await models.Postcode.filter(ctx, options); const results = await models.Postcode.filter(ctx, options);
expect(results.length).toBeGreaterThan(0); expect(results.length).toEqual(1);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
@ -27,8 +27,10 @@ describe('Postcode filter()', () => {
try { try {
const ctx = { const ctx = {
args: { filter: {
search: 46, where: {
search: 46,
}
}, },
}; };
const results = await models.Postcode.filter(ctx, options); const results = await models.Postcode.filter(ctx, options);
@ -47,8 +49,10 @@ describe('Postcode filter()', () => {
try { try {
const ctx = { const ctx = {
args: { filter: {
search: 'Alz', where: {
search: 'Alz',
}
}, },
}; };
const results = await models.Postcode.filter(ctx, options); const results = await models.Postcode.filter(ctx, options);
@ -67,8 +71,10 @@ describe('Postcode filter()', () => {
try { try {
const ctx = { const ctx = {
args: { filter: {
search: 'one', where: {
search: 'one',
}
}, },
}; };
const results = await models.Postcode.filter(ctx, options); const results = await models.Postcode.filter(ctx, options);
@ -87,8 +93,10 @@ describe('Postcode filter()', () => {
try { try {
const ctx = { const ctx = {
args: { filter: {
search: 'Ec', where: {
search: 'Ec',
}
}, },
}; };
const results = await models.Postcode.filter(ctx, options); const results = await models.Postcode.filter(ctx, options);