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 => {
const query = `
SELECT t.warehouseInFk as warehouseIn,
t.warehouseOutFk as warehouseOut,
dayDuration
FROM travel t
JOIN (
SELECT ROUND(AVG(DATEDIFF(landed, shipped))) as dayDuration
FROM travel
WHERE agencyFk = 4) AS t2
WHERE t.agencyFk = 4
ORDER BY t.id DESC LIMIT 1;`;
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 [result] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]);
return result;

View File

@ -8,9 +8,13 @@ class Controller extends Section {
}
onShippedChange(value) {
const hasFilledProperties = this.travel.landed || this.travel.warehouseInFk || this.travel.warehouseOutFk;
if (!this.travel.agencyModeFk || hasFilledProperties)
let hasFilledProperties;
let hasAgencyMode;
if (this.travel) {
hasAgencyMode = Boolean(this.travel.agencyModeFk);
hasFilledProperties = this.travel.landed || this.travel.warehouseInFk || this.travel.warehouseOutFk;
}
if (!hasAgencyMode || hasFilledProperties)
return;
const query = `travels/getTravelData`;
@ -19,15 +23,12 @@ class Controller extends Section {
};
this.$http.get(query, {params}).then(res => {
const landed = new Date(value);
const warehouseIn = res.data.warehouseIn;
const warehouseOut = res.data.warehouseOut;
const futureDate = landed.getDate() + res.data.dayDuration;
landed.setDate(futureDate);
this.travel.landed = landed;
this.travel.warehouseInFk = warehouseIn;
this.travel.warehouseOutFk = warehouseOut;
this.travel.warehouseInFk = res.data.warehouseInFk;
this.travel.warehouseOutFk = res.data.warehouseOutFk;
});
}

View File

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