diff --git a/modules/route/back/methods/vehicle/currentWarehouse.js b/modules/route/back/methods/vehicle/currentWarehouse.js index 41f94f209..8959f00b5 100644 --- a/modules/route/back/methods/vehicle/currentWarehouse.js +++ b/modules/route/back/methods/vehicle/currentWarehouse.js @@ -1,7 +1,14 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + module.exports = Self => { Self.remoteMethodCtx('currentWarehouse', { description: 'Get the vehicles with the ones on the current warehouse first', accessType: 'READ', + accepts: [{ + arg: 'filter', + type: 'object', + description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string` + }], returns: { type: 'array', root: true @@ -12,8 +19,9 @@ module.exports = Self => { } }); - Self.currentWarehouse = async(ctx, options) => { + Self.currentWarehouse = async(ctx, filter, options) => { const models = Self.app.models; + const conn = Self.dataSource.connector; const myOptions = {}; let tx; @@ -34,15 +42,44 @@ module.exports = Self => { if (!worker) throw new Error('User not found'); - const query = - `SELECT v.id, v.numberPlate, v.warehouseFk + /* const query = + `SELECT v.id, v.numberPlate, v.warehouseFk FROM vehicle v - ORDER BY warehouseFk = ${worker.warehouseFk} DESC`; + ORDER BY warehouseFk = ${worker.warehouseFk} DESC`;*/ - const result = await Self.rawSql(query, null, myOptions); + let stmt = new ParameterizedSQL( + `SELECT v.id, v.numberPlate, v.warehouseFk + FROM vehicle v`, + null, myOptions); - if (tx) await tx.commit(); - return result; + if (filter.order) { + delete filter.order; + + let order = 'ORDER BY warehouseFk = ' + worker.warehouseFk + ' DESC'; + + if (!filter.where) { + stmt.merge(order); + stmt.merge(conn.makeSuffix(filter)); + } else { + let limit = filter.limit; + delete filter.limit; + stmt.merge(conn.makeSuffix(filter)); + stmt.merge(order); + stmt.merge(conn.makeLimit({limit})); + } + + const result = await conn.executeStmt(stmt); + + if (tx) await tx.commit(); + return result; + } else { + stmt.merge(conn.makeSuffix(filter)); + + const result = await conn.executeStmt(stmt); + + if (tx) await tx.commit(); + return result; + } } catch (e) { if (tx) await tx.rollback(); throw e; diff --git a/modules/route/front/basic-data/index.html b/modules/route/front/basic-data/index.html index 63cecd632..fa848862d 100644 --- a/modules/route/front/basic-data/index.html +++ b/modules/route/front/basic-data/index.html @@ -21,6 +21,7 @@
{{name}} - {{nickname}}
+ {{$ctrl.route.vehicleFk}}