add sale and canbeInvoice refactor + tests

This commit is contained in:
Carlos Jimenez Ruiz 2021-08-05 13:32:35 +02:00
parent 4628c8ab6f
commit 870314277f
3 changed files with 138 additions and 100 deletions

View File

@ -32,15 +32,26 @@ module.exports = Self => {
} }
}); });
Self.addSale = async(ctx, id, itemId, quantity) => { Self.addSale = async(ctx, id, itemId, quantity, options) => {
const $t = ctx.req.__; // $translate const $t = ctx.req.__; // $translate
const models = Self.app.models; const models = Self.app.models;
let tx;
const myOptions = {};
const isEditable = await models.Ticket.isEditable(ctx, id); if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const isEditable = await models.Ticket.isEditable(ctx, id, myOptions);
if (!isEditable) if (!isEditable)
throw new UserError(`The sales of this ticket can't be modified`); throw new UserError(`The sales of this ticket can't be modified`);
const item = await models.Item.findById(itemId); const item = await models.Item.findById(itemId, null, myOptions);
const ticket = await models.Ticket.findById(id, { const ticket = await models.Ticket.findById(id, {
include: { include: {
relation: 'client', relation: 'client',
@ -53,12 +64,12 @@ module.exports = Self => {
} }
} }
} }
}); }, myOptions);
const res = await models.Item.getVisibleAvailable(itemId, ticket.warehouseFk, ticket.shipped); const itemInfo = await models.Item.getVisibleAvailable(itemId, ticket.warehouseFk, ticket.shipped, myOptions);
const isPackaging = item.family == 'EMB'; const isPackaging = item.family == 'EMB';
if (!isPackaging && res.available < quantity) if (!isPackaging && itemInfo.available < quantity)
throw new UserError(`This item is not available`); throw new UserError(`This item is not available`);
const newSale = await models.Sale.create({ const newSale = await models.Sale.create({
@ -66,15 +77,15 @@ module.exports = Self => {
itemFk: item.id, itemFk: item.id,
concept: item.name, concept: item.name,
quantity: quantity quantity: quantity
}); }, myOptions);
await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id]); await Self.rawSql('CALL vn.sale_calculateComponent(?, NULL)', [newSale.id], myOptions);
const sale = await models.Sale.findById(newSale.id, { const sale = await models.Sale.findById(newSale.id, {
include: { include: {
relation: 'item' relation: 'item'
} }
}); }, myOptions);
const addition = `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`; const addition = `\r\n-${sale.itemFk}: ${sale.concept} (${sale.quantity})`;
@ -90,6 +101,12 @@ module.exports = Self => {
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
} }
if (tx) await tx.commit();
return sale; return sale;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; };
}; };

View File

@ -2,16 +2,13 @@ const app = require('vn-loopback/server/server');
describe('ticket addSale()', () => { describe('ticket addSale()', () => {
const ticketId = 13; const ticketId = 13;
let newSale;
afterAll(async done => {
const sale = await app.models.Sale.findById(newSale.id);
await sale.destroy();
done();
});
it('should create a new sale for the ticket with id 13', async() => { it('should create a new sale for the ticket with id 13', async() => {
const tx = await app.models.Ticket.beginTransaction({});
try {
const options = {transaction: tx};
const ctx = { const ctx = {
req: { req: {
accessToken: {userId: 9}, accessToken: {userId: 9},
@ -21,12 +18,25 @@ describe('ticket addSale()', () => {
}; };
const itemId = 4; const itemId = 4;
const quantity = 10; const quantity = 10;
newSale = await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity); const newSale = await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity, options);
expect(newSale.itemFk).toEqual(4); expect(newSale.itemFk).toEqual(4);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should not be able to add a sale if the item quantity is not available', async() => { it('should not be able to add a sale if the item quantity is not available', async() => {
const tx = await app.models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = { const ctx = {
req: { req: {
accessToken: {userId: 9}, accessToken: {userId: 9},
@ -37,17 +47,24 @@ describe('ticket addSale()', () => {
const itemId = 11; const itemId = 11;
const quantity = 10; const quantity = 10;
let error; await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity, options);
await app.models.Ticket.addSale(ctx, ticketId, itemId, quantity).catch(e => {
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e; error = e;
}).finally(() => { }
expect(error.message).toEqual(`This item is not available`); expect(error.message).toEqual(`This item is not available`);
}); });
expect(error).toBeDefined();
});
it('should not be able to add a sale if the ticket is not editable', async() => { it('should not be able to add a sale if the ticket is not editable', async() => {
const tx = await app.models.Ticket.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const ctx = { const ctx = {
req: { req: {
accessToken: {userId: 9}, accessToken: {userId: 9},
@ -58,13 +75,14 @@ describe('ticket addSale()', () => {
const notEditableTicketId = 1; const notEditableTicketId = 1;
const itemId = 4; const itemId = 4;
const quantity = 10; const quantity = 10;
let error; await app.models.Ticket.addSale(ctx, notEditableTicketId, itemId, quantity, options);
await app.models.Ticket.addSale(ctx, notEditableTicketId, itemId, quantity).catch(e => {
await tx.rollback();
} catch (e) {
await tx.rollback();
error = e; error = e;
}).finally(() => { }
expect(error.message).toEqual(`The sales of this ticket can't be modified`); expect(error.message).toEqual(`The sales of this ticket can't be modified`);
}); });
expect(error).toBeDefined();
});
}); });

View File

@ -24,7 +24,7 @@ describe('ticket canBeInvoiced()', () => {
const options = {transaction: tx}; const options = {transaction: tx};
const ticket = await models.Ticket.findById(ticketId, null, options); const ticket = await models.Ticket.findById(ticketId, null, options);
await ticket.updateAttribute('refFk', 'T1234567', options); await ticket.updateAttribute('refFk', 'T1111111', options);
const canBeInvoiced = await models.Ticket.canBeInvoiced([ticketId], options); const canBeInvoiced = await models.Ticket.canBeInvoiced([ticketId], options);
@ -33,6 +33,7 @@ describe('ticket canBeInvoiced()', () => {
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
throw e;
} }
}); });
@ -52,6 +53,7 @@ describe('ticket canBeInvoiced()', () => {
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
throw e;
} }
}); });
@ -75,6 +77,7 @@ describe('ticket canBeInvoiced()', () => {
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
throw e;
} }
}); });