refs #6199 feat(sale): best quantity restriction
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2023-10-13 15:14:48 +02:00
parent f00a4e63a6
commit ab1cc34015
2 changed files with 42 additions and 21 deletions

View File

@ -1,4 +1,4 @@
let UserError = require('vn-loopback/util/user-error');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateQuantity', {
@ -64,26 +64,6 @@ module.exports = Self => {
const sale = await models.Sale.findById(id, filter, myOptions);
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
if (newQuantity > sale.quantity && !isRoleAdvanced)
throw new UserError('The new quantity should be smaller than the old one');
const ticketRefund = await models.TicketRefund.findOne({
where: {refundTicketFk: sale.ticketFk},
fields: ['id']}
, myOptions);
const item = await models.Item.findOne({
where: {id: sale.itemFk},
fields: ['minQuantity']}
, myOptions);
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 = sale.quantity;
const result = await sale.updateAttributes({quantity: newQuantity}, myOptions);

View File

@ -1,3 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/sale/getClaimableFromTicket')(Self);
require('../methods/sale/reserve')(Self);
@ -13,4 +15,43 @@ module.exports = Self => {
Self.validatesPresenceOf('concept', {
message: `Concept cannot be blank`
});
Self.observe('before save', async ctx => {
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 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 ticketRefund = await models.TicketRefund.findOne({
where: {refundTicketFk: ticketId},
fields: ['id']}
, ctx.options);
const item = await models.Item.findOne({
where: {id: itemId},
fields: ['minQuantity']}
, ctx.options);
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;
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
if (newQuantity > oldQuantity && !isRoleAdvanced)
throw new UserError('The new quantity should be smaller than the old one');
});
};