From 6891190c72e019cb8c2c2232a028e27a47a1ea56 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 20 Oct 2022 15:05:46 +0200 Subject: [PATCH] refactor: create endpoint to unify code --- db/changes/10491-august/00-aclUsesMana.sql | 3 ++ .../ticket/back/methods/sale/updatePrice.js | 10 +++--- modules/ticket/back/methods/sale/usesMana.js | 31 +++++++++++++++++++ modules/ticket/back/models/sale.js | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 db/changes/10491-august/00-aclUsesMana.sql create mode 100644 modules/ticket/back/methods/sale/usesMana.js diff --git a/db/changes/10491-august/00-aclUsesMana.sql b/db/changes/10491-august/00-aclUsesMana.sql new file mode 100644 index 000000000..5bb7178dd --- /dev/null +++ b/db/changes/10491-august/00-aclUsesMana.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('Sale', 'usesMana', '*', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file diff --git a/modules/ticket/back/methods/sale/updatePrice.js b/modules/ticket/back/methods/sale/updatePrice.js index dbd4ed091..d325d6eaa 100644 --- a/modules/ticket/back/methods/sale/updatePrice.js +++ b/modules/ticket/back/methods/sale/updatePrice.js @@ -78,11 +78,13 @@ module.exports = Self => { const oldPrice = sale.price; const userId = ctx.req.accessToken.userId; - 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); + // 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); + const usesMana = await models.Sale.usesMana(); + console.log(usesMana ? 'mana' : 'buyerDiscount'); const componentCode = usesMana ? 'mana' : 'buyerDiscount'; const discount = await models.Component.findOne({where: {code: componentCode}}, myOptions); const componentId = discount.id; diff --git a/modules/ticket/back/methods/sale/usesMana.js b/modules/ticket/back/methods/sale/usesMana.js new file mode 100644 index 000000000..7ff2a5e2a --- /dev/null +++ b/modules/ticket/back/methods/sale/usesMana.js @@ -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; + }; +}; diff --git a/modules/ticket/back/models/sale.js b/modules/ticket/back/models/sale.js index 2a4457263..ae247fc24 100644 --- a/modules/ticket/back/models/sale.js +++ b/modules/ticket/back/models/sale.js @@ -8,6 +8,7 @@ module.exports = Self => { require('../methods/sale/recalculatePrice')(Self); require('../methods/sale/refund')(Self); require('../methods/sale/canEdit')(Self); + require('../methods/sale/usesMana')(Self); Self.validatesPresenceOf('concept', { message: `Concept cannot be blank`