Created test and optimized sql query

This commit is contained in:
Jorge Padawan 2021-02-11 08:32:16 +01:00
parent bb02a0de6b
commit 31e5d3fd1e
3 changed files with 36 additions and 44 deletions

View File

@ -19,16 +19,12 @@ module.exports = Self => {
Self.getTravelData = async agencyModeFk => { Self.getTravelData = async agencyModeFk => {
const query = ` const query = `
SELECT t.warehouseInFk as warehouseIn, SELECT t.id, t.warehouseInFk, t.warehouseOutFk,
t.warehouseOutFk as warehouseOut, (SELECT ROUND(AVG(DATEDIFF(t.landed, t.shipped )))
dayDuration
FROM travel t FROM travel t
JOIN ( WHERE t.agencyFk = ? LIMIT 50) AS dayDuration
SELECT ROUND(AVG(DATEDIFF(landed, shipped))) as dayDuration FROM travel t
FROM travel WHERE t.agencyFk = ? ORDER BY t.id DESC LIMIT 1;`;
WHERE agencyFk = 4) AS t2
WHERE t.agencyFk = 4
ORDER BY t.id DESC LIMIT 1;`;
const [result] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]); const [result] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]);
return result; return result;

View File

@ -8,9 +8,13 @@ class Controller extends Section {
} }
onShippedChange(value) { onShippedChange(value) {
const hasFilledProperties = this.travel.landed || this.travel.warehouseInFk || this.travel.warehouseOutFk; let hasFilledProperties;
let hasAgencyMode;
if (!this.travel.agencyModeFk || hasFilledProperties) if (this.travel) {
hasAgencyMode = Boolean(this.travel.agencyModeFk);
hasFilledProperties = this.travel.landed || this.travel.warehouseInFk || this.travel.warehouseOutFk;
}
if (!hasAgencyMode || hasFilledProperties)
return; return;
const query = `travels/getTravelData`; const query = `travels/getTravelData`;
@ -19,15 +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 warehouseIn = res.data.warehouseIn;
const warehouseOut = res.data.warehouseOut;
const futureDate = landed.getDate() + res.data.dayDuration; const futureDate = landed.getDate() + res.data.dayDuration;
landed.setDate(futureDate); landed.setDate(futureDate);
this.travel.landed = landed; this.travel.landed = landed;
this.travel.warehouseInFk = warehouseIn; this.travel.warehouseInFk = res.data.warehouseInFk;
this.travel.warehouseOutFk = warehouseOut; this.travel.warehouseOutFk = res.data.warehouseOutFk;
}); });
} }

View File

@ -41,38 +41,33 @@ describe('Travel Component vnTravelCreate', () => {
}); });
}); });
fdescribe('onShippedChange()', () => { describe('onShippedChange()', () => {
it(`should do nothing if there's no agencyMode or the travel has filled properties.`, () => { it(`should do nothing if there's no agencyModeFk in the travel.`, () => {
controller.agencyModeFk = {}; controller.travel = {};
controller.landed = {landed: 'January 30,2021, 00:00:00'}; controller.onShippedChange();
controller.warehouseInFk = {warehouseInFk: 4};
controller.warehouseOutFk = {warehouseOutFk: 4};
const landed = {landed: 'January 30,2021, 00:00:00'}; expect(controller.travel.landed).toBeUndefined();
const warehouseIn = {warehouseInFk: 4}; expect(controller.travel.warehouseInFk).toBeUndefined();
const warehouseOut = {warehouseOutFk: 4}; expect(controller.travel.warehouseOutFk).toBeUndefined();
const agencyModeFk = {};
expect(controller.agencyModeFk).toEqual(agencyModeFk);
expect(controller.landed).toEqual(landed);
expect(controller.warehouseInFk).toEqual(warehouseIn);
expect(controller.warehouseOutFk).toEqual(warehouseOut);
}); });
it(`should do 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 = { controller.travel = {agencyModeFk: 1};
agencyModeFk: 4, const tomorrow = new Date();
landed: 'January 30,2021, 00:00:00', tomorrow.setDate(tomorrow.getDate() + 1);
warehouseInFk: 4, const expectedResponse = {
warehouseOutFk: 4 dayDuration: 2,
warehouseInFk: 1,
warehouseOutFk: 2
}; };
const params = {agencyModeFk: 4};
$httpBackend.expectGET(`travels/getTravelData`, params).respond({agencyModeFk: 4}); const query = `travels/getTravelData?agencyModeFk=${controller.travel.agencyModeFk}`;
controller.onShippedChange(); $httpBackend.expectGET(query).respond(expectedResponse);
// $httpBackend.flush(); controller.onShippedChange(tomorrow);
$httpBackend.flush();
expect(controller.travel.agencyModeFk).toEqual(params.agencyModeFk); expect(controller.travel.warehouseInFk).toEqual(expectedResponse.warehouseInFk);
expect(controller.travel.warehouseOutFk).toEqual(expectedResponse.warehouseOutFk);
}); });
}); });
}); });