Formated sql and tests passed
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Padawan 2021-02-17 11:01:50 +01:00
parent 661950f94a
commit b71b959b49
3 changed files with 47 additions and 19 deletions

View File

@ -1,3 +1,4 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('getAverageDays', { Self.remoteMethod('getAverageDays', {
description: 'Returns the average days duration and the two warehouses of the travel.', description: 'Returns the average days duration and the two warehouses of the travel.',
@ -18,15 +19,41 @@ module.exports = Self => {
}); });
Self.getAverageDays = async agencyModeFk => { Self.getAverageDays = async agencyModeFk => {
const query = ` const conn = Self.dataSource.connector;
SELECT t.id, t.warehouseInFk, t.warehouseOutFk, let stmts = [];
(SELECT ROUND(AVG(DATEDIFF(t.landed, t.shipped ))) let avgDays = {};
FROM travel t
WHERE t.agencyFk = ? LIMIT 50) AS dayDuration
FROM travel t
WHERE t.agencyFk = ? ORDER BY t.id DESC LIMIT 1;`;
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; return avgDays;
}; };
}; };

View File

@ -23,12 +23,12 @@ class Controller extends Section {
}; };
this.$http.get(query, {params}).then(res => { this.$http.get(query, {params}).then(res => {
const landed = new Date(value); const landed = new Date(value);
const futureDate = landed.getDate() + res.data.dayDuration; const futureDate = landed.getDate() + res.data[0].dayDuration;
landed.setDate(futureDate); landed.setDate(futureDate);
this.travel.landed = landed; this.travel.landed = landed;
this.travel.warehouseInFk = res.data.warehouseInFk; this.travel.warehouseInFk = res.data[0].warehouseInFk;
this.travel.warehouseOutFk = res.data.warehouseOutFk; this.travel.warehouseOutFk = res.data[0].warehouseOutFk;
}); });
} }

View File

@ -54,20 +54,21 @@ describe('Travel Component vnTravelCreate', () => {
it(`should fill the fields when it's selected a date and agency.`, () => { it(`should fill the fields when it's selected a date and agency.`, () => {
controller.travel = {agencyModeFk: 1}; controller.travel = {agencyModeFk: 1};
const tomorrow = new Date(); const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1); tomorrow.setDate(tomorrow.getDate() + 9);
const expectedResponse = { const expectedResponse = [{
dayDuration: 2, id: 8,
warehouseInFk: 1, dayDuration: 9,
warehouseOutFk: 2 warehouseInFk: 5,
}; warehouseOutFk: 1
}];
const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`; const query = `travels/getAverageDays?agencyModeFk=${controller.travel.agencyModeFk}`;
$httpBackend.expectGET(query).respond(expectedResponse); $httpBackend.expectGET(query).respond(expectedResponse);
controller.onShippedChange(tomorrow); controller.onShippedChange(tomorrow);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.travel.warehouseInFk).toEqual(expectedResponse.warehouseInFk); expect(controller.travel.warehouseInFk).toEqual(expectedResponse[0].warehouseInFk);
expect(controller.travel.warehouseOutFk).toEqual(expectedResponse.warehouseOutFk); expect(controller.travel.warehouseOutFk).toEqual(expectedResponse[0].warehouseOutFk);
}); });
}); });
}); });