Created
February 23, 2014 06:07
-
-
Save fastcodecoq/9167705 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
| var mongoose = require("mongoose"); | |
| var conf = require('../config.json'); | |
| mongoose.connect('mongodb://localhost/' + conf.db_name); | |
| var Schema = mongoose.Schema; | |
| var bcrypt = require('bcrypt'); | |
| var SALT_WORK_FACTOR = 10; | |
| var UserSchema = new Schema( | |
| { | |
| name: { type : String, required : true}, | |
| id: String, | |
| city: { type: String, lowercase: true, trim: true }, | |
| country: { type: String, lowercase: true, trim: true }, | |
| password: { type : String, required : true}, | |
| posts: String, | |
| email: { type : String, required : true, unique: true }, | |
| profile_pic: String, | |
| auth_login: String, | |
| billing : { type: Schema.Types.ObjectId, ref: 'billing' }, | |
| contact : { tel: String , address: String, cel: String, bussines: String}, | |
| date : { type: Date, default: Date.now }, | |
| verified : { type: Boolean, default: false}, | |
| loged : {type: Boolean, default: false}, | |
| sessions : [], | |
| notifications : {type: Boolean, default: false} | |
| } | |
| ); | |
| UserSchema.pre('save', function(next) { | |
| var user = this; | |
| if (!user.isModified('password')) return next(); | |
| bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { | |
| if (err) return next(err); | |
| bcrypt.hash(user.password, salt, function(err, hash) { | |
| if (err) return next(err); | |
| user.password = hash; | |
| bcrypt.hash(user.password + user.email, salt, function(err, hashs){ | |
| if (err) return next(err); | |
| user.auth_login = hashs; | |
| next(); | |
| }); | |
| }); | |
| }); | |
| }); | |
| UserSchema.methods.comparePassword = function(candidatePassword, cb) { | |
| bcrypt.compare(candidatePassword, this.password, function(err, isMatch) { | |
| if (err) return cb(err, false); | |
| cb(null, isMatch); | |
| }); | |
| }; | |
| UserSchema.methods.login = function(auth_login, cb) { | |
| bcrypt.compare(auth_login, this.auth_login, function(err, isMatch) { | |
| if (err) return cb(err); | |
| cb(null, isMatch); | |
| }); | |
| }; | |
| var estateSchema = new Schema({ | |
| name : { type: String, lowercase: true, trim: true }, | |
| owner : [{ type: Schema.Types.ObjectId, ref: 'users' }], | |
| date : { type: Date, default: Date.now }, | |
| city : { type: String, lowercase: true, trim: true }, | |
| country : { type: String, lowercase: true, trim: true }, | |
| coordinates : { lat : String , lng : String}, | |
| die : {type: Number , default : 30} | |
| }); | |
| var PostTypeSchema = new Schema({ | |
| name : { type: String, lowercase: true, trim: true }, | |
| price : Number, | |
| countries : { type: String, lowercase: true, trim: true }, | |
| marker: {type: String, default: conf.free_marker}, | |
| verified: {type: Boolean, default: false} | |
| }); | |
| var PostSchema = new Schema({ | |
| type : { type: String, lowercase: true, trim: true }, | |
| promoved : {type:Boolean, default:false}, | |
| promoved_type : { type: Schema.Types.ObjectId, ref: 'Promoved' }, | |
| photos : [String], | |
| video: String, | |
| coordinates : { lat : String , lng : String}, | |
| location : { address : String, state : String }, | |
| city : { type: String, lowercase: true, trim: true }, | |
| country : { type: String, lowercase: true, trim: true }, | |
| barrio: { type: String, lowercase: true, trim: true }, | |
| price: Number, | |
| title: { type: String, lowercase: true, trim: true }, | |
| contact_number: String, | |
| description: String, | |
| rooms: Number, | |
| levels: Number, | |
| area: Number, | |
| views: Number, | |
| to : { type: String, lowercase: true, trim: true }, | |
| IsEstate : { type: Boolean , default: false }, | |
| autor: [{ type: Schema.Types.ObjectId, ref: 'users' }], | |
| date: { type: Date, default: Date.now }, | |
| active: {type: Boolean, default: true}, | |
| comments : [{ user : { type : Schema.Types.ObjectId , ref : 'users'} , text : String , date : { type : Date , default : Date.now } }] | |
| }); | |
| var PromovedSchema = new Schema({ | |
| type : [{type:Schema.Types.ObjectId, ref: 'PostType'}], | |
| date : { type: Date, default: Date.now } | |
| }); | |
| var BillingSchema = new Schema({ | |
| credit : Number, | |
| transactions : [{ type: Schema.Types.ObjectId, ref: 'transactions' }], | |
| balance : Number, | |
| owner : { type: Schema.Types.ObjectId, ref: 'users' } | |
| }); | |
| var transactionsSchema = new Schema({ | |
| type : String, | |
| ammount : Number, | |
| date: { type: Date, default: Date.now }, | |
| user: [{ type: Schema.Types.ObjectId, ref: 'users' }] | |
| }); | |
| var zoneSchema = new Schema({ | |
| polygon : [], | |
| city : {type:String, lowercase: true, trim: true}, | |
| country : {type:String, lowercase: true, trim: true}, | |
| name : {type:String, trim: true}, | |
| user : { type: Schema.Types.ObjectId, ref: 'users' }, | |
| filters : [] | |
| }); | |
| var notificationSchema = new Schema({ | |
| user : { type: Schema.Types.ObjectId, ref: 'users' }, | |
| date : { type : Date, default : Date.now }, | |
| msg : String, | |
| url : String, | |
| checked : {type:Boolean, default: false} | |
| }); | |
| function models(){ | |
| this.transaction = mongoose.model('transactions', transactionsSchema); | |
| this.billing = mongoose.model('billing', BillingSchema); | |
| this.post = mongoose.model('post', PostSchema); | |
| this.PostType = mongoose.model('PostTypes', PostTypeSchema); | |
| this.estate = mongoose.model('estates' , estateSchema); | |
| this.user = mongoose.model('users', UserSchema); | |
| this.zones = mongoose.model('zones', zoneSchema); | |
| this.zones = mongoose.model('zones', zoneSchema); | |
| this.notifications = mongoose.model('notifications', notificationSchema); | |
| this.mongo = mongoose; | |
| } | |
| this.init = function(){ | |
| return new models(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment