Skip to content

Instantly share code, notes, and snippets.

// This function is boilerplate for replicating how Amazon Kinesis events are
// passed to Lambda for processing.
//
// payloadString - The String to package
//
// Returns an Object as the data would be passed to Lambda
function packageEvent(payloadString) {
var encodedPayload = new Buffer(payloadString).toString('base64')
return {
"Records": [{
exports.firebase = function (req, res) {
if(req.user) {
var generator = new FirebaseTokenGenerator(process.env.FIREBASE_SECRET);
var token = generator.createToken({uid: req.user.id});
res.send(token);
} else {
res.send(401, {error: "You must be logged in to perform this operation"});
}
};
function authenticateFirebase(token) {
// firebaseUrl is poopulated by gulp via config
var firebaseUrl = 'https://amber-inferno-2148.firebaseio.com/';
var myFirebaseRef = new Firebase(firebaseUrl);
myFirebaseRef.authWithCustomToken(token, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
// This API method returns a limited number of objects based on the limit, but
// it relies on the query parameters to define the pagination filtes (currently
// managed in Angular)
//
// Because it has to make three separate queries, we make use of Mongoose-Q to
// manage the promises in a way that is performant and readable
UserService.prototype.find = function(params, callback) {
// First we parse the parameters
var query = buildUserQuery(params);
// Then we will create a return object to which the functions below can add.
var returnObject = {};
// As we define each database query and its processing logic, the return value
getNextFewUsers = User
.find(query)
.where('deleted').ne(true)
.sort(sort)
.limit(limit)
.populate('company')
// And boom goes the dynamite.
Q.all([
getNextFewUsers,
getConsultantCount,
getCompanyCount
])
.then(function() {
callback(null, returnObject)
})
.catch(function(err) {
UserService.prototype.find = function (params, callback) {
/*
Need:
- sort
- search
*/
var query = {},
sort = params.sort || 'displayName',
options = {
'sort': (params.descending ? '-' : '') + sort
UserService.prototype.saveUser = function(user, newUser, callback) {
user.save(function(err) {
if (err) {
log(err);
callback(err, user);
} else {
searchly.indexConsultant(user, function(err, data) {
if (err) log(err);
if (newUser) {
processNewUser(user, callback);
UserSchema.methods.toJSON = function () {
var obj = this.toObject();
delete obj.password;
return obj;
};
UserSchema.methods.toCompleteJSON = function () {
var obj = this.toObject();
delete obj.password;
obj.isNewUser = this.isNewUser()
// This post-save hook middleware allows us to publish create and update events
// to which the mediator service can subscribe.
UserSchema.post('save', function (doc) {
// Determine the current event
var eventName = (this.wasNew) ? "user:create" : "user:update";
if (doc.deleted) {eventName = 'user:delete'}
// Create an authenticate a Firebase ref and update accordingly
var firebaseRef, userRef;