Merge pull request '2905- Restore ticket with shipped and landed' (#621) from 2905-restore_ticket_date into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #621
Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
Carlos Jimenez Ruiz 2021-05-13 08:29:14 +00:00
commit c8174fd132
4 changed files with 59 additions and 42 deletions

View File

@ -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 */ ;

View File

@ -3,7 +3,7 @@ const app = require('vn-loopback/server/server');
describe('SalesMonitor ordersFilter()', () => {
it('should return the orders activity', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}};
const filter = {order: 'dated DESC'};
const filter = {order: 'date_make DESC'};
const result = await app.models.SalesMonitor.ordersFilter(ctx, filter);
expect(result.length).toEqual(12);

View File

@ -21,9 +21,14 @@ module.exports = Self => {
}
});
Self.restore = async(ctx, id) => {
Self.restore = async(ctx, id, options) => {
const models = Self.app.models;
const $t = ctx.req.__; // $translate
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const ticket = await models.Ticket.findById(id, {
include: [{
relation: 'client',
@ -31,7 +36,7 @@ module.exports = Self => {
fields: ['id', 'salesPersonFk']
}
}]
});
}, myOptions);
const now = new Date();
const maxDate = new Date(ticket.updated);
@ -51,6 +56,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};
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;
}
});
});