Merge pull request '2801_ticket_sale_restrictions' (#634) from 2801_ticket_sale_restrictions into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #634 Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
commit
b4d8e25fd3
|
@ -86,6 +86,7 @@
|
||||||
"The current ticket can't be modified": "El ticket actual no puede ser modificado",
|
"The current ticket can't be modified": "El ticket actual no puede ser modificado",
|
||||||
"The current claim can't be modified": "La reclamación actual no puede ser modificada",
|
"The current claim can't be modified": "La reclamación actual no puede ser modificada",
|
||||||
"The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas",
|
"The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas",
|
||||||
|
"Sale(s) blocked, contact production": "Linea(s) bloqueada(s), contacte con produccion",
|
||||||
"Please select at least one sale": "Por favor selecciona al menos una linea",
|
"Please select at least one sale": "Por favor selecciona al menos una linea",
|
||||||
"All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket",
|
"All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket",
|
||||||
"NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada",
|
"NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada",
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethodCtx('canEdit', {
|
||||||
|
description: 'Check if all the received sales are aditable',
|
||||||
|
accessType: 'READ',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'sales',
|
||||||
|
type: ['object'],
|
||||||
|
required: true
|
||||||
|
}],
|
||||||
|
returns: {
|
||||||
|
type: 'boolean',
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/isEditable`,
|
||||||
|
verb: 'get'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.canEdit = async(ctx, sales, options) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
const userId = ctx.req.accessToken.userId;
|
||||||
|
const myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
const idsCollection = sales.map(sale => sale.id);
|
||||||
|
|
||||||
|
const saleTracking = await models.SaleTracking.find({where: {saleFk: {inq: idsCollection}}}, myOptions);
|
||||||
|
|
||||||
|
const hasSaleTracking = saleTracking.length;
|
||||||
|
|
||||||
|
const isProductionRole = await models.Account.hasRole(userId, 'production', myOptions);
|
||||||
|
|
||||||
|
const canEdit = (isProductionRole || !hasSaleTracking);
|
||||||
|
|
||||||
|
return canEdit;
|
||||||
|
};
|
||||||
|
};
|
|
@ -28,10 +28,16 @@ module.exports = Self => {
|
||||||
|
|
||||||
Self.deleteSales = async(ctx, sales, ticketId) => {
|
Self.deleteSales = async(ctx, sales, ticketId) => {
|
||||||
const models = Self.app.models;
|
const models = Self.app.models;
|
||||||
|
|
||||||
|
const canEditSales = await models.Sale.canEdit(ctx, sales);
|
||||||
|
|
||||||
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
const isTicketEditable = await models.Ticket.isEditable(ctx, ticketId);
|
||||||
if (!isTicketEditable)
|
if (!isTicketEditable)
|
||||||
throw new UserError(`The sales of this ticket can't be modified`);
|
throw new UserError(`The sales of this ticket can't be modified`);
|
||||||
|
|
||||||
|
if (!canEditSales)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let sale of sales) {
|
for (let sale of sales) {
|
||||||
const deletedSale = models.Sale.destroyById(sale.id);
|
const deletedSale = models.Sale.destroyById(sale.id);
|
||||||
|
|
|
@ -29,6 +29,11 @@ module.exports = Self => {
|
||||||
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 canEditSale = await models.Sale.canEdit(ctx, [id]);
|
||||||
|
|
||||||
|
if (!canEditSale)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
return Self.rawSql('CALL vn.sale_calculateComponent(?, null)', [id]);
|
return Self.rawSql('CALL vn.sale_calculateComponent(?, null)', [id]);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -37,6 +37,11 @@ module.exports = Self => {
|
||||||
if (!isTicketEditable)
|
if (!isTicketEditable)
|
||||||
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 canEditSale = await models.Sale.canEdit(ctx, sales);
|
||||||
|
|
||||||
|
if (!canEditSale)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let sale of sales) {
|
for (let sale of sales) {
|
||||||
const reservedSale = models.Sale.update({id: sale.id}, {reserved: reserved});
|
const reservedSale = models.Sale.update({id: sale.id}, {reserved: reserved});
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
|
describe('sale canEdit()', () => {
|
||||||
|
it('should return true if the role is production regardless of the saleTrackings', async() => {
|
||||||
|
const productionUserID = 49;
|
||||||
|
let ctx = {req: {accessToken: {userId: productionUserID}}};
|
||||||
|
|
||||||
|
const sales = [{id: 3}];
|
||||||
|
|
||||||
|
const result = await app.models.Sale.canEdit(ctx, sales);
|
||||||
|
|
||||||
|
expect(result).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return true if the role is not production and none of the sales has saleTracking', async() => {
|
||||||
|
const salesPersonUserID = 18;
|
||||||
|
let ctx = {req: {accessToken: {userId: salesPersonUserID}}};
|
||||||
|
|
||||||
|
const sales = [{id: 10}];
|
||||||
|
|
||||||
|
const result = await app.models.Sale.canEdit(ctx, sales);
|
||||||
|
|
||||||
|
expect(result).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if any of the sales has a saleTracking record', async() => {
|
||||||
|
const salesPersonUserID = 18;
|
||||||
|
let ctx = {req: {accessToken: {userId: salesPersonUserID}}};
|
||||||
|
|
||||||
|
const sales = [{id: 3}];
|
||||||
|
|
||||||
|
const result = await app.models.Sale.canEdit(ctx, sales);
|
||||||
|
|
||||||
|
expect(result).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,6 +1,7 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('sale updateConcept()', () => {
|
describe('sale updateConcept()', () => {
|
||||||
|
const ctx = {req: {accessToken: {userId: 9}}};
|
||||||
const saleId = 1;
|
const saleId = 1;
|
||||||
let originalSale;
|
let originalSale;
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ describe('sale updateConcept()', () => {
|
||||||
const newConcept = 'I am he new concept';
|
const newConcept = 'I am he new concept';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await app.models.Sale.updateConcept(undefined, newConcept);
|
await app.models.Sale.updateConcept(ctx, undefined, newConcept);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
err = e;
|
err = e;
|
||||||
}
|
}
|
||||||
|
@ -32,7 +33,7 @@ describe('sale updateConcept()', () => {
|
||||||
it('should update the sale concept', async() => {
|
it('should update the sale concept', async() => {
|
||||||
const newConcept = 'I am the new concept';
|
const newConcept = 'I am the new concept';
|
||||||
|
|
||||||
let response = await app.models.Sale.updateConcept(saleId, newConcept);
|
let response = await app.models.Sale.updateConcept(ctx, saleId, newConcept);
|
||||||
|
|
||||||
expect(response.concept).toEqual(newConcept);
|
expect(response.concept).toEqual(newConcept);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
const app = require('vn-loopback/server/server');
|
const app = require('vn-loopback/server/server');
|
||||||
|
|
||||||
describe('sale updateQuantity()', () => {
|
describe('sale updateQuantity()', () => {
|
||||||
|
const ctx = {req: {accessToken: {userId: 9}}};
|
||||||
|
|
||||||
it('should throw an error if the quantity is not a number', async() => {
|
it('should throw an error if the quantity is not a number', async() => {
|
||||||
let error;
|
let error;
|
||||||
|
|
||||||
await app.models.Sale.updateQuantity(1, 'wrong quantity!')
|
await app.models.Sale.updateQuantity(ctx, 1, 'wrong quantity!')
|
||||||
.catch(response => {
|
.catch(response => {
|
||||||
expect(response).toEqual(new Error('The value should be a number'));
|
expect(response).toEqual(new Error('The value should be a number'));
|
||||||
error = response;
|
error = response;
|
||||||
|
@ -16,7 +18,7 @@ describe('sale updateQuantity()', () => {
|
||||||
it('should throw an error if the quantity is greater than it should be', async() => {
|
it('should throw an error if the quantity is greater than it should be', async() => {
|
||||||
let error;
|
let error;
|
||||||
|
|
||||||
await app.models.Sale.updateQuantity(1, 99)
|
await app.models.Sale.updateQuantity(ctx, 1, 99)
|
||||||
.catch(response => {
|
.catch(response => {
|
||||||
expect(response).toEqual(new Error('The new quantity should be smaller than the old one'));
|
expect(response).toEqual(new Error('The new quantity should be smaller than the old one'));
|
||||||
error = response;
|
error = response;
|
||||||
|
@ -30,7 +32,7 @@ describe('sale updateQuantity()', () => {
|
||||||
|
|
||||||
expect(originalLineData.quantity).toEqual(5);
|
expect(originalLineData.quantity).toEqual(5);
|
||||||
|
|
||||||
await app.models.Sale.updateQuantity(1, 4);
|
await app.models.Sale.updateQuantity(ctx, 1, 4);
|
||||||
|
|
||||||
let modifiedLineData = await app.models.Sale.findOne({where: {id: 1}, fields: ['quantity']});
|
let modifiedLineData = await app.models.Sale.findOne({where: {id: 1}, fields: ['quantity']});
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('updateConcept', {
|
Self.remoteMethodCtx('updateConcept', {
|
||||||
description: 'Updates the concept of a sale',
|
description: 'Updates the concept of a sale',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
|
@ -24,8 +24,14 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.updateConcept = async(id, newConcept) => {
|
Self.updateConcept = async(ctx, id, newConcept) => {
|
||||||
let currentLine = await Self.app.models.Sale.findById(id);
|
const models = Self.app.models;
|
||||||
|
const currentLine = await models.Sale.findById(id);
|
||||||
|
|
||||||
|
const canEditSale = await models.Sale.canEdit(ctx, [id]);
|
||||||
|
|
||||||
|
if (!canEditSale)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
return await currentLine.updateAttributes({concept: newConcept});
|
return await currentLine.updateAttributes({concept: newConcept});
|
||||||
};
|
};
|
||||||
|
|
|
@ -55,6 +55,11 @@ module.exports = Self => {
|
||||||
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 canEditSale = await models.Sale.canEdit(ctx, [id]);
|
||||||
|
|
||||||
|
if (!canEditSale)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
const userId = ctx.req.accessToken.userId;
|
const userId = ctx.req.accessToken.userId;
|
||||||
|
|
||||||
let usesMana = await models.WorkerMana.findOne({where: {workerFk: userId}, fields: 'amount'}, options);
|
let usesMana = await models.WorkerMana.findOne({where: {workerFk: userId}, fields: 'amount'}, options);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
let UserError = require('vn-loopback/util/user-error');
|
let UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
Self.remoteMethod('updateQuantity', {
|
Self.remoteMethodCtx('updateQuantity', {
|
||||||
description: 'Changes the quantity of a sale',
|
description: 'Changes the quantity of a sale',
|
||||||
accessType: 'WRITE',
|
accessType: 'WRITE',
|
||||||
accepts: [{
|
accepts: [{
|
||||||
|
@ -26,11 +26,18 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.updateQuantity = async(id, quantity) => {
|
Self.updateQuantity = async(ctx, id, quantity) => {
|
||||||
|
const models = Self.app.models;
|
||||||
|
|
||||||
|
const canEditSale = await models.Sale.canEdit(ctx, [id]);
|
||||||
|
|
||||||
|
if (!canEditSale)
|
||||||
|
throw new UserError(`Sale(s) blocked, please contact production`);
|
||||||
|
|
||||||
if (isNaN(quantity))
|
if (isNaN(quantity))
|
||||||
throw new UserError(`The value should be a number`);
|
throw new UserError(`The value should be a number`);
|
||||||
|
|
||||||
let currentLine = await Self.app.models.Sale.findOne({where: {id: id}});
|
let currentLine = await models.Sale.findOne({where: {id: id}});
|
||||||
if (quantity > currentLine.quantity)
|
if (quantity > currentLine.quantity)
|
||||||
throw new UserError('The new quantity should be smaller than the old one');
|
throw new UserError('The new quantity should be smaller than the old one');
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ module.exports = Self => {
|
||||||
require('../methods/sale/updateQuantity')(Self);
|
require('../methods/sale/updateQuantity')(Self);
|
||||||
require('../methods/sale/updateConcept')(Self);
|
require('../methods/sale/updateConcept')(Self);
|
||||||
require('../methods/sale/recalculatePrice')(Self);
|
require('../methods/sale/recalculatePrice')(Self);
|
||||||
|
require('../methods/sale/canEdit')(Self);
|
||||||
|
|
||||||
Self.validatesPresenceOf('concept', {
|
Self.validatesPresenceOf('concept', {
|
||||||
message: `Concept cannot be blank`
|
message: `Concept cannot be blank`
|
||||||
|
|
Loading…
Reference in New Issue