#1241 summary refactor
gitea/salix/dev This commit looks good Details

This commit is contained in:
Gerard 2019-03-25 13:16:34 +01:00
parent 33a375653b
commit 462879a40b
8 changed files with 147 additions and 50 deletions

View File

@ -0,0 +1,101 @@
module.exports = Self => {
Self.remoteMethod('getTickets', {
description: 'Updates the item taxes',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'The item id',
http: {source: 'path'}
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/getTickets`,
verb: 'GET'
}
});
Self.getTickets = async id => {
let filter = {
where: {id: id},
include: [
{relation: 'ticket',
scope: {
fields: ['id', 'packages', 'warehouseFk', 'nickname', 'clientFk', 'priority'],
order: 'priority',
include: [
{
relation: 'client',
scope: {
fields: ['id', 'street', 'postcode'],
}
},
{
relation: 'state',
scope: {
fields: ['id', 'stateFk'],
include: [{relation: 'state'}]
}
},
{
relation: 'warehouse',
scope: {
fields: ['id', 'name']
}
},
{
relation: 'notes',
scope: {
where: {observationTypeFk: 3}
}
}
]
}
}, {
relation: 'agencyMode',
scope: {
fields: ['id', 'name']
}
}, {
relation: 'worker',
scope: {
fields: ['id', 'userFk'],
include: [
{
relation: 'user',
scope: {
fields: ['id', 'nickname']
}
}
]
}
}, {
relation: 'vehicle',
scope: {
fields: ['id', 'm3', 'numberPlate']
}
}
],
};
route = await Self.app.models.Route.findOne(filter);
for (let i = 0; i < route.ticket().length; i++) {
let ticket = route.ticket()[i];
let query = `
SELECT vn.ticketTotalVolume(?) AS m3`;
let options = [ticket.id];
let [volume] = await Self.rawSql(query, options);
ticket.volume = volume.m3;
}
return route.ticket();
};
};

View File

@ -0,0 +1,9 @@
const app = require('vn-loopback/server/server');
describe('route summary()', () => {
it('should return a summary object containing data freom his tickets', async() => {
let result = await app.models.Route.getTickets(1);
expect(result[2].id).toEqual(11);
});
});

View File

@ -0,0 +1,16 @@
const app = require('vn-loopback/server/server');
describe('route summary()', () => {
it('should return a summary object containing data from 1 route', async() => {
let result = await app.models.Route.summary(2);
expect(result.route.id).toEqual(2);
expect(result.route.workerFk).toEqual(56);
});
it('should return a summary object containing data freom his tickets', async() => {
let result = await app.models.Route.summary(1);
expect(result.tickets[2].id).toEqual(11);
});
});

View File

@ -22,44 +22,10 @@ module.exports = Self => {
Self.summary = async id => { Self.summary = async id => {
let summary = {}; let summary = {};
// Item basic data and taxes
let filter = { let filter = {
where: {id: id}, where: {id: id},
include: [ include: [
{relation: 'ticket', {
scope: {
fields: ['id', 'packages', 'warehouseFk', 'nickname', 'clientFk', 'priority'],
order: 'priority',
include: [
{
relation: 'client',
scope: {
fields: ['id', 'street', 'postcode'],
}
},
{
relation: 'state',
scope: {
fields: ['id', 'stateFk'],
include: [{relation: 'state'}]
}
},
{
relation: 'warehouse',
scope: {
fields: ['id', 'name']
}
},
{
relation: 'notes',
scope: {
where: {observationTypeFk: 3}
}
}
]
}
}, {
relation: 'agencyMode', relation: 'agencyMode',
scope: { scope: {
fields: ['id', 'name'] fields: ['id', 'name']
@ -87,17 +53,7 @@ module.exports = Self => {
}; };
summary.route = await Self.app.models.Route.findOne(filter); summary.route = await Self.app.models.Route.findOne(filter);
summary.tickets = await Self.app.models.Route.getTickets(id);
for (let i = 0; i < summary.route.ticket().length; i++) {
let ticket = summary.route.ticket()[i];
let query = `
SELECT vn.ticketTotalVolume(?) AS m3`;
let options = [ticket.id];
let [volume] = await Self.rawSql(query, options);
ticket.volume = volume.m3;
}
return summary; return summary;
}; };

View File

@ -1,3 +1,4 @@
module.exports = Self => { module.exports = Self => {
require('../methods/route/summary')(Self); require('../methods/route/summary')(Self);
require('../methods/route/getTickets')(Self);
}; };

View File

@ -66,8 +66,8 @@
</vn-tr> </vn-tr>
</vn-thead> </vn-thead>
<vn-tbody> <vn-tbody>
<vn-tr ng-repeat="ticket in $ctrl.summary.route.ticket"> <vn-tr ng-repeat="ticket in $ctrl.summary.tickets">
<vn-td shrink>{{ticket.priority}}</vn-td> <vn-td shrink>{{ticket.priority | dashIfEmpty}}</vn-td>
<vn-td number> <vn-td number>
<span <span
ng-click="$ctrl.showTicketDescriptor($event, ticket.id)" ng-click="$ctrl.showTicketDescriptor($event, ticket.id)"

View File

@ -15,7 +15,7 @@ class Controller {
sumPackages() { sumPackages() {
this.packagesTotal = 0; this.packagesTotal = 0;
this.summary.route.ticket.forEach(ticket => { this.summary.tickets.forEach(ticket => {
this.packagesTotal += ticket.packages; this.packagesTotal += ticket.packages;
}); });
} }
@ -41,7 +41,7 @@ class Controller {
getSummary() { getSummary() {
this.$http.get(`/api/Routes/${this.route.id}/summary`).then(response => { this.$http.get(`/api/Routes/${this.route.id}/summary`).then(response => {
this.summary = response.data; this.summary = response.data;
if (response.data && response.data.route && response.data.route.ticket) if (response.data && response.data.tickets)
this.sumPackages(); this.sumPackages();
}); });
} }

View File

@ -23,5 +23,19 @@ describe('Route', () => {
expect(controller.summary).toEqual(24); expect(controller.summary).toEqual(24);
}); });
}); });
describe('sumPackages()', () => {
it('should calculate the packages total', () => {
controller.summary = {
tickets: [
{packages: 3},
{packages: 1}
]
};
controller.sumPackages();
expect(controller.packagesTotal).toEqual(4);
});
});
}); });
}); });