priceDifference test fix + refactor

This commit is contained in:
Carlos Jimenez Ruiz 2020-10-02 09:39:58 +02:00
parent eb99ce2f43
commit ff3b75267a
4 changed files with 38 additions and 41 deletions

View File

@ -96,7 +96,7 @@ async function backTestOnce(done) {
let options = {
errorOnFail: false,
config: {
random: false
random: true
}
};

View File

@ -48,31 +48,26 @@ module.exports = Self => {
include: {relation: 'ticket'}
}, options);
const res = await models.Item.getVisibleAvailable(ctx.args.itemFk, request.ticket().warehouseFk, request.ticket().shipped);
const itemStock = await models.Item.getVisibleAvailable(ctx.args.itemFk, request.ticket().warehouseFk, request.ticket().shipped);
const isAvailable = itemStock.available > 0;
if (res.available < 0)
if (!isAvailable)
throw new UserError(`This item is not available`);
if (request.saleFk) {
sale = await models.Sale.findById(request.saleFk, null, options);
await sale.updateAttributes({
itemFk: ctx.args.itemFk,
quantity: ctx.args.quantity,
concept: item.name,
}, options);
} else {
sale = await models.Sale.create({
ticketFk: request.ticketFk,
itemFk: ctx.args.itemFk,
quantity: ctx.args.quantity,
concept: item.name
}, options);
await request.updateAttributes({
saleFk: sale.id,
itemFk: sale.itemFk,
isOk: true
}, options);
}
if (request.saleFk)
throw new UserError(`This request already contains a sale`);
sale = await models.Sale.create({
ticketFk: request.ticketFk,
itemFk: ctx.args.itemFk,
quantity: ctx.args.quantity,
concept: item.name
}, options);
await request.updateAttributes({
saleFk: sale.id,
itemFk: sale.itemFk,
isOk: true
}, options);
query = `CALL vn.sale_calculateComponent(?, NULL)`;
await Self.rawSql(query, [sale.id], options);

View File

@ -2,7 +2,6 @@ const app = require('vn-loopback/server/server');
describe('ticket-request confirm()', () => {
let originalRequest;
let originalSale;
let createdSaleId;
let ctx = {
req: {
@ -16,8 +15,13 @@ describe('ticket-request confirm()', () => {
afterAll(async done => {
await originalRequest.updateAttributes(originalRequest);
await originalSale.updateAttributes(originalSale);
await app.models.Sale.destroyById(createdSaleId);
await app.models.Sale.destroyById(addedSale.id);
await app.models.Item.rawSql(`
TRUNCATE TABLE cache.last_buy
`);
done();
});
@ -56,10 +60,9 @@ describe('ticket-request confirm()', () => {
expect(error.message).toEqual(`This item is not available`);
});
it(`should update the sale details if the request already contains a sale id`, async() => {
it(`should throw if there's a sale id`, async() => {
const requestId = 4;
const saleId = 11;
const itemId = 1;
const itemId = 2;
const quantity = 10;
ctx.args = {
@ -68,17 +71,15 @@ describe('ticket-request confirm()', () => {
quantity: quantity
};
originalRequest = await app.models.TicketRequest.findById(requestId);
originalSale = await app.models.Sale.findById(saleId);
let error;
const request = await app.models.TicketRequest.findById(requestId);
await request.updateAttributes({saleFk: saleId});
await app.models.TicketRequest.confirm(ctx);
try {
await app.models.TicketRequest.confirm(ctx);
} catch (err) {
error = err;
}
let updatedSale = await app.models.Sale.findById(saleId);
expect(updatedSale.itemFk).toEqual(itemId);
expect(updatedSale.quantity).toEqual(quantity);
expect(error.message).toEqual(`This request already contains a sale`);
});
it(`should create a new sale for the the request if there's no sale id`, async() => {
@ -86,6 +87,8 @@ describe('ticket-request confirm()', () => {
const itemId = 1;
const quantity = 10;
originalRequest = await app.models.TicketRequest.findById(requestId);
ctx.args = {
itemFk: itemId,
id: requestId,

View File

@ -87,15 +87,14 @@ module.exports = Self => {
const map = new Map();
// Sale price component, one per sale
difComponents.forEach(difComponent => {
for (difComponent of difComponents)
map.set(difComponent.saleFk, difComponent);
});
function round(value) {
return Math.round(value * 100) / 100;
}
salesObj.items.forEach(sale => {
for (sale of salesObj.items) {
const difComponent = map.get(sale.id);
if (difComponent) {
@ -110,7 +109,7 @@ module.exports = Self => {
salesObj.totalUnitPrice += sale.price;
salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
});
}
return salesObj;
};