From a378831149337e6411fc4c9d092da49b77d51b04 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Tue, 31 Jan 2023 09:21:07 +0100 Subject: [PATCH] refs#5066 @2h order vehicles by user warehouse --- .../back/methods/vehicle/currentWarehouse.js | 51 +++++++++++++++++++ modules/route/back/models/vehicle.js | 3 ++ modules/route/front/basic-data/index.html | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 modules/route/back/methods/vehicle/currentWarehouse.js create mode 100644 modules/route/back/models/vehicle.js diff --git a/modules/route/back/methods/vehicle/currentWarehouse.js b/modules/route/back/methods/vehicle/currentWarehouse.js new file mode 100644 index 000000000..41f94f209 --- /dev/null +++ b/modules/route/back/methods/vehicle/currentWarehouse.js @@ -0,0 +1,51 @@ +module.exports = Self => { + Self.remoteMethodCtx('currentWarehouse', { + description: 'Get the vehicles with the ones on the current warehouse first', + accessType: 'READ', + returns: { + type: 'array', + root: true + }, + http: { + path: `/currentWarehouse`, + verb: 'GET' + } + }); + + Self.currentWarehouse = async(ctx, options) => { + const models = Self.app.models; + const myOptions = {}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + let worker = await models.UserConfig.findOne({ + where: { + userFk: ctx.req.accessToken.userId + }, + }, myOptions); + + if (!worker) throw new Error('User not found'); + + const query = + `SELECT v.id, v.numberPlate, v.warehouseFk + FROM vehicle v + ORDER BY warehouseFk = ${worker.warehouseFk} DESC`; + + const result = await Self.rawSql(query, null, myOptions); + + if (tx) await tx.commit(); + return result; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/route/back/models/vehicle.js b/modules/route/back/models/vehicle.js new file mode 100644 index 000000000..d9a578e10 --- /dev/null +++ b/modules/route/back/models/vehicle.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/vehicle/currentWarehouse')(Self); +}; diff --git a/modules/route/front/basic-data/index.html b/modules/route/front/basic-data/index.html index 831599ae8..63cecd632 100644 --- a/modules/route/front/basic-data/index.html +++ b/modules/route/front/basic-data/index.html @@ -24,7 +24,7 @@