2014-12-16 20:46:24 -05:00
|
|
|
Campaign = require './Campaign'
|
|
|
|
Level = require '../levels/Level'
|
|
|
|
Achievement = require '../achievements/Achievement'
|
|
|
|
Handler = require '../commons/Handler'
|
|
|
|
async = require 'async'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
|
|
|
|
CampaignHandler = class CampaignHandler extends Handler
|
|
|
|
modelClass: Campaign
|
|
|
|
editableProperties: [
|
|
|
|
'name'
|
2015-01-29 12:07:25 -05:00
|
|
|
'fullName'
|
2015-03-18 19:20:30 -04:00
|
|
|
'description'
|
2015-11-12 13:25:05 -05:00
|
|
|
'type'
|
2014-12-16 20:46:24 -05:00
|
|
|
'i18n'
|
|
|
|
'i18nCoverage'
|
|
|
|
'ambientSound'
|
|
|
|
'backgroundImage'
|
|
|
|
'backgroundColor'
|
|
|
|
'backgroundColorTransparent'
|
|
|
|
'adjacentCampaigns'
|
|
|
|
'levels'
|
|
|
|
]
|
|
|
|
jsonSchema: require '../../app/schemas/models/campaign.schema'
|
|
|
|
|
|
|
|
hasAccess: (req) ->
|
2015-02-13 18:04:51 -05:00
|
|
|
req.method in ['GET', 'PUT'] or req.user?.isAdmin()
|
|
|
|
|
|
|
|
hasAccessToDocument: (req, document, method=null) ->
|
|
|
|
return true if req.user?.isAdmin()
|
|
|
|
|
|
|
|
if @modelClass.schema.uses_coco_translation_coverage and (method or req.method).toLowerCase() in ['post', 'put']
|
|
|
|
return true if @isJustFillingTranslations(req, document)
|
2015-03-18 19:20:30 -04:00
|
|
|
|
2015-02-13 18:04:51 -05:00
|
|
|
if req.method is 'GET'
|
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
2014-12-16 20:46:24 -05:00
|
|
|
|
2015-02-05 18:05:22 -05:00
|
|
|
get: (req, res) ->
|
|
|
|
return @sendForbiddenError(res) if not @hasAccess(req)
|
|
|
|
# We don't have normal text search or anything set up to make /db/campaign work, so we'll just give them all campaigns, no problem.
|
2015-10-22 09:42:42 -04:00
|
|
|
query = {}
|
|
|
|
projection = {}
|
|
|
|
if @modelClass.schema.uses_coco_translation_coverage and req.query.view is 'i18n-coverage'
|
|
|
|
query = i18nCoverage: {$exists: true}
|
|
|
|
if req.query.project
|
|
|
|
projection[field] = 1 for field in req.query.project.split(',')
|
|
|
|
q = @modelClass.find query, projection
|
2015-02-05 18:05:22 -05:00
|
|
|
q.exec (err, documents) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
documents = (@formatEntity(req, doc) for doc in documents)
|
|
|
|
@sendSuccess(res, documents)
|
|
|
|
|
2015-11-17 14:15:56 -05:00
|
|
|
getOverview: (req, res) ->
|
|
|
|
return @sendForbiddenError(res) if not @hasAccess(req)
|
|
|
|
q = @modelClass.find {}, slug: 1, adjacentCampaigns: 1, fullName: 1, description: 1, color: 1
|
|
|
|
q.exec (err, documents) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
formatCampaign = (doc) =>
|
|
|
|
obj = @formatEntity(req, doc)
|
|
|
|
obj.adjacentCampaigns = _.map(obj.adjacentCampaigns, (a) -> _.pick(a, ['showIfUnlocked', 'color', 'name', 'description' ]))
|
|
|
|
obj
|
|
|
|
documents = (formatCampaign(doc) for doc in documents)
|
|
|
|
@sendSuccess(res, documents)
|
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
getByRelationship: (req, res, args...) ->
|
|
|
|
relationship = args[1]
|
2015-11-17 14:15:56 -05:00
|
|
|
return @getOverview(req,res) if args[0] is '-' and relationship is 'overview'
|
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
if relationship in ['levels', 'achievements']
|
|
|
|
projection = {}
|
|
|
|
if req.query.project
|
|
|
|
projection[field] = 1 for field in req.query.project.split(',')
|
|
|
|
@getDocumentForIdOrSlug args[0], (err, campaign) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless campaign?
|
|
|
|
return @getRelatedLevels(req, res, campaign, projection) if relationship is 'levels'
|
|
|
|
return @getRelatedAchievements(req, res, campaign, projection) if relationship is 'achievements'
|
|
|
|
else
|
|
|
|
super(arguments...)
|
2014-12-28 16:25:20 -05:00
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
getRelatedLevels: (req, res, campaign, projection) ->
|
2014-12-28 16:25:20 -05:00
|
|
|
extraProjectionProps = []
|
2015-01-06 19:59:56 -05:00
|
|
|
unless _.isEmpty(projection)
|
2014-12-28 16:25:20 -05:00
|
|
|
# Make sure that permissions and version are fetched, but not sent back if they didn't ask for them.
|
|
|
|
extraProjectionProps.push 'permissions' unless projection.permissions
|
|
|
|
extraProjectionProps.push 'version' unless projection.version
|
|
|
|
projection.permissions = 1
|
|
|
|
projection.version = 1
|
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
levels = campaign.get('levels') or []
|
2014-12-28 16:25:20 -05:00
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
f = (levelOriginal) ->
|
|
|
|
(callback) ->
|
|
|
|
query = { original: mongoose.Types.ObjectId(levelOriginal) }
|
|
|
|
sort = { 'version.major': -1, 'version.minor': -1 }
|
|
|
|
Level.findOne(query, projection).sort(sort).exec callback
|
2014-12-28 16:25:20 -05:00
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
fetches = (f(level.original) for level in _.values(levels))
|
|
|
|
async.parallel fetches, (err, levels) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
2014-12-28 16:25:20 -05:00
|
|
|
filteredLevels = (_.omit(level.toObject(), extraProjectionProps) for level in levels)
|
|
|
|
return @sendSuccess(res, filteredLevels)
|
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
getRelatedAchievements: (req, res, campaign, projection) ->
|
|
|
|
levels = campaign.get('levels') or []
|
|
|
|
|
|
|
|
f = (levelOriginal) ->
|
|
|
|
(callback) ->
|
|
|
|
query = { related: levelOriginal }
|
|
|
|
Achievement.find(query, projection).exec callback
|
|
|
|
|
|
|
|
fetches = (f(level.original) for level in _.values(levels))
|
|
|
|
async.parallel fetches, (err, achievementses) =>
|
|
|
|
achievements = _.flatten(achievementses)
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendSuccess(res, (achievement.toObject() for achievement in achievements))
|
|
|
|
|
2014-12-28 16:25:20 -05:00
|
|
|
onPutSuccess: (req, doc) ->
|
|
|
|
docLink = "http://codecombat.com#{req.headers['x-current-path']}"
|
|
|
|
@sendChangedHipChatMessage creator: req.user, target: doc, docLink: docLink
|
|
|
|
|
2015-03-28 16:54:44 -04:00
|
|
|
getNamesByIDs: (req, res) -> @getNamesByOriginals req, res, true
|
|
|
|
|
2014-12-16 20:46:24 -05:00
|
|
|
module.exports = new CampaignHandler()
|