7152-devToTest_2414 #2228
|
@ -29,7 +29,7 @@ module.exports = Self => {
|
|||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const ticket = await models.TicketLog.findOne({
|
||||
const ticketLog = await models.TicketLog.findOne({
|
||||
fields: ['originFk', 'creationDate', 'newInstance'],
|
||||
where: {
|
||||
originFk: id,
|
||||
|
@ -38,7 +38,7 @@ module.exports = Self => {
|
|||
order: 'creationDate DESC'
|
||||
}, myOptions);
|
||||
|
||||
const ticketOG = await models.Ticket.findById(id, {
|
||||
const ticket = await models.Ticket.findById(id, {
|
||||
include: [{
|
||||
relation: 'client',
|
||||
scope: {
|
||||
|
@ -48,14 +48,14 @@ module.exports = Self => {
|
|||
}, myOptions);
|
||||
|
||||
const now = Date.vnNew();
|
||||
const maxDate = new Date(ticket.creationDate);
|
||||
const maxDate = new Date(ticketLog.creationDate);
|
||||
maxDate.setHours(maxDate.getHours() + 1);
|
||||
|
||||
if (now > maxDate)
|
||||
|
||||
throw new UserError(`You can only restore a ticket within the first hour after deletion`);
|
||||
|
||||
// Send notification to salesPerson
|
||||
const salesPersonId = ticketOG.client().salesPersonFk;
|
||||
const salesPersonId = ticket.client().salesPersonFk;
|
||||
if (salesPersonId) {
|
||||
const url = await Self.app.models.Url.getUrl();
|
||||
const message = $t(`I have restored the ticket id`, {
|
||||
|
@ -66,12 +66,12 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
const fullYear = Date.vnNew().getFullYear();
|
||||
const newShipped = ticketOG.shipped;
|
||||
const newLanded = ticketOG.landed;
|
||||
const newShipped = ticket.shipped;
|
||||
const newLanded = ticket.landed;
|
||||
newShipped.setFullYear(fullYear);
|
||||
newLanded.setFullYear(fullYear);
|
||||
|
||||
return ticketOG.updateAttributes({
|
||||
return ticket.updateAttributes({
|
||||
shipped: newShipped,
|
||||
landed: newLanded,
|
||||
isDeleted: false
|
||||
|
|
|
@ -2,7 +2,7 @@ const app = require('vn-loopback/server/server');
|
|||
const LoopBackContext = require('loopback-context');
|
||||
const models = app.models;
|
||||
|
||||
fdescribe('ticket restore()', () => {
|
||||
describe('ticket restore()', () => {
|
||||
const employeeUser = 1110;
|
||||
const ticketId = 9;
|
||||
const activeCtx = {
|
||||
|
@ -48,31 +48,36 @@ fdescribe('ticket restore()', () => {
|
|||
const tx = await app.models.Ticket.beginTransaction({});
|
||||
const now = Date.vnNew();
|
||||
|
||||
console.log('now', now);
|
||||
try {
|
||||
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({
|
||||
creationDate: '2001-01-01T11:00:00.000Z',
|
||||
isDeleted: true,
|
||||
updated: now
|
||||
}, options);
|
||||
|
||||
console.log('ticketBeforeUpdate', ticketBeforeUpdate);
|
||||
const ticketAfterUpdate = await models.TicketLog.findById(ticketId, null, options);
|
||||
console.log('ticketAfterUpdate: ', ticketAfterUpdate);
|
||||
await models.TicketLog.create({
|
||||
originFk: ticketId,
|
||||
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();
|
||||
|
||||
await models.Ticket.restore(ctx, ticketId, options);
|
||||
const ticketAfterRestore = await models.Ticket.findById(ticketId, null, options);
|
||||
console.log('ticketAfterRestore: ', ticketAfterRestore);
|
||||
|
||||
const fullYear = now.getFullYear();
|
||||
console.log('fullYear: ', fullYear);
|
||||
|
||||
const shippedFullYear = ticketAfterRestore.shipped.getFullYear();
|
||||
console.log('shippedFullYear: ', shippedFullYear);
|
||||
|
||||
const landedFullYear = ticketAfterRestore.landed.getFullYear();
|
||||
console.log('landedFullYear: ', landedFullYear);
|
||||
|
||||
expect(ticketAfterRestore.isDeleted).toBeFalsy();
|
||||
expect(shippedFullYear).toEqual(fullYear);
|
||||
|
|
Loading…
Reference in New Issue