tickets already in route are no longer shown + tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-03-22 16:13:12 +01:00
parent 0023a88bfc
commit 84378fd607
4 changed files with 72 additions and 58 deletions

View File

@ -20,6 +20,12 @@ module.exports = Self => {
}); });
Self.getSuggestedTickets = async id => { Self.getSuggestedTickets = async id => {
const ticketsInRoute = await Self.app.models.Ticket.find({
where: {routeFk: id},
fields: ['id']
});
const idsToExclude = ticketsInRoute.map(ticket => ticket.id);
const route = await Self.app.models.Route.findById(id); const route = await Self.app.models.Route.findById(id);
const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({ const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({
@ -32,11 +38,26 @@ module.exports = Self => {
for (let zoneAgencyMode of zoneAgencyModes) for (let zoneAgencyMode of zoneAgencyModes)
zoneIds.push(zoneAgencyMode.zoneFk); zoneIds.push(zoneAgencyMode.zoneFk);
const tickets = await Self.app.models.Ticket.find({ let tickets = await Self.app.models.Ticket.find({
where: { where: {
ageconyModeFk: route.agencyModeFk, agencyModeFk: route.agencyModeFk,
zoneFk: {inq: zoneIds} zoneFk: {inq: zoneIds},
} id: {nin: idsToExclude}
},
include: [
{
relation: 'warehouse',
scope: {
fields: ['id', 'name']
}
},
{
relation: 'address',
scope: {
fields: ['id', 'street', 'postalCode', 'city'],
}
},
]
}); });
return tickets; return tickets;

View File

@ -1,14 +1,30 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('route getSuggestedTickets()', () => { describe('route getSuggestedTickets()', () => {
it('should return an array of suggested tickets', async() => { it('should return an array of suggested tickets', async() => {
const result = await app.models.Route.getSuggestedTickets(1); const activeCtx = {
accessToken: {userId: 19},
headers: {origin: 'http://localhost'}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
const routeID = 1;
const ticketInRoute = await app.models.Ticket.findOne({where: {routeFk: routeID}});
await ticketInRoute.updateAttribute('routeFk', null);
const result = await app.models.Route.getSuggestedTickets(routeID);
const length = result.length; const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
expect(result.length).toEqual(4); expect(result.length).toEqual(1);
expect(anyResult.zoneFk).toEqual(1); expect(anyResult.zoneFk).toEqual(1);
expect(anyResult.agencyModeFk).toEqual(1); expect(anyResult.agencyModeFk).toEqual(1);
await ticketInRoute.updateAttribute('routeFk', routeID);
}); });
}); });

View File

@ -106,14 +106,26 @@ class Controller extends Section {
setTicketsRoute() { setTicketsRoute() {
let tickets = this.getSelectedItems(this.possibleTickets); let tickets = this.getSelectedItems(this.possibleTickets);
if (tickets.length === 0) return; if (tickets.length === 0) return;
for (let i = 0; i < tickets.length; i++) {
delete tickets[i].checked; const updates = [];
tickets[i].routeFk = this.route.id;
for (let ticket of tickets) {
delete ticket.checked;
const update = {
where: {id: ticket.id},
data: {routeFk: this.route.id}
};
updates.push(update);
} }
return this.$.possibleTicketsModel.save().then(() => { const data = {creates: [], updates: updates, deletes: []};
this.$.model.data = this.$.model.data.concat(tickets);
}); return this.$http.post(`Tickets/crud`, data)
.then(() => {
this.$.model.data = this.$.model.data.concat(tickets);
this.vnApp.showSuccess(this.$t('Data saved!'));
});
} }
onDrop($event) { onDrop($event) {

View File

@ -37,42 +37,6 @@ describe('Route', () => {
}); });
}); });
describe('buildPossibleTicketsFilter()', () => {
it('should build the possible tickets filter', () => {
let expectedFilter = {
include: [
{
relation: 'warehouse',
scope: {
fields: ['name']
}
}, {
relation: 'address'
}
],
where: {
landed: {
between: [
jasmine.any(Date),
jasmine.any(Date)
]
},
routeFk: null,
zoneFk: 67
}
};
controller.route = {
finished: new Date(),
routeFk: null,
zoneFk: 67
};
controller.buildPossibleTicketsFilter();
expect(controller.possibleTicketsFilter).toEqual(expectedFilter);
});
});
describe('getHighestPriority()', () => { describe('getHighestPriority()', () => {
it('should return the highest value found in priorities plus 1', () => { it('should return the highest value found in priorities plus 1', () => {
controller.$.model = {data: [ controller.$.model = {data: [
@ -228,13 +192,13 @@ describe('Route', () => {
}); });
describe('setTicketsRoute()', () => { describe('setTicketsRoute()', () => {
it('should perform a POST query to add tickets to the route', done => { it('should perform a POST query to add tickets to the route', () => {
controller.$.possibleTicketsModel = {save: () => {}};
jest.spyOn(controller.$.possibleTicketsModel, 'save').mockReturnValue(Promise.resolve());
controller.$.model = {data: [ controller.$.model = {data: [
{id: 1, checked: false} {id: 1, checked: false}
]}; ]};
const existingTicket = controller.$.model.data[0];
controller.route = {id: 111}; controller.route = {id: 111};
controller.possibleTickets = [ controller.possibleTickets = [
@ -245,15 +209,16 @@ describe('Route', () => {
]; ];
let expectedResult = [ let expectedResult = [
{checked: false, id: 1}, existingTicket,
{id: 3, routeFk: 111}, {id: 3},
{id: 5, routeFk: 111} {id: 5}
]; ];
controller.setTicketsRoute().then(() => { $httpBackend.expectPOST(`Tickets/crud`).respond();
expect(controller.$.model.data).toEqual(expectedResult); controller.setTicketsRoute();
done(); $httpBackend.flush();
}).catch(done.fail);
expect(controller.$.model.data).toEqual(expectedResult);
}); });
}); });