list ticket fake production

This commit is contained in:
nelo 2017-06-21 13:29:01 +02:00
parent 05b6c81a1b
commit 26e1e9a71b
7 changed files with 228 additions and 213 deletions

View File

@ -1,28 +1,26 @@
module.exports = function(self) {
self.setup = function() {
self.super_.setup.call(this);
let disableMethods = {
'create': true,
'replaceOrCreate': true,
'patchOrCreate': true,
'upsert': true,
'updateOrCreate': true,
'exists': true,
'find': true,
'findOne': true,
'findById': true,
'deleteById': true,
'replaceById': true,
'updateAttributes': false,
'createChangeStream': true,
'updateAll': true,
'upsertWithWhere': true,
'count': true
create: true,
replaceOrCreate: true,
patchOrCreate: true,
upsert: true,
updateOrCreate: true,
exists: true,
find: true,
findOne: true,
findById: true,
deleteById: true,
replaceById: true,
updateAttributes: false,
createChangeStream: true,
updateAll: true,
upsertWithWhere: true,
count: true
};
for(let method in disableMethods) {
for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]);
}
};
@ -31,12 +29,12 @@ module.exports = function(self) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
returns: {
type: [this.modelName],
root: true
},
@ -47,14 +45,14 @@ module.exports = function(self) {
});
this.list = function(ctx, clientFilter, cb) {
var where = {"and": []};
var where = {and: []};
(clientFilter) ? where.and.push(clientFilter.where) : undefined;
(serverFilter) ? where.and.push(serverFilter.where) : undefined;
var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined;
var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined;
var filter = {"where": where, "order": order, "limit": limit};
var filter = {where: where, order: order, limit: limit};
filter = removeEmpty(filter);
this.find(filter, function(err, states) {
@ -70,15 +68,15 @@ module.exports = function(self) {
};
self.remoteMethodCtx = function(methodName, args) {
if(args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
if (args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}
args.accepts.unshift(ctx);
};
args.accepts.unshift(ctx);
}
this.remoteMethod(methodName, args);
};
@ -113,7 +111,7 @@ module.exports = function(self) {
}
],
returns: {
arg: 'data',
arg: 'data',
type: [this.modelName],
root: true
},
@ -125,65 +123,56 @@ module.exports = function(self) {
this[methodName] = (params, cb) => {
let filter = removeEmpty(filterCb(params));
var response = {}
var response = {};
function returnValues(){
if(response.instances !== undefined && response.count !== undefined)
function returnValues() {
if (response.instances !== undefined && response.count !== undefined)
cb(null, response);
}
function error(){
function error() {
cb(null, response);
}
this.find(filter, function(err, instances) {
if(!err){
if (err) {
error();
} else {
response.instances = instances;
returnValues();
}
else{
error();
}
})
});
this.count(filter.where, function(err, totalCount){
if(!err){
if (err) {
error();
} else {
response.count = totalCount;
returnValues();
}
else{
error();
}
})
});
};
};
}
};
function removeEmpty(o) {
if(Array.isArray(o)) {
if (Array.isArray(o)) {
let array = [];
for(let item of o) {
for (let item of o) {
let i = removeEmpty(item);
if(!isEmpty(item))
if (!isEmpty(item))
array.push(item);
};
if(array.length > 0)
return array;
}
else if (typeof o === 'object') {
let object = {};
for(let key in o) {
let i = removeEmpty(o[key]);
if(!isEmpty(i))
object[key] = i;
}
if(Object.keys(object).length > 0)
if (array.length > 0)
return array;
} else if (typeof o === 'object') {
let object = {};
for (let key in o) {
let i = removeEmpty(o[key]);
if (!isEmpty(i))
object[key] = i;
}
if (Object.keys(object).length > 0)
return object;
}
else if (!isEmpty(o))
} else if (!isEmpty(o))
return o;
return undefined;

View File

@ -0,0 +1,44 @@
[
{
"relation": "ticket",
"scope": {
"fields": ["id"]
}
},
{
"relation": "state",
"scope": {
"fields": ["id", "name"]
}
},
{
"relation": "agency",
"scope": {
"fields": ["id", "name"]
}
},
{
"relation": "province",
"scope": {
"fields": ["id", "name"]
}
},
{
"relation": "client",
"scope": {
"fields": ["id", "name"]
}
},
{
"relation": "worker",
"scope": {
"fields": ["id", "name", "surname"]
}
},
{
"relation": "salesPerson",
"scope": {
"fields": ["id", "name", "surname"]
}
}
]

View File

@ -5,11 +5,12 @@ module.exports = function(FakeProduction) {
FakeProduction.list = function(ctx, filter, cb){
FakeProduction.connectToService(ctx, "client");
this.find(filter, function(err, tickets) {
(!err) ? cb(null, tickets) : cb(err, null);
});
filter.include = require('./filter.json');
FakeProduction.disconnectFromService("client");
this.find(filter, function(err, tickets) {
FakeProduction.disconnectFromService("client");
(err) ? cb(err, null) : cb(null, tickets);
});
}
}

View File

@ -10,6 +10,9 @@
},
"name":{
"type": "String"
},
"surname":{
"type": "String"
}
},
"acls": []

