From 4c18efa5894cea23d3600d8c7711deabd46ead49 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 14 Apr 2023 13:05:57 +0200 Subject: [PATCH] =?UTF-8?q?refs=20#5128=20creado=20m=C3=A9todo=20setRating?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../00-aclClientInforma.sql | 0 .../{231401 => 231601}/00-clientInforma.sql | 2 +- .../client/back/methods/client/setRating.js | 62 +++++++++++++++++++ modules/client/back/models/client-methods.js | 1 + modules/client/back/models/client.js | 40 +----------- .../client/front/credit-management/index.html | 5 +- modules/client/front/locale/es.yml | 2 + modules/client/front/routes.json | 14 +++-- 8 files changed, 81 insertions(+), 45 deletions(-) rename db/changes/{231401 => 231601}/00-aclClientInforma.sql (100%) rename db/changes/{231401 => 231601}/00-clientInforma.sql (85%) create mode 100644 modules/client/back/methods/client/setRating.js diff --git a/db/changes/231401/00-aclClientInforma.sql b/db/changes/231601/00-aclClientInforma.sql similarity index 100% rename from db/changes/231401/00-aclClientInforma.sql rename to db/changes/231601/00-aclClientInforma.sql diff --git a/db/changes/231401/00-clientInforma.sql b/db/changes/231601/00-clientInforma.sql similarity index 85% rename from db/changes/231401/00-clientInforma.sql rename to db/changes/231601/00-clientInforma.sql index 25405ef4d..9bf757fc3 100644 --- a/db/changes/231401/00-clientInforma.sql +++ b/db/changes/231601/00-clientInforma.sql @@ -13,4 +13,4 @@ CREATE TABLE `vn`.`clientInforma` ( KEY `informaClientFk` (`clientFk`), CONSTRAINT `informa_ClienteFk` FOREIGN KEY (`clientFk`) REFERENCES `client` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `informa_workers_fk` FOREIGN KEY (`workerFk`) REFERENCES `worker` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE -) ENGINE=InnoDB CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; +) ENGINE=InnoDB CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci COMMENT='información proporcionada por Informa, se actualiza desde el hook de client (salix)'; diff --git a/modules/client/back/methods/client/setRating.js b/modules/client/back/methods/client/setRating.js new file mode 100644 index 000000000..06e7ebf1e --- /dev/null +++ b/modules/client/back/methods/client/setRating.js @@ -0,0 +1,62 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('setRating', { + description: 'Change role and hasGrant if user has setRating', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The user id', + http: {source: 'path'} + }, + { + arg: 'rating', + type: 'number' + }, + { + arg: 'recommendedCredit', + type: 'number' + } + ], + http: { + path: `/:id/setRating`, + verb: 'POST' + } + }); + + Self.setRating = async function(ctx, id, rating, recommendedCredit, options) { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + let tx; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const isFinancial = await models.Account.hasRole(userId, 'financial', myOptions); + if (!isFinancial) + throw new UserError(`You don't have enough privileges`); + + const client = await Self.findById(id, null, myOptions); + const clientUpdated = await client.updateAttributes({ + rating: rating, + recommendedCredit: recommendedCredit + }, myOptions); + + if (tx) await tx.commit(); + + return clientUpdated; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/client/back/models/client-methods.js b/modules/client/back/models/client-methods.js index 3538dbeb8..3b1a588ac 100644 --- a/modules/client/back/models/client-methods.js +++ b/modules/client/back/models/client-methods.js @@ -47,4 +47,5 @@ module.exports = Self => { require('../methods/client/consumptionSendQueued')(Self); require('../methods/client/filter')(Self); require('../methods/client/getClientOrSupplierReference')(Self); + require('../methods/client/setRating')(Self); }; diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 579c6a8d4..94c583dcc 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -447,44 +447,8 @@ module.exports = Self => { Self.changeCreditManagement = async function changeCreditManagement(ctx, finalState, changes) { const models = Self.app.models; - const userId = ctx.options.accessToken.userId; - - // const isFinancialBoss = await models.Account.hasRole(userId, 'financialBoss', ctx.options); - // if (!isFinancialBoss) { - // const lastCredit = await models.ClientCredit.findOne({ - // where: { - // clientFk: finalState.id - // }, - // order: 'id DESC' - // }, ctx.options); - - // const lastAmount = lastCredit && lastCredit.amount; - // const lastWorkerId = lastCredit && lastCredit.workerFk; - // const lastWorkerIsFinancialBoss = await models.Account.hasRole(lastWorkerId, 'financialBoss', ctx.options); - - // if (lastAmount == 0 && lastWorkerIsFinancialBoss) - // throw new UserError(`You can't change the credit set to zero from a financialBoss`); - - // const creditLimits = await models.ClientCreditLimit.find({ - // fields: ['roleFk'], - // where: { - // maxAmount: {gte: changes.credit} - // } - // }, ctx.options); - - // const requiredRoles = []; - // for (limit of creditLimits) - // requiredRoles.push(limit.roleFk); - - // const userRequiredRoles = await models.RoleMapping.count({ - // roleId: {inq: requiredRoles}, - // principalType: 'USER', - // principalId: userId - // }, ctx.options); - - // if (userRequiredRoles <= 0) - // throw new UserError(`You don't have enough privileges to set this credit amount`); - // } + const loopBackContext = LoopBackContext.getCurrentContext(); + const userId = loopBackContext.active.accessToken.userId; await models.ClientInforma.create({ clientFk: finalState.id, diff --git a/modules/client/front/credit-management/index.html b/modules/client/front/credit-management/index.html index 78cc6edb5..85ecb1f96 100644 --- a/modules/client/front/credit-management/index.html +++ b/modules/client/front/credit-management/index.html @@ -1,11 +1,11 @@ - + + save="post">
@@ -37,6 +37,7 @@ +