7152-devToTest_2414 #2228

Merged
alexm merged 636 commits from 7152-devToTest_2414 into test 2024-03-28 08:26:34 +00:00
2 changed files with 24 additions and 19 deletions
Showing only changes of commit ed57212618 - Show all commits

View File

@ -29,7 +29,7 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const ticket = await models.TicketLog.findOne({ const ticketLog = await models.TicketLog.findOne({
fields: ['originFk', 'creationDate', 'newInstance'], fields: ['originFk', 'creationDate', 'newInstance'],
where: { where: {
originFk: id, originFk: id,
@ -38,7 +38,7 @@ module.exports = Self => {
order: 'creationDate DESC' order: 'creationDate DESC'
}, myOptions); }, myOptions);
const ticketOG = await models.Ticket.findById(id, { const ticket = await models.Ticket.findById(id, {
include: [{ include: [{
relation: 'client', relation: 'client',
scope: { scope: {
@ -48,14 +48,14 @@ module.exports = Self => {
}, myOptions); }, myOptions);
const now = Date.vnNew(); const now = Date.vnNew();
const maxDate = new Date(ticket.creationDate); const maxDate = new Date(ticketLog.creationDate);
maxDate.setHours(maxDate.getHours() + 1); maxDate.setHours(maxDate.getHours() + 1);
if (now > maxDate) if (now > maxDate)
throw new UserError(`You can only restore a ticket within the first hour after deletion`); throw new UserError(`You can only restore a ticket within the first hour after deletion`);
// Send notification to salesPerson // Send notification to salesPerson
const salesPersonId = ticketOG.client().salesPersonFk; const salesPersonId = ticket.client().salesPersonFk;
if (salesPersonId) { if (salesPersonId) {
const url = await Self.app.models.Url.getUrl(); const url = await Self.app.models.Url.getUrl();
const message = $t(`I have restored the ticket id`, { const message = $t(`I have restored the ticket id`, {
@ -66,12 +66,12 @@ module.exports = Self => {
} }
const fullYear = Date.vnNew().getFullYear(); const fullYear = Date.vnNew().getFullYear();
const newShipped = ticketOG.shipped; const newShipped = ticket.shipped;
const newLanded = ticketOG.landed; const newLanded = ticket.landed;
newShipped.setFullYear(fullYear); newShipped.setFullYear(fullYear);
newLanded.setFullYear(fullYear); newLanded.setFullYear(fullYear);
return ticketOG.updateAttributes({ return ticket.updateAttributes({
shipped: newShipped, shipped: newShipped,
landed: newLanded, landed: newLanded,
isDeleted: false isDeleted: false

View File

@ -2,7 +2,7 @@ const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
const models = app.models; const models = app.models;
fdescribe('ticket restore()', () => { describe('ticket restore()', () => {
const employeeUser = 1110; const employeeUser = 1110;
const ticketId = 9; const ticketId = 9;
const activeCtx = { const activeCtx = {
@ -48,31 +48,36 @@ fdescribe('ticket restore()', () => {
const tx = await app.models.Ticket.beginTransaction({}); const tx = await app.models.Ticket.beginTransaction({});
const now = Date.vnNew(); const now = Date.vnNew();
console.log('now', now);
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
const ticketBeforeUpdate = await models.TicketLog.findById(ticketId, null, options); const ticketBeforeUpdate = await models.Ticket.findById(ticketId, null, options);
await ticketBeforeUpdate.updateAttributes({ await ticketBeforeUpdate.updateAttributes({
creationDate: '2001-01-01T11:00:00.000Z', isDeleted: true,
updated: now
}, options); }, options);
console.log('ticketBeforeUpdate', ticketBeforeUpdate); await models.TicketLog.create({
const ticketAfterUpdate = await models.TicketLog.findById(ticketId, null, options); originFk: ticketId,
console.log('ticketAfterUpdate: ', ticketAfterUpdate); userFk: employeeUser,
action: 'update',
changedModel: 'Ticket',
creationDate: new Date('2001-01-01 11:01:00'),
newInstance: '{"isDeleted":true}'
}, options);
const ticketAfterUpdate = await models.Ticket.findById(ticketId, null, options);
expect(ticketAfterUpdate.isDeleted).toBeTruthy(); expect(ticketAfterUpdate.isDeleted).toBeTruthy();
await models.Ticket.restore(ctx, ticketId, options); await models.Ticket.restore(ctx, ticketId, options);
const ticketAfterRestore = await models.Ticket.findById(ticketId, null, options); const ticketAfterRestore = await models.Ticket.findById(ticketId, null, options);
console.log('ticketAfterRestore: ', ticketAfterRestore);
const fullYear = now.getFullYear(); const fullYear = now.getFullYear();
console.log('fullYear: ', fullYear);
const shippedFullYear = ticketAfterRestore.shipped.getFullYear(); const shippedFullYear = ticketAfterRestore.shipped.getFullYear();
console.log('shippedFullYear: ', shippedFullYear);
const landedFullYear = ticketAfterRestore.landed.getFullYear(); const landedFullYear = ticketAfterRestore.landed.getFullYear();
console.log('landedFullYear: ', landedFullYear);
expect(ticketAfterRestore.isDeleted).toBeFalsy(); expect(ticketAfterRestore.isDeleted).toBeFalsy();
expect(shippedFullYear).toEqual(fullYear); expect(shippedFullYear).toEqual(fullYear);