From 7f6e9fc617ddf497a10dc3b00453f0fc5ac7dcf4 Mon Sep 17 00:00:00 2001 From: Vicente Falco Date: Mon, 18 Sep 2017 07:39:06 +0200 Subject: [PATCH] Service gets if a client has got a Role customer - getRoleCustomer --- .../client/common/methods/client/roles.js | 45 +++++++++++++++++++ services/client/common/models/client.js | 1 + 2 files changed, 46 insertions(+) create mode 100644 services/client/common/methods/client/roles.js diff --git a/services/client/common/methods/client/roles.js b/services/client/common/methods/client/roles.js new file mode 100644 index 000000000..6d494f35d --- /dev/null +++ b/services/client/common/methods/client/roles.js @@ -0,0 +1,45 @@ +module.exports = (Client) => { + Client.remoteMethod('getRoleCustomer', { + description: 'devuelve true/false si es Customer el client', + accessType: 'READ', + accepts: [ + { + arg: 'id', + type: 'string', + required: true, + description: 'Model id', + http: {source: 'path'} + }, + { + arg: 'filter', + type: 'object', + required: true, + description: 'Filter defining where', + http: function(ctx) { + return ctx.req.query; + } + } + ], + returns: { + arg: 'data', + type: String, + root: true + }, + http: { + path: `/:id/getRoleCustomer`, + verb: 'get' + } + }); + + Client.getRoleCustomer = (id, ctx, cb) => { + let query = `SELECT count(*) isCustomer FROM Account ac JOIN Role ON Role.id = ac.roleFK WHERE Role.\`name\`='customer' AND ac.id IN (?)`; + const params = [id]; + Client.rawSql(query, params, cb) + .then((response) => { + cb(null, response); + }) + .catch((reject) => { + cb (reject, null) + }); + }; +} \ No newline at end of file diff --git a/services/client/common/models/client.js b/services/client/common/models/client.js index ea0ba81bf..4c99400c7 100644 --- a/services/client/common/models/client.js +++ b/services/client/common/models/client.js @@ -12,6 +12,7 @@ module.exports = function(Client) { require('../methods/client/create.js')(Client); require('../methods/client/employee.js')(Client); require('../methods/client/filter.js')(Client); + require('../methods/client/roles.js')(Client); // Validations