llamamos al servicio client para obtener el empleado

cambiamos el estado
This commit is contained in:
nelo 2017-06-15 11:02:50 +02:00
parent f346e5e83c
commit b86f71bb25
8 changed files with 111 additions and 53 deletions

View File

@ -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',

View File

@ -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){

View File

@ -0,0 +1,13 @@
{
"name": "Employee",
"base": "MyModel",
"validateUpsert": true,
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
}
},
"acls": []
}

View File

@ -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',

View File

@ -4,6 +4,5 @@ module.exports = function(TicketState) {
var models = app.models;
// Methods
require('../methods/ticket-state/change-state.js')(TicketState);
};

View File

@ -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"

View File

@ -56,5 +56,9 @@
"Warehouse":{
"dataSource": "vn",
"public": true
},
"Employee":{
"dataSource": "client",
"public": true
}
}

View File

@ -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',