'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Company Schema
*/
var CompanySchema = new Schema({
name: {
type: String,
default: '',
//required: 'Please fill Company name',
trim: true
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
address: {
type: String,
default: '',
//required: 'Please fill Company address',
trim: true
},
locked: {
type: Boolean,
default: true,
},
deleted: {
type: Boolean,
default: false,
},
logo: {
type: String,
default: '',
},
email: {
type: String,
default: '',
//required: 'Please fill Company email',
},
tel: {
type: String,
default: '',
//required: 'Please fill Company tel',
},
fax: {
type: String,
default: '',
//required: 'Please fill Company fax',
},
type: {
type: String,
//required: 'Please fill Company type',
trim: true
},
description: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
agents: [{
type: Schema.ObjectId,
ref: 'Agent'
}]
});
mongoose.model('Company', CompanySchema);
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Agent Schema
*/
var AgentSchema = new Schema({
// Agent model fields
// ...
firstname: {
type: String,
default: ''
},
lastname: {
type: String,
default: ''
},
email: {
type: String,
default: ''
},
password: {
type: String,
default: ''
},
roles: {
type: Array,
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
deleted: {
type: Boolean,
default: false
},
locked: {
type: Boolean,
default: false
},
company: {
type: Schema.ObjectId,
ref: 'Company'
}
});
var exports = module.exports = mongoose.model('Agent', AgentSchema);
var preapareAgents = function(req,company){
var admin = new Agent(req.body.admin);
admin.roles = ['admin', 'read','write'];
admin.company = company;
admin.locked = false;
var agents = req.body.agents;
for (var i = 0; i <= agents.length - 1; i++) {
agents[i].roles = [agents[i].role];
agents[i] = new Agent(agents[i]);
agents[i].company = company;
}
agents.push(admin);
return agents;
};
company.save(function(err){
if(err){
console.log('company err');
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}else{
console.log('company save');
var agents = preapareAgents(req, company);
Agent.create(agents, function(err){
if(err){
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}else{
res.jsonp({
company: company,
agents: agents
});
}
});
}
});
user: {
type: Schema.ObjectId,
ref: 'User'
},
agents: [{
type: Schema.ObjectId,
ref: 'Agent'
}]
}
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.