#6694 Filter postcode by multiple fields #1939

Merged
jsegarra merged 7 commits from 6694_postcode_location_filter into dev 2024-01-22 07:22:59 +00:00
2 changed files with 35 additions and 32 deletions
Showing only changes of commit 71f387f626 - Show all commits

View File

@ -0,0 +1,34 @@
module.exports = Self => {
Self.remoteMethod('filter', {
description:
'Find all postcodes of the model matched by postcode, town, province or country.',
accessType: 'READ',
returns: {
type: ['object'],
root: true,
},
http: {
path: `/filter`,
verb: 'GET',
},
});
Self.filter = async(ctx, filter, options) => {
const {Postcode} = Self.app.models;
let {value} = ctx.where;
let limit = ctx.where?.limit ?? 30;
return await Postcode.rawSql(`
SELECT pc.code, t.name as town, p.name as province , c.country
FROM postCode pc
JOIN town t on t.id = pc.townFk
JOIN province p on p.id = t.provinceFk
JOIN country c on c.id = p.countryFk
WHERE
pc.code like '%${value}%'
or t.name like '%${value}%'
or p.name like '%${value}%'
or c.country like '%${value}%'
LIMIT ${limit}
`);
};
};

View File

@ -1,38 +1,7 @@
let UserError = require('vn-loopback/util/user-error'); let UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('filter', { require('../methods/postcode/filter.js')(Self);
jsegarra marked this conversation as resolved Outdated
Outdated
Review

La ruta iria en su propio archivo.

La ruta iria en su propio archivo.

Quieres decir que haga un archivo llamado "filter"(pej) para ese método?

Quieres decir que haga un archivo llamado "filter"(pej) para ese método?
Outdated
Review

Exacto

Exacto

Corregido 71f387f626

Corregido 71f387f626add3952755e0c83b6ead88bd435941
description:
'Find all postcodes of the model matched by postcode, town, province or country.',
accessType: 'READ',
returns: {
type: ['object'],
root: true,
},
http: {
path: `/filter`,
verb: 'GET',
},
});
Self.filter = async(ctx, filter, options) => {
const {Postcode} = Self.app.models;
let {value} = ctx.where;
let limit = ctx.where?.limit ?? 30;
return await Postcode.rawSql(`
SELECT pc.code, t.name as town, p.name as province , c.country
FROM postCode pc
JOIN town t on t.id = pc.townFk
JOIN province p on p.id = t.provinceFk
JOIN country c on c.id = p.countryFk
WHERE
pc.code like '%${value}%'
or t.name like '%${value}%'
or p.name like '%${value}%'
or c.country like '%${value}%'
LIMIT ${limit}
`);
};
Self.rewriteDbError(function(err) { Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY') if (err.code === 'ER_DUP_ENTRY')
return new UserError(`This postcode already exists`); return new UserError(`This postcode already exists`);