refs #6694 feat: add filter for postcodes
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Javier Segarra 2024-01-17 08:34:15 +01:00
parent f75362a6d7
commit 30b505aa8f
1 changed files with 32 additions and 0 deletions

View File

@ -1,6 +1,38 @@
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', {
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`);