265 lines
9.0 KiB
JavaScript
265 lines
9.0 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
|
|
describe('ticket componentUpdate()', () => {
|
|
const ctx = beforeAll.getCtx(1101);
|
|
const ticketID = 11;
|
|
const today = Date.vnNew();
|
|
const tomorrow = Date.vnNew();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
let deliveryComponentId;
|
|
let firstvalueBeforeChange;
|
|
let secondvalueBeforeChange;
|
|
let componentOfSaleSeven;
|
|
let componentOfSaleEight;
|
|
let componentValue;
|
|
|
|
beforeAll.mockLoopBackContext();
|
|
beforeAll(async() => {
|
|
const deliveryComponenet = await models.Component.findOne({where: {code: 'delivery'}});
|
|
deliveryComponentId = deliveryComponenet.id;
|
|
componentOfSaleSeven = `SELECT value
|
|
FROM vn.saleComponent
|
|
WHERE saleFk = 7 AND componentFk = ${deliveryComponentId}`;
|
|
componentOfSaleEight = `SELECT value
|
|
FROM vn.saleComponent
|
|
WHERE saleFk = 8 AND componentFk = ${deliveryComponentId}`;
|
|
[componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven);
|
|
firstvalueBeforeChange = componentValue.value;
|
|
|
|
[componentValue] = await models.SaleComponent.rawSql(componentOfSaleEight);
|
|
secondvalueBeforeChange = componentValue.value;
|
|
});
|
|
|
|
it('should change the agencyMode to modify the sale components value', async() => {
|
|
const tx = await models.SaleComponent.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const args = {
|
|
id: ticketID,
|
|
clientFk: 1102,
|
|
agencyModeFk: 8,
|
|
addressFk: 122,
|
|
zoneFk: 5,
|
|
warehouseFk: 1,
|
|
companyFk: 442,
|
|
shipped: today,
|
|
landed: tomorrow,
|
|
isDeleted: false,
|
|
option: 'renewPrices',
|
|
isWithoutNegatives: false
|
|
};
|
|
|
|
ctx.args = args;
|
|
await models.Ticket.componentUpdate(ctx, options);
|
|
|
|
[componentValue] = await models.SaleComponent.rawSql(componentOfSaleSeven, null, options);
|
|
let firstvalueAfterChange = componentValue.value;
|
|
|
|
[componentValue] = await models.SaleComponent.rawSql(componentOfSaleEight, null, options);
|
|
let secondvalueAfterChange = componentValue.value;
|
|
|
|
expect(firstvalueBeforeChange).not.toEqual(firstvalueAfterChange);
|
|
expect(secondvalueBeforeChange).not.toEqual(secondvalueAfterChange);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should change the addressFk and check that delivery observations have been changed', async() => {
|
|
const tx = await models.SaleComponent.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const args = {
|
|
id: ticketID,
|
|
clientFk: 1102,
|
|
agencyModeFk: 8,
|
|
addressFk: 2,
|
|
zoneFk: 5,
|
|
warehouseFk: 1,
|
|
companyFk: 442,
|
|
shipped: today,
|
|
landed: tomorrow,
|
|
isDeleted: false,
|
|
option: 'renewPrices',
|
|
isWithoutNegatives: false
|
|
};
|
|
|
|
ctx.args = args;
|
|
const observationTypeDelivery = await models.ObservationType.findOne({
|
|
where: {code: 'delivery'}
|
|
}, options);
|
|
const originalTicketObservation = await models.TicketObservation.findOne({
|
|
where: {
|
|
ticketFk: args.id,
|
|
observationTypeFk: observationTypeDelivery.id}
|
|
}, options);
|
|
|
|
expect(originalTicketObservation).toBeDefined();
|
|
|
|
await models.Ticket.componentUpdate(ctx, options);
|
|
|
|
const removedTicketObservation = await models.TicketObservation.findOne({
|
|
where: {
|
|
ticketFk: ticketID,
|
|
observationTypeFk: observationTypeDelivery.id}
|
|
}, options);
|
|
|
|
expect(removedTicketObservation).toBeNull();
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should change warehouse and without negatives', async() => {
|
|
const ctx = beforeAll.getCtx(9);
|
|
const tx = await models.SaleComponent.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const saleToTransfer = 27;
|
|
const originDate = today;
|
|
const newDate = tomorrow;
|
|
const ticketID = 14;
|
|
newDate.setHours(0, 0, 0, 0, 0);
|
|
originDate.setHours(0, 0, 0, 0, 0);
|
|
|
|
const args = {
|
|
id: ticketID,
|
|
clientFk: 1104,
|
|
agencyModeFk: 2,
|
|
addressFk: 4,
|
|
zoneFk: 9,
|
|
warehouseFk: 1,
|
|
companyFk: 442,
|
|
shipped: newDate,
|
|
landed: tomorrow,
|
|
isDeleted: false,
|
|
option: 'renewPrices',
|
|
isWithoutNegatives: true
|
|
};
|
|
ctx.args = args;
|
|
|
|
const oldTicket = await models.Ticket.findById(ticketID, null, options);
|
|
|
|
await models.Ticket.componentUpdate(ctx, options);
|
|
|
|
const [newTicketID] = await models.Ticket.rawSql('SELECT MAX(id) as id FROM ticket', null, options);
|
|
const newTicket = await models.Ticket.findById(newTicketID.id, null, options);
|
|
const newTicketSale = await models.Sale.findOne({where: {ticketFk: args.id}}, options);
|
|
|
|
expect(oldTicket.shipped).toEqual(originDate);
|
|
expect(newTicket.shipped).toEqual(newDate);
|
|
expect(newTicketSale.id).toEqual(saleToTransfer);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
describe('componentUpdate() keepPrice', () => {
|
|
const ctx = beforeAll.getCtx();
|
|
it('should change shipped and keep price', async() => {
|
|
const tx = await models.Ticket.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const saleId = 27;
|
|
const originDate = today;
|
|
const newDate = tomorrow;
|
|
const ticketID = 14;
|
|
newDate.setHours(0, 0, 0, 0, 0);
|
|
originDate.setHours(0, 0, 0, 0, 0);
|
|
|
|
const args = {
|
|
id: ticketID,
|
|
clientFk: 1104,
|
|
agencyModeFk: 2,
|
|
addressFk: 4,
|
|
zoneFk: 9,
|
|
warehouseFk: 1,
|
|
companyFk: 442,
|
|
shipped: newDate,
|
|
landed: tomorrow,
|
|
isDeleted: false,
|
|
option: 'renewPrices',
|
|
isWithoutNegatives: false,
|
|
keepPrice: true
|
|
};
|
|
|
|
ctx.args = args;
|
|
|
|
const beforeSale = await models.Sale.findById(saleId, null, options);
|
|
await models.Ticket.componentUpdate(ctx, options);
|
|
const afterSale = await models.Sale.findById(saleId, null, options);
|
|
|
|
expect(beforeSale.price).toEqual(afterSale.price);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it('should change shipped and not keep price', async() => {
|
|
const tx = await models.Ticket.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
|
|
const saleId = 27;
|
|
const originDate = today;
|
|
const newDate = tomorrow;
|
|
const ticketID = 14;
|
|
newDate.setHours(0, 0, 0, 0, 0);
|
|
originDate.setHours(0, 0, 0, 0, 0);
|
|
|
|
const args = {
|
|
id: ticketID,
|
|
clientFk: 1104,
|
|
agencyModeFk: 2,
|
|
addressFk: 4,
|
|
zoneFk: 9,
|
|
warehouseFk: 1,
|
|
companyFk: 442,
|
|
shipped: newDate,
|
|
landed: tomorrow,
|
|
isDeleted: false,
|
|
option: 'renewPrices',
|
|
isWithoutNegatives: false,
|
|
keepPrice: false
|
|
};
|
|
|
|
ctx.args = args;
|
|
|
|
const beforeSale = await models.Sale.findById(saleId, null, options);
|
|
await models.Ticket.componentUpdate(ctx, options);
|
|
const afterSale = await models.Sale.findById(saleId, null, options);
|
|
|
|
expect(beforeSale.price).not.toEqual(afterSale.price);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|
|
});
|