fix(sale_updateQuantity): if clientType is loses skip test: fix updateQuantity activeCtx and add test for minQuantity
This commit is contained in:
parent
ab1cc34015
commit
b769fa9bcf
|
@ -189,5 +189,6 @@
|
|||
"The sales do not exists": "The sales do not exists",
|
||||
"Ticket without Route": "Ticket without route",
|
||||
"Booking completed": "Booking complete",
|
||||
"The ticket is in preparation": "The ticket [{{ticketId}}]({{{ticketUrl}}}) of the sales person {{salesPersonId}} is in preparation"
|
||||
"The ticket is in preparation": "The ticket [{{ticketId}}]({{{ticketUrl}}}) of the sales person {{salesPersonId}} is in preparation",
|
||||
"You can only add negative amounts in refund tickets": "You can only add negative amounts in refund tickets"
|
||||
}
|
|
@ -64,7 +64,7 @@ describe('claim regularizeClaim()', () => {
|
|||
|
||||
claimEnds = await importTicket(ticketId, claimId, userId, options);
|
||||
|
||||
for (claimEnd of claimEnds)
|
||||
for (const claimEnd of claimEnds)
|
||||
await claimEnd.updateAttributes({claimDestinationFk: trashDestination}, options);
|
||||
|
||||
let claimBefore = await models.Claim.findById(claimId, null, options);
|
||||
|
|
|
@ -2,20 +2,6 @@ const models = require('vn-loopback/server/server').models;
|
|||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
describe('sale updateQuantity()', () => {
|
||||
beforeAll(async() => {
|
||||
const activeCtx = {
|
||||
accessToken: {userId: 9},
|
||||
http: {
|
||||
req: {
|
||||
headers: {origin: 'http://localhost'}
|
||||
}
|
||||
}
|
||||
};
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
||||
active: activeCtx
|
||||
});
|
||||
});
|
||||
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: 9},
|
||||
|
@ -23,6 +9,18 @@ describe('sale updateQuantity()', () => {
|
|||
__: () => {}
|
||||
}
|
||||
};
|
||||
function getActiveCtx(userId) {
|
||||
return {
|
||||
active: {
|
||||
accessToken: {userId},
|
||||
http: {
|
||||
req: {
|
||||
headers: {origin: 'http://localhost'}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
it('should throw an error if the quantity is greater than it should be', async() => {
|
||||
const ctx = {
|
||||
|
@ -32,6 +30,8 @@ describe('sale updateQuantity()', () => {
|
|||
__: () => {}
|
||||
}
|
||||
};
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
|
||||
let error;
|
||||
|
@ -50,6 +50,8 @@ describe('sale updateQuantity()', () => {
|
|||
});
|
||||
|
||||
it('should add quantity if the quantity is greater than it should be and is role advanced', async() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
const saleId = 17;
|
||||
const buyerId = 35;
|
||||
|
@ -87,6 +89,8 @@ describe('sale updateQuantity()', () => {
|
|||
});
|
||||
|
||||
it('should update the quantity of a given sale current line', async() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
const saleId = 25;
|
||||
const newQuantity = 4;
|
||||
|
@ -119,6 +123,8 @@ describe('sale updateQuantity()', () => {
|
|||
__: () => {}
|
||||
}
|
||||
};
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
|
||||
|
||||
const saleId = 17;
|
||||
const newQuantity = -10;
|
||||
|
||||
|
@ -140,6 +146,8 @@ describe('sale updateQuantity()', () => {
|
|||
});
|
||||
|
||||
it('should update a negative quantity when is a ticket refund', async() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
const saleId = 13;
|
||||
const newQuantity = -10;
|
||||
|
@ -159,4 +167,30 @@ describe('sale updateQuantity()', () => {
|
|||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error if the quantity is less than the minimum quantity of the item', async() => {
|
||||
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9));
|
||||
|
||||
const tx = await models.Sale.beginTransaction({});
|
||||
const itemId = 2;
|
||||
const saleId = 17;
|
||||
const newQuantity = 29;
|
||||
|
||||
let error;
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const item = await models.Item.findById(itemId, null, options);
|
||||
await item.updateAttribute('minQuantity', 30, options);
|
||||
|
||||
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toEqual(new Error('The amount cannot be less than the minimum'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('updateQuantity', {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const LoopBackContext = require('loopback-context');
|
||||
|
||||
module.exports = Self => {
|
||||
require('../methods/sale/getClaimableFromTicket')(Self);
|
||||
|
@ -20,21 +21,46 @@ module.exports = Self => {
|
|||
const models = Self.app.models;
|
||||
const changes = ctx.data || ctx.instance;
|
||||
const instance = ctx.currentInstance;
|
||||
console.log(ctx?.req?.accessToken, instance, changes);
|
||||
const newQuantity = changes?.quantity;
|
||||
if (newQuantity === null) return;
|
||||
|
||||
const newQuantity = changes?.quantity;
|
||||
if (newQuantity == null) return;
|
||||
|
||||
const loopBackContext = LoopBackContext.getCurrentContext();
|
||||
ctx.req = loopBackContext.active;
|
||||
const canEditQuantity = await models.ACL.checkAccessAcl(ctx, 'Sale', 'canForceQuantity', 'WRITE');
|
||||
if (canEditQuantity) return;
|
||||
|
||||
const ticketId = changes?.ticketFk || instance?.ticketFk;
|
||||
const itemId = changes?.itemFk || instance?.itemFk;
|
||||
|
||||
const ticket = await models.Ticket.findById(
|
||||
ticketId,
|
||||
{
|
||||
fields: ['id', 'clientFk'],
|
||||
include: {
|
||||
relation: 'client',
|
||||
scope: {
|
||||
fields: ['id', 'clientTypeFk'],
|
||||
include: {
|
||||
relation: 'type',
|
||||
scope: {
|
||||
fields: ['code', 'description']
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
ctx.options);
|
||||
if (ticket.client().type().code === 'loses') return;
|
||||
|
||||
const ticketRefund = await models.TicketRefund.findOne({
|
||||
where: {refundTicketFk: ticketId},
|
||||
fields: ['id']}
|
||||
, ctx.options);
|
||||
|
||||
if (newQuantity < 0 && !ticketRefund)
|
||||
throw new UserError('You can only add negative amounts in refund tickets');
|
||||
|
||||
const item = await models.Item.findOne({
|
||||
where: {id: itemId},
|
||||
fields: ['minQuantity']}
|
||||
|
@ -43,9 +69,6 @@ module.exports = Self => {
|
|||
if (newQuantity < item.minQuantity && !ticketRefund)
|
||||
throw new UserError('The amount cannot be less than the minimum');
|
||||
|
||||
if (newQuantity < 0 && !ticketRefund)
|
||||
throw new UserError('You can only add negative amounts in refund tickets');
|
||||
|
||||
const oldQuantity = instance?.quantity;
|
||||
if (oldQuantity === null) return;
|
||||
|
||||
|
|
Loading…
Reference in New Issue