Merge pull request '#2934 - HOTFIX: Route suggested tickets' (#635) from 2934-suggested_tickets into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #635
Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
Carlos Jimenez Ruiz 2021-06-02 09:28:12 +00:00
commit 611907f4dd
5 changed files with 93 additions and 48 deletions

View File

@ -19,14 +19,17 @@ module.exports = Self => {
}
});
Self.getSuggestedTickets = async id => {
const route = await Self.app.models.Route.findById(id);
Self.getSuggestedTickets = async(id, options) => {
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const route = await Self.app.models.Route.findById(id, null, myOptions);
const zoneAgencyModes = await Self.app.models.ZoneAgencyMode.find({
where: {
agencyModeFk: route.agencyModeFk
}
});
}, myOptions);
const zoneIds = [];
for (let zoneAgencyMode of zoneAgencyModes)
@ -38,10 +41,9 @@ module.exports = Self => {
maxDate.setHours(23, 59, 59, 59);
let tickets = await Self.app.models.Ticket.find({
where: {
agencyModeFk: route.agencyModeFk,
zoneFk: {inq: zoneIds},
routeFk: null,
shipped: {between: [minDate, maxDate]}
landed: {between: [minDate, maxDate]}
},
include: [
{
@ -57,7 +59,7 @@ module.exports = Self => {
}
},
]
});
}, myOptions);
return tickets;
};

View File

@ -26,26 +26,46 @@ module.exports = Self => {
}
});
Self.insertTicket = async(routeId, ticketId) => {
Self.insertTicket = async(routeId, ticketId, options) => {
const models = Self.app.models;
const route = await models.Route.findById(routeId);
const minDate = new Date(route.finished);
minDate.setHours(0, 0, 0, 0);
let tx;
let myOptions = {};
const maxDate = new Date(route.finished);
maxDate.setHours(23, 59, 59, 59);
const ticket = await models.Ticket.findOne({
where: {
id: ticketId,
routeFk: null,
landed: {between: [minDate, maxDate]},
}
});
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!ticket)
throw new UserError('The selected ticket is not suitable for this route');
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
return ticket.updateAttribute('routeFk', route.id);
try {
const route = await models.Route.findById(routeId, null, myOptions);
const minDate = new Date(route.created);
minDate.setHours(0, 0, 0, 0);
const maxDate = new Date(route.created);
maxDate.setHours(23, 59, 59, 59);
const ticket = await models.Ticket.findOne({
where: {
id: ticketId,
routeFk: null,
landed: {between: [minDate, maxDate]},
}
}, myOptions);
if (!ticket)
throw new UserError('The selected ticket is not suitable for this route');
const result = await ticket.updateAttribute('routeFk', route.id, myOptions);
if (tx) await tx.commit();
return result;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -2,6 +2,8 @@ const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('route getSuggestedTickets()', () => {
const routeID = 1;
const ticketId = 12;
it('should return an array of suggested tickets', async() => {
const activeCtx = {
accessToken: {userId: 19},
@ -11,20 +13,30 @@ describe('route getSuggestedTickets()', () => {
active: activeCtx
});
const routeID = 1;
const ticketInRoute = await app.models.Ticket.findById(12);
const tx = await app.models.Ticket.beginTransaction({});
await ticketInRoute.updateAttribute('routeFk', null);
try {
const options = {transaction: tx};
const ticketInRoute = await app.models.Ticket.findById(ticketId, null, options);
const result = await app.models.Route.getSuggestedTickets(routeID);
await ticketInRoute.updateAttributes({
routeFk: null,
landed: new Date()
}, options);
const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
const result = await app.models.Route.getSuggestedTickets(routeID, options);
expect(result.length).toEqual(1);
expect(anyResult.zoneFk).toEqual(1);
expect(anyResult.agencyModeFk).toEqual(1);
const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
await ticketInRoute.updateAttribute('routeFk', routeID);
expect(result.length).toEqual(1);
expect(anyResult.zoneFk).toEqual(1);
expect(anyResult.agencyModeFk).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -3,7 +3,6 @@ const LoopBackContext = require('loopback-context');
describe('route insertTicket()', () => {
const deliveryId = 56;
let originalTicket;
const routeId = 1;
const activeCtx = {
accessToken: {userId: deliveryId},
@ -19,12 +18,24 @@ describe('route insertTicket()', () => {
it('should add the ticket to a route', async() => {
const ticketId = 12;
originalTicket = await app.models.Ticket.findById(ticketId);
await originalTicket.updateAttribute('routeFk', null);
const tx = await app.models.Ticket.beginTransaction({});
const result = await app.models.Route.insertTicket(routeId, ticketId);
try {
const options = {transaction: tx};
const ticketInRoute = await app.models.Ticket.findById(ticketId, null, options);
await ticketInRoute.updateAttributes({
routeFk: null,
landed: new Date()
}, options);
expect(result.routeFk).toEqual(routeId);
const result = await app.models.Route.insertTicket(routeId, ticketId, options);
expect(result.routeFk).toEqual(routeId);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should throw and error if the ticket is not suitable for the route', async() => {

View File

@ -63,14 +63,14 @@
display-controls=true>
</vn-input-number>
</vn-td>
<vn-td expand title="{{ticket.street}}">{{::ticket.street}}</vn-td>
<vn-td expand title="{{::ticket.street}}">{{::ticket.street}}</vn-td>
<vn-td expand>{{::ticket.city}}</vn-td>
<vn-td shrink>{{::ticket.postalCode}}</vn-td>
<vn-td expand>
<span
ng-click="clientDescriptor.show($event, ticket.clientFk)"
class="link">
{{ticket.nickname}}
{{::ticket.nickname}}
</span>
</vn-td>
<vn-td shrink>{{::ticket.packages}}</vn-td>
@ -84,8 +84,8 @@
</vn-td>
<vn-td shrink>
<vn-icon
ng-if="ticket.notes.length"
title="{{ticket.notes[0].description}}"
ng-if="::ticket.notes.length"
title="{{::ticket.notes[0].description}}"
icon="insert_drive_file"
class="bright">
</vn-icon>
@ -149,18 +149,18 @@
ng-model="ticket.checked">
</vn-check>
</vn-td>
<vn-td number>{{ticket.id}}</vn-td>
<vn-td number>{{::ticket.id}}</vn-td>
<vn-td number>
<span
ng-click="$ctrl.showClientDescriptor($event, ticket.clientFk)"
ng-click="::$ctrl.showClientDescriptor($event, ticket.clientFk)"
class="link">
{{ticket.nickname}}
{{::ticket.nickname}}
</span>
</vn-td>
<vn-td number shrink>{{ticket.packages}}</vn-td>
<vn-td expand>{{ticket.warehouse.name}}</vn-td>
<vn-td number shrink>{{ticket.address.postalCode}}</vn-td>
<vn-td expand title="{{ticket.address.street}}">{{ticket.address.street}}</vn-td>
<vn-td number shrink>{{::ticket.packages}}</vn-td>
<vn-td expand>{{::ticket.warehouse.name}}</vn-td>
<vn-td number shrink>{{::ticket.address.postalCode}}</vn-td>
<vn-td expand title="{{::ticket.address.street}}">{{::ticket.address.street}}</vn-td>
</vn-tr>
</vn-tbody>
</vn-table>