98 lines
3.1 KiB
JavaScript
98 lines
3.1 KiB
JavaScript
const app = require('vn-loopback/server/server');
|
|
|
|
describe('sale updateDiscount()', () => {
|
|
const originalSaleId = 8;
|
|
let componentId;
|
|
let originalSale;
|
|
let salesPersonMana;
|
|
|
|
beforeAll(async done => {
|
|
originalSale = await app.models.Sale.findById(originalSaleId);
|
|
let manaDiscount = await app.models.Component.findOne({where: {code: 'mana'}});
|
|
componentId = manaDiscount.id;
|
|
|
|
let ticket = await app.models.Ticket.findById(originalSale.ticketFk);
|
|
let client = await app.models.Client.findById(ticket.clientFk);
|
|
salesPersonMana = await app.models.WorkerMana.findById(client.salesPersonFk);
|
|
|
|
done();
|
|
});
|
|
|
|
afterAll(async done => {
|
|
await originalSale.save();
|
|
await app.models.SaleComponent.updateAll({componentFk: componentId, saleFk: originalSaleId}, {value: 0});
|
|
await salesPersonMana.save();
|
|
|
|
done();
|
|
});
|
|
|
|
|
|
it('should throw an error if no sales were selected', async() => {
|
|
let ctx = {req: {accessToken: {userId: 9}}};
|
|
let error;
|
|
const ticketId = 11;
|
|
const sales = [];
|
|
const newDiscount = 10;
|
|
|
|
try {
|
|
await app.models.Ticket.updateDiscount(ctx, ticketId, sales, newDiscount);
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(error.message).toEqual('Please select at least one sale');
|
|
});
|
|
|
|
it('should throw an error if no sales belong to different tickets', async() => {
|
|
let ctx = {req: {accessToken: {userId: 9}}};
|
|
let error;
|
|
const ticketId = 11;
|
|
const sales = [1, 14];
|
|
const newDiscount = 10;
|
|
|
|
try {
|
|
await app.models.Ticket.updateDiscount(ctx, ticketId, sales, newDiscount);
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(error.message).toEqual('All sales must belong to the same ticket');
|
|
});
|
|
|
|
it('should throw an error if the ticket is invoiced already', async() => {
|
|
let ctx = {req: {accessToken: {userId: 9}}};
|
|
let error;
|
|
const ticketId = 1;
|
|
const sales = [1];
|
|
const newDiscount = 100;
|
|
|
|
try {
|
|
await app.models.Ticket.updateDiscount(ctx, ticketId, sales, newDiscount);
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(error.message).toEqual(`The sales of this ticket can't be modified`);
|
|
});
|
|
|
|
it('should update the discount if the salesPerson has mana', async() => {
|
|
let ctx = {req: {accessToken: {userId: 9}}};
|
|
const ticketId = 11;
|
|
const sales = [originalSaleId];
|
|
const newDiscount = 100;
|
|
|
|
await app.models.Ticket.updateDiscount(ctx, ticketId, sales, newDiscount);
|
|
|
|
let updatedSale = await app.models.Sale.findById(originalSaleId);
|
|
let createdSaleComponent = await app.models.SaleComponent.findOne({
|
|
where: {
|
|
componentFk: componentId,
|
|
saleFk: originalSaleId
|
|
}
|
|
});
|
|
|
|
expect(createdSaleComponent.componentFk).toEqual(componentId);
|
|
expect(updatedSale.discount).toEqual(100);
|
|
});
|
|
});
|