From b3c7511e59277a081e8cc2480ed36bf6aeff1fc8 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 18 Oct 2023 08:52:06 +0200 Subject: [PATCH] refs #6067 feat(url): getByUser --- back/methods/url/getByUser.js | 40 +++++++++++++++++++++++ back/methods/url/specs/getByUser.spec.js | 19 +++++++++++ back/models/url.js | 3 ++ back/models/url.json | 9 +---- back/models/vn-user.js | 4 +-- db/changes/234201/00-aclUrlHedera.sql | 6 ++-- modules/worker/back/methods/worker/new.js | 1 - 7 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 back/methods/url/getByUser.js create mode 100644 back/methods/url/specs/getByUser.spec.js create mode 100644 back/models/url.js diff --git a/back/methods/url/getByUser.js b/back/methods/url/getByUser.js new file mode 100644 index 000000000..dd4805182 --- /dev/null +++ b/back/methods/url/getByUser.js @@ -0,0 +1,40 @@ +module.exports = function(Self) { + Self.remoteMethod('getByUser', { + description: 'returns the starred modules for the current user', + accessType: 'READ', + accepts: [{ + arg: 'userId', + type: 'number', + description: 'The user id', + required: true, + http: {source: 'path'} + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:userId/get-by-user`, + verb: 'GET' + } + }); + + Self.getByUser = async userId => { + const models = Self.app.models; + const appNames = ['hedera']; + const filter = { + fields: ['appName', 'url'], + where: { + appName: {inq: appNames}, + environment: process.env.NODE_ENV ?? 'development', + } + }; + + const isWorker = await models.Account.findById(userId, {fields: ['id']}); + if (!isWorker) + return models.Url.find(filter); + + appNames.push('salix'); + return models.Url.find(filter); + }; +}; diff --git a/back/methods/url/specs/getByUser.spec.js b/back/methods/url/specs/getByUser.spec.js new file mode 100644 index 000000000..f6af6ec00 --- /dev/null +++ b/back/methods/url/specs/getByUser.spec.js @@ -0,0 +1,19 @@ +const {models} = require('vn-loopback/server/server'); + +describe('getByUser()', () => { + const worker = 1; + const notWorker = 2; + it(`should return only hedera url if not is worker`, async() => { + const urls = await models.Url.getByUser(notWorker); + + expect(urls.length).toEqual(1); + expect(urls[0].appName).toEqual('hedera'); + }); + + it(`should return more than hedera url`, async() => { + const urls = await models.Url.getByUser(worker); + + expect(urls.length).toBeGreaterThan(1); + expect(urls.find(url => url.appName == 'salix').appName).toEqual('salix'); + }); +}); diff --git a/back/models/url.js b/back/models/url.js new file mode 100644 index 000000000..216d149ba --- /dev/null +++ b/back/models/url.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/url/getByUser')(Self); +}; diff --git a/back/models/url.json b/back/models/url.json index 13f50b099..8610ff28b 100644 --- a/back/models/url.json +++ b/back/models/url.json @@ -21,12 +21,5 @@ "type": "string", "required": true } - }, - "acls": [{ - "accessType": "READ", - "principalType": "ROLE", - "principalId": "$everyone", - "permission": "ALLOW" - }] - + } } diff --git a/back/models/vn-user.js b/back/models/vn-user.js index 8e9b59aab..66af807b8 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -222,7 +222,6 @@ module.exports = function(Self) { ]} }); - const isWorker = instance.isWorker || await Self.app.models.Account.findById(instance.id, null, ctx.options); class Mailer { async send(verifyOptions, cb) { const params = { @@ -236,11 +235,12 @@ module.exports = function(Self) { cb(null, verifyOptions.to); } } + console.log('instance.id', instance.id); const options = { type: 'email', to: newEmail, from: {}, - redirect: `${liliumUrl.url}verifyEmail?isWorker=${!!isWorker}`, + redirect: `${liliumUrl.url}verifyEmail?userId=${instance.id}`, template: false, mailer: new Mailer, host: url[1].split('/')[2], diff --git a/db/changes/234201/00-aclUrlHedera.sql b/db/changes/234201/00-aclUrlHedera.sql index 0d38a2ae8..79d9fb4c8 100644 --- a/db/changes/234201/00-aclUrlHedera.sql +++ b/db/changes/234201/00-aclUrlHedera.sql @@ -3,7 +3,5 @@ INSERT INTO `salix`.`url` (`appName`, `environment`, `url`) ('hedera', 'test', 'https://test-shop.verdnatura.es/'), ('hedera', 'production', 'https://shop.verdnatura.es/'); -DELETE FROM `salix`.`ACL` - WHERE model = 'Url' - AND 'accessType' = 'READ' - AND principalId = 'employee'; +INSERT INTO `salix`.`ACL` ( model, property, accessType, permission, principalType, principalId) + VALUES('Url', 'getByUser', 'READ', 'ALLOW', 'ROLE', '$everyone'); diff --git a/modules/worker/back/methods/worker/new.js b/modules/worker/back/methods/worker/new.js index 7c8978ec6..199a3be62 100644 --- a/modules/worker/back/methods/worker/new.js +++ b/modules/worker/back/methods/worker/new.js @@ -155,7 +155,6 @@ module.exports = Self => { password: randomPassword.password, email: args.email, roleFk: workerConfig.roleFk, - isWorker: true // to verifyEmail }, myOptions );