2023-10-13 13:14:48 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
2018-08-30 09:02:50 +00:00
|
|
|
require('../methods/sale/getClaimableFromTicket')(Self);
|
2018-07-03 13:00:16 +00:00
|
|
|
require('../methods/sale/reserve')(Self);
|
2020-07-01 12:28:05 +00:00
|
|
|
require('../methods/sale/deleteSales')(Self);
|
2018-06-28 13:21:04 +00:00
|
|
|
require('../methods/sale/updatePrice')(Self);
|
|
|
|
require('../methods/sale/updateQuantity')(Self);
|
2019-09-06 09:43:15 +00:00
|
|
|
require('../methods/sale/updateConcept')(Self);
|
2019-11-21 11:00:56 +00:00
|
|
|
require('../methods/sale/recalculatePrice')(Self);
|
2022-04-04 11:26:53 +00:00
|
|
|
require('../methods/sale/refund')(Self);
|
2021-05-26 07:14:18 +00:00
|
|
|
require('../methods/sale/canEdit')(Self);
|
2022-10-20 13:05:46 +00:00
|
|
|
require('../methods/sale/usesMana')(Self);
|
2019-07-02 10:12:15 +00:00
|
|
|
|
|
|
|
Self.validatesPresenceOf('concept', {
|
|
|
|
message: `Concept cannot be blank`
|
|
|
|
});
|
2023-10-13 13:14:48 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2018-04-10 05:48:04 +00:00
|
|
|
};
|
2023-10-13 13:14:48 +00:00
|
|
|
|