From b71b959b495d0a5e5158c33a4a2d78972d5167ea Mon Sep 17 00:00:00 2001 From: Jorge Padawan Date: Wed, 17 Feb 2021 11:01:50 +0100 Subject: [PATCH 1/6] 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); }); }); }); From 0732ff38b19ddf6e6b6d78ff803835ce0adcff46 Mon Sep 17 00:00:00 2001 From: Jorge Padawan Date: Wed, 17 Feb 2021 11:04:46 +0100 Subject: [PATCH 2/6] Quit clogs --- modules/travel/back/methods/travel/getAverageDays.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/travel/back/methods/travel/getAverageDays.js b/modules/travel/back/methods/travel/getAverageDays.js index 9a326652d..e56ac21e6 100644 --- a/modules/travel/back/methods/travel/getAverageDays.js +++ b/modules/travel/back/methods/travel/getAverageDays.js @@ -36,7 +36,6 @@ module.exports = Self => { 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 ))) @@ -53,7 +52,6 @@ module.exports = Self => { let result = await conn.executeStmt(sql); avgDays = result[resultAvgDays]; - console.log(avgDays); return avgDays; }; }; From 5692b5f07e09436835171d7700dc0abb9783b6e9 Mon Sep 17 00:00:00 2001 From: jorgebl Date: Mon, 22 Feb 2021 09:53:03 +0100 Subject: [PATCH 3/6] Added conditional and test --- .../travel/back/methods/travel/getAverageDays.js | 4 +++- modules/travel/front/create/index.js | 2 ++ modules/travel/front/create/index.spec.js | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/travel/back/methods/travel/getAverageDays.js b/modules/travel/back/methods/travel/getAverageDays.js index e56ac21e6..7f6be936e 100644 --- a/modules/travel/back/methods/travel/getAverageDays.js +++ b/modules/travel/back/methods/travel/getAverageDays.js @@ -31,7 +31,7 @@ module.exports = Self => { t.warehouseInFk, t.warehouseOutFk, t.landed, - t.shipped,+ + t.shipped, t.agencyFk FROM travel t WHERE t.agencyFk = ? LIMIT 50)`, [agencyModeFk]); @@ -43,6 +43,7 @@ module.exports = Self => { 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 @@ -52,6 +53,7 @@ module.exports = Self => { let result = await conn.executeStmt(sql); avgDays = result[resultAvgDays]; + return avgDays; }; }; diff --git a/modules/travel/front/create/index.js b/modules/travel/front/create/index.js index c76728e59..ba5bbd1ff 100644 --- a/modules/travel/front/create/index.js +++ b/modules/travel/front/create/index.js @@ -22,6 +22,8 @@ class Controller extends Section { agencyModeFk: this.travel.agencyModeFk }; this.$http.get(query, {params}).then(res => { + if (res.data.length < 1) + return; const landed = new Date(value); const futureDate = landed.getDate() + res.data[0].dayDuration; landed.setDate(futureDate); diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index dcb17d494..af1864ffd 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -41,7 +41,7 @@ describe('Travel Component vnTravelCreate', () => { }); }); - describe('onShippedChange()', () => { + fdescribe('onShippedChange()', () => { it(`should do nothing if there's no agencyModeFk in the travel.`, () => { controller.travel = {}; controller.onShippedChange(); @@ -51,6 +51,20 @@ describe('Travel Component vnTravelCreate', () => { expect(controller.travel.warehouseOutFk).toBeUndefined(); }); + it(`should do nothing if it hasn't value on response data.`, () => { + controller.travel = {agencyModeFk: 4}; + const tomorrow = new Date(); + const result = [{}]; + const expectedResponse = [{}]; + + const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; + $httpBackend.expectGET(query).respond(expectedResponse); + controller.onShippedChange(tomorrow); + $httpBackend.flush(); + + expect(expectedResponse).toEqual(result); + }); + it(`should fill the fields when it's selected a date and agency.`, () => { controller.travel = {agencyModeFk: 1}; const tomorrow = new Date(); From 71eeaadb43d095144901f0bbcef31c8c1bf3cc35 Mon Sep 17 00:00:00 2001 From: jorgebl Date: Tue, 23 Feb 2021 14:54:07 +0100 Subject: [PATCH 4/6] Removed focus on test --- modules/travel/front/create/index.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index af1864ffd..6771afce0 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -41,7 +41,7 @@ describe('Travel Component vnTravelCreate', () => { }); }); - fdescribe('onShippedChange()', () => { + describe('onShippedChange()', () => { it(`should do nothing if there's no agencyModeFk in the travel.`, () => { controller.travel = {}; controller.onShippedChange(); From 7895af8a542e51b7ea40dc86bc9cd46029f81278 Mon Sep 17 00:00:00 2001 From: jorgebl Date: Tue, 23 Feb 2021 17:00:31 +0100 Subject: [PATCH 5/6] Fixed test --- modules/travel/front/create/index.spec.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/travel/front/create/index.spec.js b/modules/travel/front/create/index.spec.js index 6771afce0..a9d5a1b02 100644 --- a/modules/travel/front/create/index.spec.js +++ b/modules/travel/front/create/index.spec.js @@ -54,15 +54,21 @@ describe('Travel Component vnTravelCreate', () => { it(`should do nothing if it hasn't value on response data.`, () => { controller.travel = {agencyModeFk: 4}; const tomorrow = new Date(); - const result = [{}]; - const expectedResponse = [{}]; + const expectedResponse = [{ + agencyModeFk: 4, + warehouseInFk: undefined, + warehouseOutFk: undefined, + dayDuration: 0 + }]; const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; $httpBackend.expectGET(query).respond(expectedResponse); controller.onShippedChange(tomorrow); $httpBackend.flush(); - expect(expectedResponse).toEqual(result); + 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.`, () => { From 3af50dfc806d4a1f57a65d86c7822254c4d63e5d Mon Sep 17 00:00:00 2001 From: jorgebl Date: Wed, 24 Feb 2021 11:32:31 +0100 Subject: [PATCH 6/6] 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); }); }); });