From b71b959b495d0a5e5158c33a4a2d78972d5167ea Mon Sep 17 00:00:00 2001 From: Jorge Padawan Date: Wed, 17 Feb 2021 11:01:50 +0100 Subject: [PATCH] Formated sql and tests passed --- .../back/methods/travel/getAverageDays.js | 43 +++++++++++++++---- modules/travel/front/create/index.js | 6 +-- modules/travel/front/create/index.spec.js | 17 ++++---- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/modules/travel/back/methods/travel/getAverageDays.js b/modules/travel/back/methods/travel/getAverageDays.js index bcc98dbe2..9a326652d 100644 --- a/modules/travel/back/methods/travel/getAverageDays.js +++ b/modules/travel/back/methods/travel/getAverageDays.js @@ -1,3 +1,4 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { Self.remoteMethod('getAverageDays', { description: 'Returns the average days duration and the two warehouses of the travel.', @@ -18,15 +19,41 @@ module.exports = Self => { }); Self.getAverageDays = async agencyModeFk => { - const query = ` - SELECT t.id, t.warehouseInFk, t.warehouseOutFk, - (SELECT ROUND(AVG(DATEDIFF(t.landed, t.shipped ))) - FROM travel t - WHERE t.agencyFk = ? LIMIT 50) AS dayDuration - FROM travel t - WHERE t.agencyFk = ? ORDER BY t.id DESC LIMIT 1;`; + const conn = Self.dataSource.connector; + let stmts = []; + let avgDays = {}; - const [avgDays] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]); + stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.travel'); + + stmt = new ParameterizedSQL(` + CREATE TEMPORARY TABLE tmp.travel ( + SELECT t.id, + t.warehouseInFk, + t.warehouseOutFk, + t.landed, + t.shipped,+ + t.agencyFk + FROM travel t + WHERE t.agencyFk = ? LIMIT 50)`, [agencyModeFk]); + stmts.push(stmt); + console.log(stmts); + + stmt = new ParameterizedSQL(`SELECT t.id, t.warehouseInFk, t.warehouseOutFk, + (SELECT ROUND(AVG(DATEDIFF(t.landed, t.shipped ))) + FROM tmp.travel t + WHERE t.agencyFk ORDER BY id DESC LIMIT 50) AS dayDuration + FROM tmp.travel t + WHERE t.agencyFk ORDER BY t.id DESC LIMIT 1`); + let resultAvgDays = stmts.push(stmt) - 1; + stmts.push( + `DROP TEMPORARY TABLE + tmp.travel`); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await conn.executeStmt(sql); + + avgDays = result[resultAvgDays]; + console.log(avgDays); return avgDays; }; }; diff --git a/modules/travel/front/create/index.js b/modules/travel/front/create/index.js index cf0b2f382..c76728e59 100644 --- a/modules/travel/front/create/index.js +++ b/modules/travel/front/create/index.js @@ -23,12 +23,12 @@ class Controller extends Section { }; this.$http.get(query, {params}).then(res => { const landed = new Date(value); - const futureDate = landed.getDate() + res.data.dayDuration; + const futureDate = landed.getDate() + res.data[0].dayDuration; landed.setDate(futureDate); this.travel.landed = landed; - this.travel.warehouseInFk = res.data.warehouseInFk; - this.travel.warehouseOutFk = res.data.warehouseOutFk; + this.travel.warehouseInFk = res.data[0].warehouseInFk; + this.travel.warehouseOutFk = res.data[0].warehouseOutFk; }); } diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index b59530604..dcb17d494 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -54,20 +54,21 @@ describe('Travel Component vnTravelCreate', () => { it(`should fill the fields when it's selected a date and agency.`, () => { controller.travel = {agencyModeFk: 1}; const tomorrow = new Date(); - tomorrow.setDate(tomorrow.getDate() + 1); - const expectedResponse = { - dayDuration: 2, - warehouseInFk: 1, - warehouseOutFk: 2 - }; + tomorrow.setDate(tomorrow.getDate() + 9); + const expectedResponse = [{ + id: 8, + dayDuration: 9, + warehouseInFk: 5, + warehouseOutFk: 1 + }]; const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; $httpBackend.expectGET(query).respond(expectedResponse); controller.onShippedChange(tomorrow); $httpBackend.flush(); - expect(controller.travel.warehouseInFk).toEqual(expectedResponse.warehouseInFk); - expect(controller.travel.warehouseOutFk).toEqual(expectedResponse.warehouseOutFk); + expect(controller.travel.warehouseInFk).toEqual(expectedResponse[0].warehouseInFk); + expect(controller.travel.warehouseOutFk).toEqual(expectedResponse[0].warehouseOutFk); }); }); });