Last active
February 24, 2017 17:50
-
-
Save gsasouza/d57d7c4d9659109b8b6b846f7227b2ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Created by Gabriel Souza on 30/11/2016. | |
| */ | |
| 'use strict'; | |
| var mongoose = require('mongoose'), | |
| Schema = mongoose.Schema, | |
| fs = require('fs'), | |
| bCrypt = require('bcrypt-nodejs'), | |
| saltRounds = require('config').saltRounds, | |
| autoincrement = require('mongoose-sequence'); | |
| var clientModel = new Schema({ | |
| clientId:{ | |
| type: Number | |
| }, | |
| cnpj: { | |
| type: String, | |
| required: true, | |
| unique: [true, 'CNPJ is required'] | |
| }, | |
| name: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'name is required'] | |
| }, | |
| status: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'status is required'] | |
| }, | |
| password: { | |
| type: String, | |
| required: [true, 'password is required'] | |
| }, | |
| email: { | |
| type: String, | |
| required:[true, 'email is required'] | |
| }, | |
| contact: { | |
| type: String, | |
| required: [true, 'contact is required'] | |
| }, | |
| adress : { | |
| publicPlace: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'Adress\'s public place is required'] | |
| }, | |
| zip: { | |
| type: String, | |
| required: [true, 'Adress\'s ZIP code is required'] | |
| }, | |
| neighborhood: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'Adress\'s neighborhood is required'] | |
| }, | |
| number: { | |
| type: String, | |
| required: [true, 'Adress\'s number is required'] | |
| }, | |
| city: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'Adress\'s city is required'] | |
| }, | |
| uf: { | |
| type: String, | |
| uppercase: true, | |
| required: [true, 'Adress\'s uf is required'] | |
| } | |
| } | |
| }); | |
| clientModel.plugin(autoincrement, {inc_field: 'clientId'}); | |
| clientModel.methods.isValidPassword = function (password) { | |
| return bCrypt.compareSync(password, this.password); | |
| }; | |
| clientModel.methods.generatePassword = function(){ | |
| return Math.floor(Math.random()*(100000000-1000000+1)+1000000).toString(16); | |
| } | |
| clientModel.pre('save', function (next) { | |
| var self = this; | |
| var error = new Error(); | |
| if(!self.isModified('password')) return next(); | |
| bCrypt.genSalt(saltRounds, function(err, salt){ | |
| if(err) return next(error); | |
| bCrypt.hash(self.password, salt, null, function(err, hash){ | |
| if(err) return next(error); | |
| self.password = hash; | |
| next(); | |
| }); | |
| }); | |
| }); | |
| clientModel.pre('save', function (next) { | |
| for(var key in this){ | |
| if(typeof this[key] === 'string'){ | |
| if(key !== 'password' && key !== 'email'){ | |
| this[key] = this[key].toUpperCase(); | |
| } | |
| } | |
| } | |
| next(); | |
| }); | |
| clientModel.post('save', function(error, client, next){ | |
| var e = new Error(); | |
| if (error.name === 'MongoError' && error.code === 11000) { | |
| error.name = 'Already.Registered'; | |
| error.message = 'Company is already registered'; | |
| return next (error); | |
| } | |
| next(error); | |
| }); | |
| clientModel.post('save', function(error, client, next){ | |
| var dirName = './balanceSheets/' + this.clientId; | |
| console.log(this); | |
| fs.mkdir(dirName, function(err){ | |
| console.log(err); | |
| if(err) return next(err); | |
| }); | |
| }) | |
| module.exports = mongoose.model('Client', clientModel); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment