#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');
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}
`);
};
require('../methods/postcode/filter.js')(Self);
Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY')
return new UserError(`This postcode already exists`);