From 3af50dfc806d4a1f57a65d86c7822254c4d63e5d Mon Sep 17 00:00:00 2001 From: jorgebl Date: Wed, 24 Feb 2021 11:32:31 +0100 Subject: [PATCH] QA refactor --- .../back/methods/travel/getAverageDays.js | 49 ++++++++++-------- modules/travel/front/create/index.js | 9 ++-- modules/travel/front/create/index.spec.js | 50 ++++++++----------- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/modules/travel/back/methods/travel/getAverageDays.js b/modules/travel/back/methods/travel/getAverageDays.js index 7f6be936e..9a9649d84 100644 --- a/modules/travel/back/methods/travel/getAverageDays.js +++ b/modules/travel/back/methods/travel/getAverageDays.js @@ -9,7 +9,7 @@ module.exports = Self => { required: true }], returns: { - type: 'number', + type: 'object', root: true }, http: { @@ -21,38 +21,45 @@ module.exports = Self => { Self.getAverageDays = async agencyModeFk => { const conn = Self.dataSource.connector; let stmts = []; - let avgDays = {}; 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]); + 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); - 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`); + 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`); + + const avgDaysIndex = stmts.push(stmt) - 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); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql); - avgDays = result[resultAvgDays]; + const [avgDays] = result[avgDaysIndex]; return avgDays; }; diff --git a/modules/travel/front/create/index.js b/modules/travel/front/create/index.js index ba5bbd1ff..a85917ca8 100644 --- a/modules/travel/front/create/index.js +++ b/modules/travel/front/create/index.js @@ -22,15 +22,16 @@ class Controller extends Section { agencyModeFk: this.travel.agencyModeFk }; this.$http.get(query, {params}).then(res => { - if (res.data.length < 1) + if (!res.data) return; + const landed = new Date(value); - const futureDate = landed.getDate() + res.data[0].dayDuration; + const futureDate = landed.getDate() + res.data.dayDuration; landed.setDate(futureDate); this.travel.landed = landed; - this.travel.warehouseInFk = res.data[0].warehouseInFk; - this.travel.warehouseOutFk = res.data[0].warehouseOutFk; + this.travel.warehouseInFk = res.data.warehouseInFk; + this.travel.warehouseOutFk = res.data.warehouseOutFk; }); } diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index a9d5a1b02..e3f85d9ae 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -51,15 +51,30 @@ describe('Travel Component vnTravelCreate', () => { expect(controller.travel.warehouseOutFk).toBeUndefined(); }); - it(`should do nothing if it hasn't value on response data.`, () => { + it(`should do nothing if there's no response data.`, () => { controller.travel = {agencyModeFk: 4}; const tomorrow = new Date(); - const expectedResponse = [{ - agencyModeFk: 4, - warehouseInFk: undefined, - warehouseOutFk: undefined, - dayDuration: 0 - }]; + + const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; + $httpBackend.expectGET(query).respond(undefined); + controller.onShippedChange(tomorrow); + $httpBackend.flush(); + + expect(controller.travel.warehouseInFk).toBeUndefined(); + expect(controller.travel.warehouseOutFk).toBeUndefined(); + expect(controller.travel.dayDuration).toBeUndefined(); + }); + + 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() + 9); + const expectedResponse = { + id: 8, + dayDuration: 9, + warehouseInFk: 5, + warehouseOutFk: 1 + }; const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; $httpBackend.expectGET(query).respond(expectedResponse); @@ -68,27 +83,6 @@ describe('Travel Component vnTravelCreate', () => { expect(controller.travel.warehouseInFk).toEqual(expectedResponse.warehouseInFk); expect(controller.travel.warehouseOutFk).toEqual(expectedResponse.warehouseOutFk); - expect(controller.travel.dayDuration).toEqual(expectedResponse.dayDuration); - }); - - 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() + 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[0].warehouseInFk); - expect(controller.travel.warehouseOutFk).toEqual(expectedResponse[0].warehouseOutFk); }); }); });