View File

@ -58,10 +58,10 @@
"model": "Employee",
"foreignKey": "workerFk"
},
"salePerson": {
"salesPerson": {
"type": "belongsTo",
"model": "Employee",
"foreignKey": "salePersonFk"
"foreignKey": "salesPersonFk"
}
},
"acls": [

View File

@ -1,28 +1,26 @@
module.exports = function(self) {
self.setup = function() {
self.super_.setup.call(this);
let disableMethods = {
'create': true,
'replaceOrCreate': true,
'patchOrCreate': true,
'upsert': true,
'updateOrCreate': true,
'exists': true,
'find': true,
'findOne': true,
'findById': true,
'deleteById': true,
'replaceById': true,
'updateAttributes': false,
'createChangeStream': true,
'updateAll': true,
'upsertWithWhere': true,
'count': true
create: true,
replaceOrCreate: true,
patchOrCreate: true,
upsert: true,
updateOrCreate: true,
exists: true,
find: true,
findOne: true,
findById: true,
deleteById: true,
replaceById: true,
updateAttributes: false,
createChangeStream: true,
updateAll: true,
upsertWithWhere: true,
count: true
};
for(let method in disableMethods) {
for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]);
}
};
@ -31,12 +29,12 @@ module.exports = function(self) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
returns: {
type: [this.modelName],
root: true
},
@ -47,14 +45,14 @@ module.exports = function(self) {
});
this.list = function(ctx, clientFilter, cb) {
var where = {"and": []};
var where = {and: []};
(clientFilter) ? where.and.push(clientFilter.where) : undefined;
(serverFilter) ? where.and.push(serverFilter.where) : undefined;
var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined;
var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined;
var filter = {"where": where, "order": order, "limit": limit};
var filter = {where: where, order: order, limit: limit};
filter = removeEmpty(filter);
this.find(filter, function(err, states) {
@ -70,15 +68,15 @@ module.exports = function(self) {
};
self.remoteMethodCtx = function(methodName, args) {
if(args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
if (args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}
args.accepts.unshift(ctx);
};
args.accepts.unshift(ctx);
}
this.remoteMethod(methodName, args);
};
@ -113,7 +111,7 @@ module.exports = function(self) {
}
],
returns: {
arg: 'data',
arg: 'data',
type: [this.modelName],
root: true
},
@ -125,65 +123,56 @@ module.exports = function(self) {
this[methodName] = (params, cb) => {
let filter = removeEmpty(filterCb(params));
var response = {}
var response = {};
function returnValues(){
if(response.instances !== undefined && response.count !== undefined)
function returnValues() {
if (response.instances !== undefined && response.count !== undefined)
cb(null, response);
}
function error(){
function error() {
cb(null, response);
}
this.find(filter, function(err, instances) {
if(!err){
if (err) {
error();
} else {
response.instances = instances;
returnValues();
}
else{
error();
}
})
});
this.count(filter.where, function(err, totalCount){
if(!err){
if (err) {
error();
} else {
response.count = totalCount;
returnValues();
}
else{
error();
}
})
});
};
};
}
};
function removeEmpty(o) {
if(Array.isArray(o)) {
if (Array.isArray(o)) {
let array = [];
for(let item of o) {
for (let item of o) {
let i = removeEmpty(item);
if(!isEmpty(item))
if (!isEmpty(item))
array.push(item);
};
if(array.length > 0)
return array;
}
else if (typeof o === 'object') {
let object = {};
for(let key in o) {
let i = removeEmpty(o[key]);
if(!isEmpty(i))
object[key] = i;
}
if(Object.keys(object).length > 0)
if (array.length > 0)
return array;
} else if (typeof o === 'object') {
let object = {};
for (let key in o) {
let i = removeEmpty(o[key]);
if (!isEmpty(i))
object[key] = i;
}
if (Object.keys(object).length > 0)
return object;
}
else if (!isEmpty(o))
} else if (!isEmpty(o))
return o;
return undefined;

View File

@ -1,28 +1,26 @@
module.exports = function(self) {
self.setup = function() {
self.super_.setup.call(this);
let disableMethods = {
'create': true,
'replaceOrCreate': true,
'patchOrCreate': true,
'upsert': true,
'updateOrCreate': true,
'exists': true,
'find': true,
'findOne': true,
'findById': true,
'deleteById': true,
'replaceById': true,
'updateAttributes': false,
'createChangeStream': true,
'updateAll': true,
'upsertWithWhere': true,
'count': true
create: true,
replaceOrCreate: true,
patchOrCreate: true,
upsert: true,
updateOrCreate: true,
exists: true,
find: true,
findOne: true,
findById: true,
deleteById: true,
replaceById: true,
updateAttributes: false,
createChangeStream: true,
updateAll: true,
upsertWithWhere: true,
count: true
};
for(let method in disableMethods) {
for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]);
}
};
@ -31,12 +29,12 @@ module.exports = function(self) {
this.remoteMethodCtx('list', {
accepts: [
{
arg: 'filter',
type: 'object',
arg: 'filter',
type: 'object',
description: 'Filter defining where'
}
],
returns: {
returns: {
type: [this.modelName],
root: true
},
@ -47,14 +45,14 @@ module.exports = function(self) {
});
this.list = function(ctx, clientFilter, cb) {
var where = {"and": []};
var where = {and: []};
(clientFilter) ? where.and.push(clientFilter.where) : undefined;
(serverFilter) ? where.and.push(serverFilter.where) : undefined;
var order = (clientFilter && clientFilter.order) ? clientFilter.order : (serverFilter && serverFilter.order) ? serverFilter.order : undefined;
var limit = (serverFilter && serverFilter.limit) ? serverFilter.limit : (clientFilter && clientFilter.limit) ? clientFilter.limit : undefined;
var filter = {"where": where, "order": order, "limit": limit};
var filter = {where: where, order: order, limit: limit};
filter = removeEmpty(filter);
this.find(filter, function(err, states) {
@ -70,15 +68,15 @@ module.exports = function(self) {
};
self.remoteMethodCtx = function(methodName, args) {
if(args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
if (args.accepts !== undefined && Array.isArray(args.accepts)) {
var ctx = {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}
args.accepts.unshift(ctx);
};
args.accepts.unshift(ctx);
}
this.remoteMethod(methodName, args);
};
@ -113,7 +111,7 @@ module.exports = function(self) {
}
],
returns: {
arg: 'data',
arg: 'data',
type: [this.modelName],
root: true
},
@ -125,65 +123,56 @@ module.exports = function(self) {
this[methodName] = (params, cb) => {
let filter = removeEmpty(filterCb(params));
var response = {}
var response = {};
function returnValues(){
if(response.instances !== undefined && response.count !== undefined)
function returnValues() {
if (response.instances !== undefined && response.count !== undefined)
cb(null, response);
}
function error(){
function error() {
cb(null, response);
}
this.find(filter, function(err, instances) {
if(!err){
if (err) {
error();
} else {
response.instances = instances;
returnValues();
}
else{
error();
}
})
});
this.count(filter.where, function(err, totalCount){
if(!err){
if (err) {
error();
} else {
response.count = totalCount;
returnValues();
}
else{
error();
}
})
});
};
};
}
};
function removeEmpty(o) {
if(Array.isArray(o)) {
if (Array.isArray(o)) {
let array = [];
for(let item of o) {
for (let item of o) {
let i = removeEmpty(item);
if(!isEmpty(item))
if (!isEmpty(item))
array.push(item);
};
if(array.length > 0)
return array;
}
else if (typeof o === 'object') {
let object = {};
for(let key in o) {
let i = removeEmpty(o[key]);
if(!isEmpty(i))
object[key] = i;
}
if(Object.keys(object).length > 0)
if (array.length > 0)
return array;
} else if (typeof o === 'object') {
let object = {};
for (let key in o) {
let i = removeEmpty(o[key]);
if (!isEmpty(i))
object[key] = i;
}
if (Object.keys(object).length > 0)
return object;
}
else if (!isEmpty(o))
} else if (!isEmpty(o))
return o;
return undefined;