Created
March 1, 2018 17:18
-
-
Save aguileraq/5ffbc0cbad8b76de8df18a312f51c49f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Model = require('../models/model'); | |
let Restaurante = require('../models/schema_restaurante'); | |
let Admin = require('../models/schema_admin'); | |
// Moment js | |
var moment = require('moment'); | |
// Working Hours | |
var WorkingHours = require('working-hours').WorkingHours; | |
// Random String | |
var randomstring = require("randomstring"); | |
var bcrypt = require('bcryptjs'); | |
//multer | |
var multer = require('multer'); | |
var path = require('path'); | |
var storage = multer.diskStorage({ | |
destination: function(req, file, callback) { | |
callback(null, './public/img/') | |
}, | |
filename: function(req, file, callback) { | |
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)) | |
} | |
}); | |
exports.doRestauranteNuevo = function(req, res) { | |
var upload = multer({ | |
storage: storage, | |
fileFilter: function(req, file, callback) { | |
var ext = path.extname(file.originalname) | |
if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') { | |
//return callback(res.end('Only images are allowed'), null) | |
return callback(console.log('Only images are allowed'), null) | |
} | |
callback(null, true) | |
} | |
}).single('logo_imagen'); | |
/*upload(req, res, function(err) { | |
res.end('File is uploaded') | |
})*/ | |
upload(req, res, function(err) { | |
req.checkBody('nombre', 'Ingresa el nombre completo del restaurante').isLength({ min: 5 }); | |
req.checkBody('eslogan', 'Ingresa el eslogan completo del restaurante').isLength({ min: 5 }); | |
req.checkBody('informacion', 'Ingresa el concepto del restaurante').isLength({ min: 5 }); | |
req.checkBody('correo_de_pedidos', 'Ingresa el correo electrónico para recibir los pedidos').isEmail(); | |
req.checkBody('servicios', 'Selecciona por lo menos una opción').notEmpty(); | |
req.checkBody('direccion[calle]', 'Escribe la calle donde se ubica el restaurante').isLength({min:10}); | |
req.checkBody('direccion[numero]', 'Escribe el número donde se ubica tu restaurante').isLength({min:2}); | |
req.checkBody('direccion[colonia]', 'Escribe la colonia donde se ubica el restaurante').isLength({min:5}); | |
req.checkBody('direccion[cp]', 'Escribe el codigo postal de la zona donde se encuentra el restaurante').isPostalCode('MX'); | |
req.checkBody('direccion[ciudad]', 'Escribe la ciudad donde se ubica el restaurante').isLength({min:5}); | |
req.checkBody('direccion[estado]', 'Escribe el estado postal donde se ubica el restaurante').isLength({min:5}); | |
req.checkBody('direccion[telefono1]', 'Escribe el número de teléfono del restaurante').isLength({min:6}).isNumeric(); | |
req.checkBody('direccion[telefono2]', 'Escribe el número de teléfono del restaurante').optional({ checkFalsy: true }).isLength({min:6}).isNumeric(); | |
req.checkBody('administrador[nombre]', 'Escribe el nombre del administrador del restaurante').isLength({min:6}); | |
req.checkBody('administrador[email]', 'Ingresa el correo electrónico del administrador').isEmail(); | |
req.checkBody('administrador[celular]', 'Ingresa el número celular del administrador').isLength({min:8}).isNumeric(); | |
req.checkBody('segundo_administrador[nombre]', 'Escribe el nombre de la segunda persona a cargo del restaurante').isLength({min:6}); | |
req.checkBody('segundo_administrador[celular]', 'Ingresa el número celular de la segunda persona a cargo').isLength({min:8}).isNumeric();; | |
req.checkBody('propietario[nombre]', 'Escribe el nombre del propietario del restaurante').isLength({min:6}); | |
req.checkBody('propietario[celular]', 'Ingresa el número celular del propietario').isLength({min:8}).isNumeric(); | |
req.checkBody('entrega[nombre_repartidor]', 'Escribe el nombre del repartidor').isLength({min:6}).notEmpty(); | |
req.checkBody('entrega[medio_transporte]', 'Selecciona el medio de transporte del repartidor').notEmpty(); | |
req.checkBody('bancarios[banco]', 'Escribe el nombre del banco').isLength({min:4}); | |
req.checkBody('bancarios[rfc]', 'Escribe el RFC completo').isLength({min:12}); | |
req.checkBody('bancarios[cuentahabiente]', 'Escribe el nombre del cuentahabiente').isLength({min:4}); | |
req.checkBody('bancarios[clabe]', 'Escribe la CLABE del banco').isLength({min:6}); | |
req.checkBody('bancarios[correo_facturacion]', 'Escribe el nombre del banco').isEmail(); | |
let errors = req.validationErrors(); | |
/*if(errors){ | |
console.log(errors); | |
}*/ | |
if(err){ | |
console.log(err); | |
}else if(errors){ | |
console.log(errors); | |
let obj = { | |
'type':'error', | |
'data':errors | |
}; | |
res.send(JSON.stringify(obj)); | |
} | |
else{ | |
let restaurante = new Restaurante(req.body); | |
restaurante.id = Math.random().toString(26).slice(2); | |
restaurante.username = req.body.nombre.replace(/[^A-Z0-9]/ig, "").toLowerCase(); | |
restaurante.password = "123456"; | |
console.log(restaurante); | |
bcrypt.genSalt(10, function(err, salt){ | |
bcrypt.hash(restaurante.password, salt, function(err, hash){ | |
if(err){ | |
console.log(err); | |
} | |
restaurante.password = hash; | |
restaurante.save(function(err,dat){ | |
if(err){ | |
console.log(err); | |
return; | |
}else{ | |
console.log(dat._id); | |
Admin.findOneAndUpdate({ _id: req.user._id }, {$push: {restaurantes: dat._id}}, {new: true}, function(err, doc){ | |
if(err){ | |
console.log("Something wrong when updating data!"); | |
} | |
console.log(doc); | |
let obj = { | |
'type':'success', | |
'data': 'Registro exitoso.', | |
'title': 'Alta de Restaurante', | |
'redirect': '/' | |
}; | |
res.send(JSON.stringify(obj)); | |
}); | |
} | |
}); | |
}); | |
}); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment