2014-05-18 09:04:54 -04:00
|
|
|
Achievement = require './Achievement'
|
|
|
|
Handler = require '../commons/Handler'
|
|
|
|
|
|
|
|
class AchievementHandler extends Handler
|
|
|
|
modelClass: Achievement
|
|
|
|
|
2014-05-18 10:04:50 -04:00
|
|
|
# Used to determine which properties requests may edit
|
2014-10-27 20:11:48 -04:00
|
|
|
editableProperties: [
|
|
|
|
'name'
|
|
|
|
'query'
|
|
|
|
'worth'
|
|
|
|
'collection'
|
|
|
|
'description'
|
|
|
|
'userField'
|
|
|
|
'proportionalTo'
|
|
|
|
'icon'
|
|
|
|
'function'
|
|
|
|
'related'
|
|
|
|
'difficulty'
|
|
|
|
'category'
|
|
|
|
'rewards'
|
|
|
|
'i18n'
|
|
|
|
'i18nCoverage'
|
|
|
|
]
|
2015-02-25 21:41:39 -05:00
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
allowedMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
|
2014-05-18 09:04:54 -04:00
|
|
|
jsonSchema = require '../../app/schemas/models/achievement.coffee'
|
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
|
2014-05-18 10:04:50 -04:00
|
|
|
hasAccess: (req) ->
|
2015-02-25 21:41:39 -05:00
|
|
|
req.method in ['GET', 'PUT'] or req.user?.isAdmin() or req.user?.isArtisan()
|
2014-10-27 20:11:48 -04:00
|
|
|
|
|
|
|
hasAccessToDocument: (req, document, method=null) ->
|
|
|
|
method = (method or req.method).toLowerCase()
|
|
|
|
return true if method is 'get'
|
2015-02-25 21:41:39 -05:00
|
|
|
return true if req.user?.isAdmin() or req.user?.isArtisan()
|
2014-10-27 20:11:48 -04:00
|
|
|
return true if method is 'put' and @isJustFillingTranslations(req, document)
|
|
|
|
return
|
2014-05-18 10:04:50 -04:00
|
|
|
|
2014-08-04 16:21:43 -04:00
|
|
|
get: (req, res) ->
|
|
|
|
# /db/achievement?related=<ID>
|
|
|
|
if req.query.related
|
2014-09-19 05:26:18 -04:00
|
|
|
return @sendForbiddenError(res) if not @hasAccess(req)
|
2014-08-04 16:21:43 -04:00
|
|
|
Achievement.find {related: req.query.related}, (err, docs) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
docs = (@formatEntity(req, doc) for doc in docs)
|
|
|
|
@sendSuccess res, docs
|
|
|
|
else
|
|
|
|
super req, res
|
|
|
|
|
2014-08-08 11:14:57 -04:00
|
|
|
delete: (req, res, slugOrID) ->
|
2015-02-25 21:41:39 -05:00
|
|
|
return @sendForbiddenError res unless req.user?.isAdmin() or req.user?.isArtisan()
|
2014-08-08 11:14:57 -04:00
|
|
|
@getDocumentForIdOrSlug slugOrID, (err, document) => # Check first
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless document?
|
|
|
|
document.remove (err, document) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
@sendNoContent res
|
|
|
|
|
2015-03-28 16:54:44 -04:00
|
|
|
getNamesByIDs: (req, res) -> @getNamesByOriginals req, res, true
|
|
|
|
|
2014-05-19 18:05:21 -04:00
|
|
|
module.exports = new AchievementHandler()
|