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

40 lines
1.1 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('hasCustomerRole', {
description: 'Comprueba si un usuario tiene el rol de cliente',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'string',
required: true,
description: 'The user id',
http: {source: 'path'}
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/hasCustomerRole`,
verb: 'GET'
}
});
Self.hasCustomerRole = async(id, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const query = `
SELECT COUNT(*) > 0 isCustomer
FROM salix.Account A
JOIN salix.Role r ON r.id = A.roleFK
WHERE r.name = 'customer'
AND A.id IN (?)`;
const [result] = await Self.rawSql(query, [id], myOptions);
const {isCustomer} = result;
return isCustomer;
};
};