salix/services/loopback/common/methods/client/hasCustomerRole.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

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'}
}, {
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/hasCustomerRole`,
verb: 'GET'
}
});
Self.hasCustomerRole = (id, context, callback) => {
let 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 (?)`;
Self.rawSql(query, [id]).then(
instances => callback(null, instances[0]),
err => callback(err)
);
};
};