diff --git a/db/dump/structure.sql b/db/dump/structure.sql index 8bc0aed8f..407770aef 100644 --- a/db/dump/structure.sql +++ b/db/dump/structure.sql @@ -36802,11 +36802,7 @@ BEGIN `body` = CONCAT(account.myUserGetName(), ' ha modificado el ticket ', NEW.id); END IF; - - IF !(DATE(NEW.shipped) <=> DATE(OLD.shipped)) AND DATE(NEW.shipped) = CURDATE() THEN - INSERT INTO tmp.ticketDate_updated(ticketFk, oldShipped, newShipped, workerFk) - VALUES (NEW.id, OLD.shipped, NEW.shipped, vn.getUser()); - END IF; + END */;; DELIMITER ; /*!50003 SET sql_mode = @saved_sql_mode */ ; diff --git a/modules/ticket/back/methods/ticket/restore.js b/modules/ticket/back/methods/ticket/restore.js index 19429f61b..6a0d65a17 100644 --- a/modules/ticket/back/methods/ticket/restore.js +++ b/modules/ticket/back/methods/ticket/restore.js @@ -21,9 +21,15 @@ module.exports = Self => { } }); - Self.restore = async(ctx, id) => { + Self.restore = async(ctx, id, options) => { const models = Self.app.models; const $t = ctx.req.__; // $translate + + let myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + const ticket = await models.Ticket.findById(id, { include: [{ relation: 'client', @@ -31,7 +37,7 @@ module.exports = Self => { fields: ['id', 'salesPersonFk'] } }] - }); + }, myOptions); const now = new Date(); const maxDate = new Date(ticket.updated); @@ -51,6 +57,16 @@ module.exports = Self => { await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); } - return ticket.updateAttribute('isDeleted', false); + const fullYear = new Date().getFullYear(); + const newShipped = ticket.shipped; + const newLanded = ticket.landed; + newShipped.setFullYear(fullYear); + newLanded.setFullYear(fullYear); + + return ticket.updateAttributes({ + shipped: newShipped, + landed: newLanded, + isDeleted: false + }, myOptions); }; }; diff --git a/modules/ticket/back/methods/ticket/specs/restore.spec.js b/modules/ticket/back/methods/ticket/specs/restore.spec.js index 97cf78f91..52135fc54 100644 --- a/modules/ticket/back/methods/ticket/specs/restore.spec.js +++ b/modules/ticket/back/methods/ticket/specs/restore.spec.js @@ -4,6 +4,7 @@ const models = app.models; describe('ticket restore()', () => { const employeeUser = 110; + const ticketId = 18; const activeCtx = { accessToken: {userId: employeeUser}, headers: { @@ -13,45 +14,30 @@ describe('ticket restore()', () => { }; const ctx = {req: activeCtx}; - let createdTicket; - - beforeEach(async done => { + beforeEach(() => { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ active: activeCtx }); - try { - const sampleTicket = await models.Ticket.findById(11); - sampleTicket.id = undefined; - - createdTicket = await models.Ticket.create(sampleTicket); - } catch (error) { - console.error(error); - } - - done(); - }); - - afterEach(async done => { - try { - await models.Ticket.destroyById(createdTicket.id); - } catch (error) { - console.error(error); - } - - done(); }); it('should throw an error if the given ticket has past the deletion time', async() => { let error; + const tx = await app.models.Ticket.beginTransaction({}); const now = new Date(); now.setHours(now.getHours() - 1); try { - const ticket = await models.Ticket.findById(createdTicket.id); - await ticket.updateAttributes({isDeleted: true, updated: now}); - await app.models.Ticket.restore(ctx, createdTicket.id); + const options = {transaction: tx}; + const ticket = await models.Ticket.findById(ticketId, null, options); + await ticket.updateAttributes({ + isDeleted: true, + updated: now + }, options); + await app.models.Ticket.restore(ctx, ticketId, options); + await tx.rollback(); } catch (e) { + await tx.rollback(); error = e; } @@ -59,17 +45,37 @@ describe('ticket restore()', () => { }); it('should restore the ticket making its state no longer deleted', async() => { + const tx = await app.models.Ticket.beginTransaction({}); const now = new Date(); - const ticketBeforeUpdate = await models.Ticket.findById(createdTicket.id); - await ticketBeforeUpdate.updateAttributes({isDeleted: true, updated: now}); - const ticketAfterUpdate = await models.Ticket.findById(createdTicket.id); + try { + const options = {transaction: tx}; - expect(ticketAfterUpdate.isDeleted).toBeTruthy(); + const ticketBeforeUpdate = await models.Ticket.findById(ticketId, null, options); + await ticketBeforeUpdate.updateAttributes({ + isDeleted: true, + updated: now + }, options); - await models.Ticket.restore(ctx, createdTicket.id); - const ticketAfterRestore = await models.Ticket.findById(createdTicket.id); + const ticketAfterUpdate = await models.Ticket.findById(ticketId, null, options); - expect(ticketAfterRestore.isDeleted).toBeFalsy(); + expect(ticketAfterUpdate.isDeleted).toBeTruthy(); + + await models.Ticket.restore(ctx, ticketId, options); + const ticketAfterRestore = await models.Ticket.findById(ticketId, null, options); + + const fullYear = now.getFullYear(); + const shippedFullYear = ticketAfterRestore.shipped.getFullYear(); + const landedFullYear = ticketAfterRestore.landed.getFullYear(); + + expect(ticketAfterRestore.isDeleted).toBeFalsy(); + expect(shippedFullYear).toEqual(fullYear); + expect(landedFullYear).toEqual(fullYear); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); });