Merge pull request 'refactor delete zone' (#323) from 2335-zone_descriptor into dev
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
Reviewed-by: Carlos Jimenez <carlosjr@verdnatura.es>
This commit is contained in:
commit
cd5ec02e42
|
@ -1,5 +1,5 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('deleteZone', {
|
Self.remoteMethodCtx('deleteZone', {
|
||||||
description: 'Delete a zone',
|
description: 'Delete a zone',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: {
|
accepts: {
|
||||||
|
@ -18,18 +18,44 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.deleteZone = async id => {
|
Self.deleteZone = async(ctx, id) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
|
const token = ctx.req.accessToken;
|
||||||
|
const userId = token.userId;
|
||||||
|
const today = new Date();
|
||||||
|
today.setHours(0, 0, 0, 0);
|
||||||
const tx = await Self.beginTransaction({});
|
const tx = await Self.beginTransaction({});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const options = {transaction: tx};
|
const options = {transaction: tx};
|
||||||
const filter = {where: {zoneFk: id}};
|
const filter = {
|
||||||
|
where: {
|
||||||
|
zoneFk: id
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
relation: 'state',
|
||||||
|
scope: {
|
||||||
|
fields: ['id', 'alertLevel', 'code']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
|
||||||
const ticketList = await models.Ticket.find(filter, options);
|
const ticketList = await models.Ticket.find(filter, options);
|
||||||
|
const fixingState = await models.State.findOne({where: {code: 'FIXING'}}, options);
|
||||||
|
const worker = await models.Worker.findOne({
|
||||||
|
where: {userFk: userId}
|
||||||
|
}, options);
|
||||||
|
|
||||||
ticketList.forEach(ticket => {
|
ticketList.forEach(ticket => {
|
||||||
promises.push(ticket.updateAttributes({zoneFk: null}, options));
|
promises.push(ticket.updateAttributes({zoneFk: null}, options));
|
||||||
|
|
||||||
|
if (ticket.state().alertLevel == 0 && ticket.shipped >= today) {
|
||||||
|
promises.push(models.TicketTracking.create({
|
||||||
|
ticketFk: ticket.id,
|
||||||
|
stateFk: fixingState.id,
|
||||||
|
workerFk: worker.id
|
||||||
|
}, options));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
await models.Zone.destroyById(id, options);
|
await models.Zone.destroyById(id, options);
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
// 2302
|
|
||||||
describe('zone deletezone()', () => {
|
describe('zone deletezone()', () => {
|
||||||
let zoneId = 9;
|
let zoneId = 9;
|
||||||
let originalZoneTickets;
|
let originalZoneTickets;
|
||||||
let originalZone;
|
let originalZone;
|
||||||
let originalZoneIncluded;
|
let originalZoneIncluded;
|
||||||
|
let ticketsId;
|
||||||
|
let originalTicketsState;
|
||||||
|
|
||||||
beforeAll(async done => {
|
beforeAll(async done => {
|
||||||
originalZone = await app.models.Zone.findById(zoneId);
|
originalZone = await app.models.Zone.findById(zoneId);
|
||||||
originalZoneTickets = await app.models.Ticket.find({where: {zoneFk: zoneId}});
|
originalZoneTickets = await app.models.Ticket.find({where: {zoneFk: zoneId}});
|
||||||
originalZoneIncluded = await app.models.ZoneIncluded.find({where: {zoneFk: zoneId}});
|
originalZoneIncluded = await app.models.ZoneIncluded.find({where: {zoneFk: zoneId}});
|
||||||
|
ticketsId = originalZoneTickets.map(originalZoneTickets => originalZoneTickets.id);
|
||||||
|
originalTicketsState = await app.models.TicketState.find({where: {
|
||||||
|
ticketFk: {inq: ticketsId},
|
||||||
|
code: 'FIXING'}});
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -27,13 +32,20 @@ describe('zone deletezone()', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should delete a zone and update their tickets', async() => {
|
it('should delete a zone and update their tickets', async() => {
|
||||||
await app.models.Zone.deleteZone(zoneId);
|
const ctx = {req: {accessToken: {userId: 9}}};
|
||||||
|
await app.models.Zone.deleteZone(ctx, zoneId);
|
||||||
|
|
||||||
let updatedZone = await app.models.Zone.findById(zoneId);
|
let updatedZone = await app.models.Zone.findById(zoneId);
|
||||||
let zoneUpdatedTicket = await app.models.Ticket.findById(originalZoneTickets[0].id);
|
let zoneUpdatedTicket = await app.models.Ticket.findById(originalZoneTickets[0].id);
|
||||||
|
let ticketsId = originalZoneTickets.map(originalZoneTickets => originalZoneTickets.id);
|
||||||
|
|
||||||
|
let updatedTicketState = await app.models.TicketState.find({where: {
|
||||||
|
ticketFk: {inq: ticketsId},
|
||||||
|
code: 'FIXING'}});
|
||||||
|
|
||||||
expect(updatedZone).toBeNull();
|
expect(updatedZone).toBeNull();
|
||||||
expect(zoneUpdatedTicket.zoneFk).not.toBe(zoneId);
|
expect(zoneUpdatedTicket.zoneFk).not.toBe(zoneId);
|
||||||
|
expect(originalTicketsState.length).not.toBeGreaterThan(updatedTicketState.length);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue