refs #6067 feat(url): getByUser

This commit is contained in:
Alex Moreno 2023-10-18 08:52:06 +02:00
parent 6871ce4b15
commit b3c7511e59
7 changed files with 67 additions and 15 deletions

View File

@ -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);
};
};

View File

@ -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');
});
});

3
back/models/url.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/url/getByUser')(Self);
};

View File

@ -21,12 +21,5 @@
"type": "string",
"required": true
}
},
"acls": [{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}]
}
}

View File

@ -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],

View File

@ -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');

View File

@ -155,7 +155,6 @@ module.exports = Self => {
password: randomPassword.password,
email: args.email,
roleFk: workerConfig.roleFk,
isWorker: true // to verifyEmail
},
myOptions
);