Added functionality to autocomplete the inputs

This commit is contained in:
Jorge Padawan 2021-02-10 08:47:58 +01:00
parent 1adaeca615
commit 1c6bddbfb6
6 changed files with 101 additions and 45 deletions

View File

@ -0,0 +1,36 @@
module.exports = Self => {
Self.remoteMethod('getTravelData', {
description: 'Returns the days of travel duration',
accessType: 'READ',
accepts: [{
arg: 'agencyModeFk',
type: 'number',
required: true
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/getTravelData`,
verb: 'GET'
}
});
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;`;
const [result] = await Self.rawSql(query, [agencyModeFk, agencyModeFk]);
return result;
};
};

View File

@ -1,44 +0,0 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethod('getTravelDaysDuration', {
description: 'Return the total days of travel',
accessType: 'READ',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'The travel id',
http: {source: 'path'}
},
returns: {
type: 'number',
root: true
},
http: {
path: `/:id/getTravelDaysDuration`,
verb: 'GET'
}
});
Self.getTravelDaysDuration = async id => {
let stmt;
stmt = new ParameterizedSQL(`
SELECT
ROUND(
AVG(
DATEDIFF(landed , shipped)
)
)
FROM travel
WHERE agencyFk = ?
GROUP BY agencyFK`, [
id
]);
let result = await Self.rawStmt(stmt);
return result;
};
};

View File

@ -8,6 +8,7 @@ module.exports = Self => {
require('../methods/travel/deleteThermograph')(Self);
require('../methods/travel/updateThermograph')(Self);
require('../methods/travel/extraCommunityFilter')(Self);
require('../methods/travel/getTravelData')(Self);
require('../methods/travel/cloneWithEntries')(Self);
Self.rewriteDbError(function(err) {

View File

@ -20,7 +20,9 @@
</vn-horizontal>
<vn-horizontal>
<vn-date-picker
on-change="$ctrl.onShippedChange(value)"
label="Shipped"
id="dateShipped"
ng-model="$ctrl.travel.shipped">
</vn-date-picker>
<vn-date-picker

View File

@ -7,6 +7,30 @@ class Controller extends Section {
this.travel = JSON.parse(this.$params.q);
}
onShippedChange(value) {
const hasFilledProperties = this.travel.landed || this.travel.warehouseInFk || this.travel.warehouseOutFk;
if (!this.travel.agencyModeFk || hasFilledProperties)
return;
const query = `travels/getTravelData`;
const params = {
agencyModeFk: this.travel.agencyModeFk
};
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;
});
}
onSubmit() {
return this.$.watcher.submit().then(
res => this.$state.go('travel.card.basicData', {id: res.data.id})

View File

@ -5,10 +5,12 @@ describe('Travel Component vnTravelCreate', () => {
let $scope;
let $state;
let controller;
let $httpBackend;
beforeEach(ngModule('travel'));
beforeEach(inject(($componentController, $rootScope, _$state_) => {
beforeEach(inject(($componentController, $rootScope, _$state_, _$httpBackend_) => {
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
$state = _$state_;
$scope.watcher = watcher;
@ -38,4 +40,39 @@ describe('Travel Component vnTravelCreate', () => {
expect(controller.travel).toEqual(json);
});
});
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};
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);
});
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
};
const params = {agencyModeFk: 4};
$httpBackend.expectGET(`travels/getTravelData`, params).respond({agencyModeFk: 4});
controller.onShippedChange();
// $httpBackend.flush();
expect(controller.travel.agencyModeFk).toEqual(params.agencyModeFk);
});
});
});