refactor: create endpoint to unify code
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-10-20 15:05:46 +02:00
parent 29093d7d30
commit 6891190c72
4 changed files with 41 additions and 4 deletions

View File

@ -0,0 +1,3 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('Sale', 'usesMana', '*', 'ALLOW', 'ROLE', 'employee');

View File

@ -78,11 +78,13 @@ module.exports = Self => {
const oldPrice = sale.price; const oldPrice = sale.price;
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions); // const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions);
const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions); // const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions);
const workerDepartment = await models.WorkerDepartment.findById(userId); // const workerDepartment = await models.WorkerDepartment.findById(userId);
const usesMana = departments.find(department => department.id == workerDepartment.departmentFk); // const usesMana = departments.find(department => department.id == workerDepartment.departmentFk);
const usesMana = await models.Sale.usesMana();
console.log(usesMana ? 'mana' : 'buyerDiscount');
const componentCode = usesMana ? 'mana' : 'buyerDiscount'; const componentCode = usesMana ? 'mana' : 'buyerDiscount';
const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions); const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions);
const componentId = discount.id; const componentId = discount.id;

View File

@ -0,0 +1,31 @@
module.exports = Self => {
Self.remoteMethodCtx('usesMana', {
description: 'Returns if the worker uses mana',
accessType: 'WRITE',
accepts: [],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/usesMana`,
verb: 'POST'
}
});
Self.usesMana = async(ctx, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const salesDepartment = await models.Department.findOne({where: {code: 'VT'}, fields: 'id'}, myOptions);
const departments = await models.Department.getLeaves(salesDepartment.id, null, myOptions);
const workerDepartment = await models.WorkerDepartment.findById(userId);
const usesMana = departments.find(department => department.id == workerDepartment.departmentFk);
return usesMana ? true : false;
};
};

View File

@ -8,6 +8,7 @@ module.exports = Self => {
require('../methods/sale/recalculatePrice')(Self); require('../methods/sale/recalculatePrice')(Self);
require('../methods/sale/refund')(Self); require('../methods/sale/refund')(Self);
require('../methods/sale/canEdit')(Self); require('../methods/sale/canEdit')(Self);
require('../methods/sale/usesMana')(Self);
Self.validatesPresenceOf('concept', { Self.validatesPresenceOf('concept', {
message: `Concept cannot be blank` message: `Concept cannot be blank`