Skip to content

Instantly share code, notes, and snippets.

@volodymyrlut
Created May 2, 2015 13:55
Show Gist options
  • Save volodymyrlut/974d8945d75a52f75773 to your computer and use it in GitHub Desktop.
Save volodymyrlut/974d8945d75a52f75773 to your computer and use it in GitHub Desktop.
var Mongoose = require('mongoose');
var Crypto = require('crypto');
var Constants = require('../util/Constants');
var Config = require('../util/Config');
//Mongoose.connect(Config.get('mongo:url'));
//Below portion of code is required to support MongoDB connection on both - local and AppFog environments
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
}
else{
var mongo = {
"hostname":Config.get('mongo:server'),
"port":27017,
"username":"",
"password":"",
"name":"",
"db":Config.get('mongo:db')
}
}
var generate_mongo_url = function(obj){
obj.hostname = (obj.hostname || 'localhost');
obj.port = (obj.port || 27017);
obj.db = (obj.db || 'test');
if(obj.username && obj.password){
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
else{
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}
var mongourl = generate_mongo_url(mongo);
Mongoose.connect(mongourl);
//Mongoose.connect('mongodb://localhost/tap2win');
var db = Mongoose.connection;
/*db.on('error', function (err) {
log.error('connection error:', err.message);
});
db.once('open', function callback () {
log.info("Connected to DB!");
});*/
var Schema = Mongoose.Schema;
// Page Schema
var FBConsumer = new Schema({
id :{
type: String,
required: true
},
fname :{
type: String,
required: true
},
gender :{
type: String
},
locale:{
type: String
},
age:{
type: String
},
occupation:{
type: String
},
email:{
type: String,
required: true
}
});
var FBConsumerModel = Mongoose.model('FBConsumer', FBConsumer);
var Pages = new Schema({
type: {
type: String,
enum: [Constants.PAGE_TYPE_WELCOME,
Constants.PAGE_TYPE_THANK_YOU,
Constants.PAGE_TYPE_INACTIVE],
required: true
},
imageName: {
type: String
},
text: {
type: String
},
videoLink: {
type: String
}
});
// SocialNetwork Schema
var SocialNetworks = new Schema({
name: {
type: String,
enum: [Constants.SOCIAL_NETWORK_FACEBOOK],
required: true
},
pageLink: {
type: String,
required: true
},
action: {
type: String,
enum: [Constants.SOCIAL_NETWORK_ACTION_LIKE],
required: true
}
});
// Configuration Schema
var Configuration = new Schema({
name: {
text: {
type: String,
required: true,
unique: true
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
}
}
},
description: {
text: {
type: String
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
}
}
},
subdescription: {
text: {
type: String
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
},
}
},
icons: [{
text: {
type: String
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
}
},
styleImage: {
"background-image": {
type: String
},
"height": {
type: String
},
"width": {
type: String
}
}
},
{
text: {
type: String
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
}
},
styleImage: {
"background-image": {
type: String
},
"height": {
type: String
},
"width": {
type: String
}
}
},
{
text: {
type: String
},
style: {
"color": {
type: String
},
"font-size": {
type: String
},
"font-weight": {
type: String
}
},
styleImage: {
"background-image": {
type: String
},
"height": {
type: String
},
"width": {
type: String
}
}
}
],
status: {
type: String,
enum: [Constants.CONFIGURATION_STATUS_CREATED,
Constants.CONFIGURATION_STATUS_STARTED,
Constants.CONFIGURATION_STATUS_PAUSED,
Constants.CONFIGURATION_STATUS_STOPPED,
Constants.CONFIGURATION_STATUS_DELETED],
required: true
},
loyaltyProgramID: {
type: String,
required: true
},
loyaltyProgramName: {
type: String,
required: true
},
pages: [Pages],
surveyID: {
type: String
},
surveyName: {
type: String
},
socialNetworks: [SocialNetworks],
askLocation: {
type: Boolean,
required: true
},
loyaltyRecordType: {
type: String,
enum: [Constants.CRM_RECORD_TYPE_1],
required: true
}
});
var ConfigurationModel = Mongoose.model('Configuration', Configuration);
// User Schema
var User = new Schema({
username: {
type: String,
unique: true,
required: true
},
hashedPassword: {
type: String,
required: true
},
salt: {
type: String,
required: true
},
created: {
type: Date,
default: Date.now
}
});
User.methods.encryptPassword = function(password) {
return Crypto.createHmac('sha1', this.salt).update(password).digest('hex');
//more secure - return Crypto.pbkdf2Sync(password, this.salt, 10000, 512);
};
User.virtual('userId')
.get(function () {
return this.id;
});
User.virtual('password')
.set(function(password) {
this._plainPassword = password;
this.salt = Crypto.randomBytes(32).toString('base64');
//more secure - this.salt = Crypto.randomBytes(128).toString('base64');
this.hashedPassword = this.encryptPassword(password);
})
.get(function() { return this._plainPassword; });
User.methods.checkPassword = function(password) {
return this.encryptPassword(password) === this.hashedPassword;
};
var UserModel = Mongoose.model('User', User);
// Client Schema
var Client = new Schema({
name: {
type: String,
unique: true,
required: true
},
clientId: {
type: String,
unique: true,
required: true
},
clientSecret: {
type: String,
required: true
}
});
var ClientModel = Mongoose.model('Client', Client);
// AccessToken Schema
var AccessToken = new Schema({
userId: {
type: String,
required: true
},
clientId: {
type: String,
required: true
},
token: {
type: String,
unique: true,
required: true
},
created: {
type: Date,
default: Date.now
}
});
var AccessTokenModel = Mongoose.model('AccessToken', AccessToken);
// RefreshToken Schema
var RefreshToken = new Schema({
userId: {
type: String,
required: true
},
clientId: {
type: String,
required: true
},
token: {
type: String,
unique: true,
required: true
},
created: {
type: Date,
default: Date.now
}
});
var RefreshTokenModel = Mongoose.model('RefreshToken', RefreshToken);
// Code Schema
var Code = new Schema({
uniqueCode: {
type: String,
unique: true,
required: true
},
fbPage: {
type: String,
required: true
}
});
var CodeModel = Mongoose.model('Code', Code);
module.exports.Mongoose = Mongoose;
module.exports.UserModel = UserModel;
module.exports.ClientModel = ClientModel;
module.exports.AccessTokenModel = AccessTokenModel;
module.exports.RefreshTokenModel = RefreshTokenModel;
module.exports.ConfigurationModel = ConfigurationModel;
module.exports.CodeModel = CodeModel;
module.exports.FBConsumerModel = FBConsumerModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment