diff --git a/modules/zone/back/methods/zone/deleteZone.js b/modules/zone/back/methods/zone/deleteZone.js index b371972bf..d5dd42d7b 100644 --- a/modules/zone/back/methods/zone/deleteZone.js +++ b/modules/zone/back/methods/zone/deleteZone.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethod('deleteZone', { + Self.remoteMethodCtx('deleteZone', { description: 'Delete a zone', accessType: 'WRITE', accepts: { @@ -18,18 +18,44 @@ module.exports = Self => { } }); - Self.deleteZone = async id => { + Self.deleteZone = async(ctx, id) => { 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({}); - try { 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 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 => { 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 models.Zone.destroyById(id, options); diff --git a/modules/zone/back/methods/zone/specs/deleteZone.spec.js b/modules/zone/back/methods/zone/specs/deleteZone.spec.js index cb19a8e25..d8fa3efc1 100644 --- a/modules/zone/back/methods/zone/specs/deleteZone.spec.js +++ b/modules/zone/back/methods/zone/specs/deleteZone.spec.js @@ -1,16 +1,21 @@ const app = require('vn-loopback/server/server'); -// 2302 describe('zone deletezone()', () => { let zoneId = 9; let originalZoneTickets; let originalZone; let originalZoneIncluded; + let ticketsId; + let originalTicketsState; beforeAll(async done => { originalZone = await app.models.Zone.findById(zoneId); originalZoneTickets = await app.models.Ticket.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(); }); @@ -27,13 +32,20 @@ describe('zone deletezone()', () => { }); 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 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(zoneUpdatedTicket.zoneFk).not.toBe(zoneId); + expect(originalTicketsState.length).not.toBeGreaterThan(updatedTicketState.length); }); });