diff --git a/services/client/common/models/my-model.js b/services/client/common/models/my-model.js index 9246e02a9..f6dfad090 100644 --- a/services/client/common/models/my-model.js +++ b/services/client/common/models/my-model.js @@ -1,6 +1,3 @@ -let loopBackContext = require('loopback-context'); -let app = require('../../server/server'); - module.exports = function(self) { self.setup = function() { @@ -29,25 +26,40 @@ module.exports = function(self) { //this.disableRemoteMethod(method, disableMethods[method]); } }; - - self.getUser = function() { - let currentUser = loopBackContext.getCurrentContext(); - let userId = currentUser.get('currentUser'); - return userId; - }; - - self.getEmployee = function() { - let userId = self.getUser(); - let employee = app.models.Employee; - return employee.findOne({where: {userFk: userId}}); - }; - self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); }); }; + self.remoteMethodCtx = function(methodName, args) { + if(args.accepts !== undefined && Array.isArray(args.accepts)) { + var ctx = { + arg: 'context', + type: 'object', + http: function(ctx) { + return ctx; + } + } + args.accepts.unshift(ctx); + } + this.remoteMethod(methodName, args); + }; + + self.connectToService = function(ctx, dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer(ctx.req.accessToken.id).toString('base64'), + sendImmediately: true + }; + }; + + self.disconnectFromService = function(dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer("").toString('base64'), + sendImmediately: true + }; + }; + self.installMethod = function(methodName, filterCb) { this.remoteMethod(methodName, { description: 'List items using a filter', diff --git a/services/production/common/methods/ticket-state/change-state.js b/services/production/common/methods/ticket-state/change-state.js index 90f2f041d..4e7cbbffb 100644 --- a/services/production/common/methods/ticket-state/change-state.js +++ b/services/production/common/methods/ticket-state/change-state.js @@ -1,5 +1,5 @@ module.exports = function(TicketState) { - TicketState.remoteMethod('changeState', { + TicketState.remoteMethodCtx('changeState', { description: 'Change state of tickets', accepts: [ { @@ -27,10 +27,15 @@ module.exports = function(TicketState) { } }); - TicketState.changeState = function(tickets, state, cb) { - icketState.getEmployee().then(function(emp){ - changeState(emp, tickets, state, cb); + TicketState.changeState = function(ctx, tickets, state, cb) { + TicketState.connectToService(ctx, "client"); + + TicketState.app.models.Employee.findOne({where: {userFk: ctx.req.accessToken.userId}}, function(err, emp){ + if(!err) + changeState(emp.id, tickets, state, cb); }); + + TicketState.disconnectFromService("client"); }; var changeState = function(emp, tickets, state, cb){ diff --git a/services/production/common/models/employee.json b/services/production/common/models/employee.json new file mode 100644 index 000000000..bb676a071 --- /dev/null +++ b/services/production/common/models/employee.json @@ -0,0 +1,13 @@ +{ + "name": "Employee", + "base": "MyModel", + "validateUpsert": true, + "properties": { + "id": { + "type": "Number", + "id": true, + "description": "Identifier" + } + }, + "acls": [] +} \ No newline at end of file diff --git a/services/production/common/models/my-model.js b/services/production/common/models/my-model.js index 9246e02a9..f6dfad090 100644 --- a/services/production/common/models/my-model.js +++ b/services/production/common/models/my-model.js @@ -1,6 +1,3 @@ -let loopBackContext = require('loopback-context'); -let app = require('../../server/server'); - module.exports = function(self) { self.setup = function() { @@ -29,25 +26,40 @@ module.exports = function(self) { //this.disableRemoteMethod(method, disableMethods[method]); } }; - - self.getUser = function() { - let currentUser = loopBackContext.getCurrentContext(); - let userId = currentUser.get('currentUser'); - return userId; - }; - - self.getEmployee = function() { - let userId = self.getUser(); - let employee = app.models.Employee; - return employee.findOne({where: {userFk: userId}}); - }; - self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); }); }; + self.remoteMethodCtx = function(methodName, args) { + if(args.accepts !== undefined && Array.isArray(args.accepts)) { + var ctx = { + arg: 'context', + type: 'object', + http: function(ctx) { + return ctx; + } + } + args.accepts.unshift(ctx); + } + this.remoteMethod(methodName, args); + }; + + self.connectToService = function(ctx, dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer(ctx.req.accessToken.id).toString('base64'), + sendImmediately: true + }; + }; + + self.disconnectFromService = function(dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer("").toString('base64'), + sendImmediately: true + }; + }; + self.installMethod = function(methodName, filterCb) { this.remoteMethod(methodName, { description: 'List items using a filter', diff --git a/services/production/common/models/ticket-state.js b/services/production/common/models/ticket-state.js index f6890964d..58606e1e9 100644 --- a/services/production/common/models/ticket-state.js +++ b/services/production/common/models/ticket-state.js @@ -4,6 +4,5 @@ module.exports = function(TicketState) { var models = app.models; // Methods - require('../methods/ticket-state/change-state.js')(TicketState); }; \ No newline at end of file diff --git a/services/production/package.json b/services/production/package.json index 0c2dfa970..efe774be9 100644 --- a/services/production/package.json +++ b/services/production/package.json @@ -16,6 +16,7 @@ "loopback-boot": "^2.24.0", "loopback-component-explorer": "^4.2.0", "loopback-connector-mysql": "^3.0.0", + "loopback-connector-remote": "^3.1.1", "loopback-context": "^3.1.0", "serve-favicon": "^2.0.1", "strong-error-handler": "^2.1.0" diff --git a/services/production/server/model-config.json b/services/production/server/model-config.json index edfa734b5..8c99a47b4 100644 --- a/services/production/server/model-config.json +++ b/services/production/server/model-config.json @@ -56,5 +56,9 @@ "Warehouse":{ "dataSource": "vn", "public": true + }, + "Employee":{ + "dataSource": "client", + "public": true } } diff --git a/services/service/models/my-model.js b/services/service/models/my-model.js index 9246e02a9..f6dfad090 100644 --- a/services/service/models/my-model.js +++ b/services/service/models/my-model.js @@ -1,6 +1,3 @@ -let loopBackContext = require('loopback-context'); -let app = require('../../server/server'); - module.exports = function(self) { self.setup = function() { @@ -29,25 +26,40 @@ module.exports = function(self) { //this.disableRemoteMethod(method, disableMethods[method]); } }; - - self.getUser = function() { - let currentUser = loopBackContext.getCurrentContext(); - let userId = currentUser.get('currentUser'); - return userId; - }; - - self.getEmployee = function() { - let userId = self.getUser(); - let employee = app.models.Employee; - return employee.findOne({where: {userFk: userId}}); - }; - self.rawSql = function(query, params, cb) { this.dataSource.connector.execute(query, params, function(error, response) { cb(error, response); }); }; + self.remoteMethodCtx = function(methodName, args) { + if(args.accepts !== undefined && Array.isArray(args.accepts)) { + var ctx = { + arg: 'context', + type: 'object', + http: function(ctx) { + return ctx; + } + } + args.accepts.unshift(ctx); + } + this.remoteMethod(methodName, args); + }; + + self.connectToService = function(ctx, dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer(ctx.req.accessToken.id).toString('base64'), + sendImmediately: true + }; + }; + + self.disconnectFromService = function(dataSource) { + this.app.dataSources[dataSource].connector.remotes.auth = { + bearer: new Buffer("").toString('base64'), + sendImmediately: true + }; + }; + self.installMethod = function(methodName, filterCb) { this.remoteMethod(methodName, { description: 'List items using a filter',