secret
Created

  • Download Gist
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/**
* Client - CRUD/filter/
*
*/
module.exports = flow = {
'moduleName':'client',
'obj': null,
'model': null,
'db' : function(){
// The contact embedded doc allows multiple
// contact details
var contact = new this.obj.Schema({
key: { type: String, required: true }, // Form of contact
value: { type: String, required: true } // Contact detail
},{strict: true});
 
// The assistant embedded doc allows multiple
// assistants and multiple contact details
// per assistant
var assistant = new this.obj.Schema({
name: { type: String, required: true }, // Name of assistant
key: { type: String, required: true }, // Form of contact
value: { type: String, required: true } // Contact detail
},{strict: true});
 
var clientSchema = new this.obj.Schema({
firstname: { type: String, required: true, trim: true }, // The first name of the client
lastname: { type: String, required: true, trim: true }, // The second name of the client
password: { type: String, trim: true, index: true, required: true }, // The password of the client
tier: { type: Number, required: true, index: true, trim: true }, // The tier of the client
level: [{ type: Number, trim: true }], // Array of levelIDs associated with client
contact: [contact], // The contact details associated with the client
assistant: [assistant], // The details of the client's assistant
jobTitle: { type: String, trim: true }, // The client's job title
accountIds: [{ type: Number, trim: true, index: true}], // The account IDs (usually 1) associated with the client
approved: { type: Boolean, index: true, default:'0' }, // The approval details associated with the client
approvedOn: { type: Number, trim: true },
isActive: { type: Number, required: true, index: true, 'default': 1 } // Whether the client is visible or not
},{strict: true});
 
clientSchema.plugin(this.obj.useTimestamps);
clientSchema.pre('save',function(next){
this.timeApproved = new Date(this.timeApproved*1000).toISOString();
console.log(this);
next();
});
 
flow.model = this.obj.mongoose.model(this.moduleName,clientSchema);
},
 
'routes' : function(){
// Standard Controllers
var standard = Object.create(this.obj.standardControllers).init(),
// Middleware that modifies what's exposed
// to the Controller.
middleware = this.obj.routing;
 
// GET /
this.obj.express.get('/'+this.moduleName,middleware.getParams,standard.list);
// GET /:id
this.obj.express.get('/'+this.moduleName+'/:id',middleware.getParams,standard.show);
 
// POST /
this.obj.express.post('/'+this.moduleName,standard.create);
 
// POST /search
this.obj.express.post('/'+this.moduleName+'/search',middleware.getParams,standard.search);
// POST /:id OR PUT /:id
this.obj.express.post('/'+this.moduleName+'/:id',standard.update);
this.obj.express.put('/'+this.moduleName+'/:id',standard.update);
 
// DELETE /:id
this.obj.express.del('/'+this.moduleName+'/:id',standard.remove);
this.obj.express.post('/'+this.moduleName+'/delete/:id',standard.remove);
 
return this;
},
'init': function(obj){
this.obj = obj;
this.routes();
this.db();
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.