2905 - Restore ticket shipped and landed

This commit is contained in:
Joan Sanchez 2021-05-13 08:56:26 +02:00
parent fc00e759c2
commit e6e91ced99
3 changed files with 59 additions and 41 deletions

View File

@ -36803,10 +36803,6 @@ BEGIN
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 */ ;

View File

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

View File

@ -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};
const ticketBeforeUpdate = await models.Ticket.findById(ticketId, null, options);
await ticketBeforeUpdate.updateAttributes({
isDeleted: true,
updated: now
}, options);
const ticketAfterUpdate = await models.Ticket.findById(ticketId, null, options);
expect(ticketAfterUpdate.isDeleted).toBeTruthy();
await models.Ticket.restore(ctx, createdTicket.id);
const ticketAfterRestore = await models.Ticket.findById(createdTicket.id);
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;
}
});
});