Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
Fix #15 acrescentando validações e campo hospital_id
Browse files Browse the repository at this point in the history
Signed-off-by: Guilherme Deusdará <guibanci@gmail.com>
Signed-off-by: João Egewarth <egewarth@gmail.com>

Co-authored-by: João Egewarth <egewarth@gmail.com>
  • Loading branch information
gdeusdara and egewarth committed Jun 21, 2018
1 parent 59d99b8 commit 3546987
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
76 changes: 59 additions & 17 deletions Client/api.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
// The RegExp Object above validates MongoBD ObjectIds
var checkObjectId = new RegExp('^[0-9a-fA-F]{24}$');

function validate_field(field, result){
if (field.value == null || field.value == ''){
result[field.field_name + '_error'] = 'O campo ' + field.verbose + ' é obrigatório.';
} else if (typeof(field.value) != 'string') {
result[field.field_name + '_error'] = 'O campo ' + field.verbose +' deve ser uma string.';
}
return result;
}

function validate_id(field, result){
if (field.value == null || field.value == ''){
result[field.field_name + '_error'] = 'O campo ' + field.verbose + ' é obrigatório.';
} else if (!checkObjectId.test(field.value)) {
result[field.field_name + '_error'] = 'O ' + field.verbose +' é inválido.';
}
return result;
}

module.exports = function api(options){

this.add('role:api,path:create', function(msg,respond){
var name = {
verbose: 'Nome',
field_name: 'name'
}
var hospital_id = {
verbose: 'Id de Hospital',
field_name: 'hospital_id'
}
result = {}
name.value = msg.args.body.name
hospital_id.value = msg.args.body.hospital_id

var name = msg.args.body.name
var id = msg.args.query.id
result = validate_field(name, result)
result = validate_id(hospital_id, result)

this.act('role:sector,cmd:create',{
name:name,
id:id
}, respond)
if (Object.entries(result)[0]) {
console.log("Result:");
console.log(result);
result.success = false;
respond(null, result)
// else, everything sucess
} else {
this.act('role:sector,cmd:create', {
name: name.value,
hospital_id: hospital_id.value
}, respond)
}
})

this.add('role:api,path:listSector', function(msg,respond) {
this.act('role:sector, cmd:listSector',{
this.add('role:api,path:list', function(msg,respond) {
this.act('role:sector, cmd:list',{
},respond)
});

Expand All @@ -27,15 +67,17 @@ module.exports = function api(options){
pin: 'role:api,path:*',
map: {
create: { POST:true,
auth: {
strategy: 'jwt',
fail: '/api/sector/error',
}},
listSector: { GET:true,
auth: {
strategy: 'jwt',
fail: '/api/sector/error',
}},
// auth: {
// strategy: 'jwt',
// fail: '/api/sector/error',
// }
},
list: { GET:true,
// auth: {
// strategy: 'jwt',
// fail: '/api/sector/error',
// }
},
error: {GET:true}
}
}}, respond)
Expand Down
28 changes: 26 additions & 2 deletions Server/_sector.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
var Promise = require('bluebird');

module.exports = function(options){

this.add('role:sector,cmd:create', function create (msg,respond) {
this.add('role:sector,cmd:create', async function create (msg,respond) {
var sector = this.make('sectors')
sector.name = msg.name
sector.hospital_id = msg.hospital_id

var result = {success:false};

var sector_list$ = Promise.promisify(sector.list$, { context: sector });

await sector_list$(
{
name: sector.name,
hospital_id: sector.hospital_id
})
.then(await function(list_of_sectors){
if (list_of_sectors.length != 0){
result.duplicate_sector_error = 'Este setor já existe neste hospital'
respond(null, result)
}
})
.catch(function(err) {
console.log('error')
console.log(err)
})

sector.save$(function(err,sector){
respond(null,sector)
})
})

this.add('role:sector, cmd:listSector', function listSector(msg, respond){
this.add('role:sector, cmd:list', function list(msg, respond){
var sector = this.make('sectors');
sector.list$({all$:true}, function(error,sector){
respond(null,sector);
Expand Down
1 change: 1 addition & 0 deletions Server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "MIT",
"dependencies": {
"bcrypt": "2.0.1",
"bluebird": "^3.5.1",
"body-parser": "1.15.2",
"chai": "^4.1.2",
"express": "^4.14.0",
Expand Down

0 comments on commit 3546987

Please sign in to comment.