const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { Self.remoteMethodCtx('getLanded', { description: 'Returns the first shipped and landed possible for params', accessType: 'READ', accepts: [{ arg: 'shipped', type: 'date', required: true }, { arg: 'addressFk', type: 'number', required: true }, { arg: 'agencyModeFk', type: 'number', required: true }, { arg: 'warehouseFk', type: 'number', required: true }], returns: { type: 'object', root: true }, http: { path: `/getLanded`, verb: 'get' } }); Self.getLanded = async(ctx, shipped, addressFk, agencyModeFk, warehouseFk) => { const userId = ctx.req.accessToken.userId; const models = Self.app.models; const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss'); let showExpired = false; if (isProductionBoss) showExpired = true; let stmts = []; stmts.push(new ParameterizedSQL( `CALL vn.zone_getLanded(?, ?, ?, ?, ?)`, [ shipped, addressFk, agencyModeFk, warehouseFk, showExpired ] )); let rsIndex = stmts.push( `SELECT * FROM tmp.zoneGetLanded`) - 1; stmts.push(`DROP TEMPORARY TABLE tmp.zoneGetLanded`); let sql = ParameterizedSQL.join(stmts, ';'); let landed = await Self.rawStmt(sql); return landed[rsIndex][0]; }; };