salix/modules/client/back/methods/client/isValidClient.js

52 lines
1.5 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('isValidClient', {
description: 'Checks if the user havent got the role employee, is active and has verified data',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'string',
required: true,
description: 'The user id',
http: {source: 'path'}
}, {
arg: 'context',
type: 'object',
required: true,
description: 'Filter defining where',
http: function(context) {
return context.req.query;
}
}
],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/isValidClient`,
verb: 'GET'
}
});
Self.isValidClient = async id => {
let query =
`SELECT r.name
FROM salix.Account a
JOIN vn.client c ON a.id = c.id
JOIN salix.RoleMapping rm ON rm.principalId = a.id
JOIN salix.Role r ON r.id = rm.roleId
WHERE a.id = ? AND c.isActive AND c.isTaxDataChecked`;
let roleNames = await Self.rawSql(query, [id]);
let isEmployee = roleNames.findIndex(role => {
return role.name === 'employee';
});
if (!roleNames.length || isEmployee > -1 ) return false;
return true;
};
};