Merge pull request 'Formated sql and tests passed' (#551) from 2603-travel_create_autocomplete_date into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #551
Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
Carlos Jimenez Ruiz 2021-02-24 10:53:06 +00:00
commit 7ab77e6662
3 changed files with 65 additions and 13 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.',
@ -8,7 +9,7 @@ module.exports = Self => {
required: true required: true
}], }],
returns: { returns: {
type: 'number', type: 'object',
root: true root: true
}, },
http: { http: {
@ -18,15 +19,48 @@ 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 )))
FROM travel t stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.travel');
WHERE t.agencyFk = ? LIMIT 50) AS dayDuration
FROM travel t stmt = new ParameterizedSQL(`
WHERE t.agencyFk = ? ORDER BY t.id DESC LIMIT 1;`; 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`);
const avgDaysIndex = stmts.push(stmt) - 1;
stmts.push(
`DROP TEMPORARY TABLE
tmp.travel`);
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const [avgDays] = result[avgDaysIndex];
const [avgDays] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]);
return avgDays; return avgDays;
}; };
}; };

View File

@ -22,6 +22,9 @@ class Controller extends Section {
agencyModeFk: this.travel.agencyModeFk agencyModeFk: this.travel.agencyModeFk
}; };
this.$http.get(query, {params}).then(res => { this.$http.get(query, {params}).then(res => {
if (!res.data)
return;
const landed = new Date(value); const landed = new Date(value);
const futureDate = landed.getDate() + res.data.dayDuration; const futureDate = landed.getDate() + res.data.dayDuration;
landed.setDate(futureDate); landed.setDate(futureDate);

View File

@ -51,14 +51,29 @@ describe('Travel Component vnTravelCreate', () => {
expect(controller.travel.warehouseOutFk).toBeUndefined(); expect(controller.travel.warehouseOutFk).toBeUndefined();
}); });
it(`should do nothing if there's no response data.`, () => {
controller.travel = {agencyModeFk: 4};
const tomorrow = new Date();
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.`, () => { 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}`;