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 ', `body` = CONCAT(account.myUserGetName(), ' ha modificado el ticket ',
NEW.id); NEW.id);
END IF; 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 */;; END */;;
DELIMITER ; DELIMITER ;
/*!50003 SET sql_mode = @saved_sql_mode */ ; /*!50003 SET sql_mode = @saved_sql_mode */ ;

View File

@ -3,7 +3,7 @@ const app = require('vn-loopback/server/server');
describe('SalesMonitor ordersFilter()', () => { describe('SalesMonitor ordersFilter()', () => {
it('should return the orders activity', async() => { it('should return the orders activity', async() => {
const ctx = {req: {accessToken: {userId: 18}}, args: {}}; 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); const result = await app.models.SalesMonitor.ordersFilter(ctx, filter);
expect(result.length).toEqual(12); 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 models = Self.app.models;
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const ticket = await models.Ticket.findById(id, { const ticket = await models.Ticket.findById(id, {
include: [{ include: [{
relation: 'client', relation: 'client',
@ -31,7 +36,7 @@ module.exports = Self => {
fields: ['id', 'salesPersonFk'] fields: ['id', 'salesPersonFk']
} }
}] }]
}); }, myOptions);
const now = new Date(); const now = new Date();
const maxDate = new Date(ticket.updated); const maxDate = new Date(ticket.updated);
@ -51,6 +56,16 @@ module.exports = Self => {
await models.Chat.sendCheckingPresence(ctx, salesPersonId, message); 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()', () => { describe('ticket restore()', () => {
const employeeUser = 110; const employeeUser = 110;
const ticketId = 18;
const activeCtx = { const activeCtx = {
accessToken: {userId: employeeUser}, accessToken: {userId: employeeUser},
headers: { headers: {
@ -13,45 +14,30 @@ describe('ticket restore()', () => {
}; };
const ctx = {req: activeCtx}; const ctx = {req: activeCtx};
let createdTicket; beforeEach(() => {
beforeEach(async done => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx 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() => { it('should throw an error if the given ticket has past the deletion time', async() => {
let error; let error;
const tx = await app.models.Ticket.beginTransaction({});
const now = new Date(); const now = new Date();
now.setHours(now.getHours() - 1); now.setHours(now.getHours() - 1);
try { try {
const ticket = await models.Ticket.findById(createdTicket.id); const options = {transaction: tx};
await ticket.updateAttributes({isDeleted: true, updated: now}); const ticket = await models.Ticket.findById(ticketId, null, options);
await app.models.Ticket.restore(ctx, createdTicket.id); await ticket.updateAttributes({
isDeleted: true,
updated: now
}, options);
await app.models.Ticket.restore(ctx, ticketId, options);
await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback();
error = e; error = e;
} }
@ -59,17 +45,37 @@ describe('ticket restore()', () => {
}); });
it('should restore the ticket making its state no longer deleted', async() => { it('should restore the ticket making its state no longer deleted', async() => {
const tx = await app.models.Ticket.beginTransaction({});
const now = new Date(); 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 ticketAfterUpdate = await models.Ticket.findById(ticketId, null, options);
const ticketAfterRestore = await models.Ticket.findById(createdTicket.id);
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;
}
}); });
}); });