Introduced findBySlugOrId to fix a bug

This commit is contained in:
Ruben Vereecken 2014-08-14 16:38:50 +02:00
parent 0084b64d67
commit 5f7ef825f8
3 changed files with 11 additions and 3 deletions

3
server/lib/utils.coffee Normal file
View file

@ -0,0 +1,3 @@
module.exports =
isID: (id) -> _.isString(id) and id.length is 24 and id.match(/[a-f0-9]/gi)?.length is 24

View file

@ -1,6 +1,7 @@
mongoose = require('mongoose') mongoose = require('mongoose')
textSearch = require('mongoose-text-search') textSearch = require('mongoose-text-search')
log = require 'winston' log = require 'winston'
utils = require '../lib/utils'
module.exports.MigrationPlugin = (schema, migrations) -> module.exports.MigrationPlugin = (schema, migrations) ->
# Property name migrations made EZ # Property name migrations made EZ
@ -31,9 +32,13 @@ module.exports.NamedPlugin = (schema) ->
schema.add({name: String, slug: String}) schema.add({name: String, slug: String})
schema.index({'slug': 1}, {unique: true, sparse: true, name: 'slug index'}) schema.index({'slug': 1}, {unique: true, sparse: true, name: 'slug index'})
schema.statics.getBySlug = (slug, done) -> schema.statics.findBySlug = (slug, done) ->
@findOne {slug: slug}, done @findOne {slug: slug}, done
schema.statics.findBySlugOrId = (slugOrID, done) ->
return @findById slugOrID, done if utils.isID slugOrID
@findOne {slug: slugOrID}, done
schema.pre('save', (next) -> schema.pre('save', (next) ->
if schema.uses_coco_versions if schema.uses_coco_versions
v = @get('version') v = @get('version')

View file

@ -208,7 +208,7 @@ UserHandler = class UserHandler extends Handler
@sendSuccess(res, {result: 'success'}) @sendSuccess(res, {result: 'success'})
avatar: (req, res, id) -> avatar: (req, res, id) ->
@modelClass.findById(id).exec (err, document) => @modelClass.findBySlugOrId(id).exec (err, document) =>
return @sendDatabaseError(res, err) if err return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless document return @sendNotFoundError(res) unless document
photoURL = document?.get('photoURL') photoURL = document?.get('photoURL')
@ -232,7 +232,7 @@ UserHandler = class UserHandler extends Handler
IDify: (idOrSlug, done) -> IDify: (idOrSlug, done) ->
return done null, idOrSlug if Handler.isID idOrSlug return done null, idOrSlug if Handler.isID idOrSlug
User.getBySlug idOrSlug, (err, user) -> done err, user?.get '_id' User.findBySlug idOrSlug, (err, user) -> done err, user?.get '_id'
getLevelSessions: (req, res, userIDOrSlug) -> getLevelSessions: (req, res, userIDOrSlug) ->
@IDify userIDOrSlug, (err, userID) => @IDify userIDOrSlug, (err, userID) =>