refactor delete zone
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Bernat Exposito Domenech 2020-06-23 07:38:36 +02:00
parent 356c1a7b45
commit e96df49622
2 changed files with 44 additions and 6 deletions

View File

@ -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);

View File

@ -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);
}); });
}); });