2016-03-30 16:57:19 -04:00
|
|
|
utils = require '../lib/utils'
|
|
|
|
errors = require '../commons/errors'
|
|
|
|
wrap = require 'co-express'
|
|
|
|
Promise = require 'bluebird'
|
|
|
|
database = require '../commons/database'
|
|
|
|
mongoose = require 'mongoose'
|
2016-04-06 13:56:06 -04:00
|
|
|
Campaign = require '../models/Campaign'
|
2016-03-30 16:57:19 -04:00
|
|
|
parse = require '../commons/parse'
|
2016-04-06 13:56:06 -04:00
|
|
|
LevelSession = require '../models/LevelSession'
|
2016-05-04 15:05:28 -04:00
|
|
|
Achievement = require '../models/Achievement'
|
|
|
|
Level = require '../models/Level'
|
2016-04-13 14:39:17 -04:00
|
|
|
slack = require '../slack'
|
2016-03-30 16:57:19 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
fetchByType: wrap (req, res, next) ->
|
|
|
|
type = req.query.type
|
|
|
|
return next() unless type
|
|
|
|
unless _.contains(Campaign.jsonSchema.properties.type.enum, type)
|
|
|
|
throw new errors.UnprocessableEntity('Bad campaign type')
|
|
|
|
dbq = Campaign.find { type: type }
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
campaigns = yield dbq.exec()
|
|
|
|
campaigns = (campaign.toObject({req: req}) for campaign in campaigns)
|
|
|
|
res.status(200).send(campaigns)
|
2016-04-13 14:39:17 -04:00
|
|
|
|
|
|
|
put: wrap (req, res) ->
|
|
|
|
campaign = yield database.getDocFromHandle(req, Campaign)
|
|
|
|
if not campaign
|
|
|
|
throw new errors.NotFound('Campaign not found.')
|
2016-04-13 12:54:24 -04:00
|
|
|
levelsBefore = _.keys(campaign.get('levels'))
|
2016-04-13 14:39:17 -04:00
|
|
|
hasPermission = req.user.isAdmin()
|
|
|
|
unless hasPermission or database.isJustFillingTranslations(req, campaign)
|
|
|
|
throw new errors.Forbidden('Must be an admin or submitting translations to edit a campaign')
|
|
|
|
|
|
|
|
database.assignBody(req, campaign)
|
|
|
|
database.validateDoc(campaign)
|
2016-04-13 12:54:24 -04:00
|
|
|
levelsAfter = _.keys(campaign.get('levels'))
|
|
|
|
if not _.isEqual(levelsBefore, levelsAfter)
|
|
|
|
campaign.set('levelsUpdated', new Date())
|
2016-04-13 14:39:17 -04:00
|
|
|
campaign = yield campaign.save()
|
|
|
|
res.status(200).send(campaign.toObject())
|
|
|
|
docLink = "http://codecombat.com#{req.headers['x-current-path']}"
|
|
|
|
slack.sendChangedSlackMessage creator: req.user, target: campaign, docLink: docLink
|
2016-05-04 15:05:28 -04:00
|
|
|
|
|
|
|
fetchRelatedAchievements: wrap (req, res) ->
|
|
|
|
campaign = yield database.getDocFromHandle(req, Campaign)
|
|
|
|
if not campaign
|
|
|
|
throw new errors.NotFound('Campaign not found.')
|
|
|
|
levels = campaign.get('levels') or []
|
|
|
|
project = parse.getProjectFromReq(req)
|
|
|
|
fetches = []
|
|
|
|
for level in _.values(levels)
|
|
|
|
fetches.push Achievement.find({ related: level.original }).select(project)
|
|
|
|
achievementses = yield fetches
|
|
|
|
achievements = _.flatten(achievementses)
|
|
|
|
res.status(200).send((achievement.toObject({req: req}) for achievement in achievements))
|
|
|
|
|
|
|
|
fetchRelatedLevels: wrap (req, res) ->
|
|
|
|
campaign = yield database.getDocFromHandle(req, Campaign)
|
|
|
|
if not campaign
|
|
|
|
throw new errors.NotFound('Campaign not found.')
|
|
|
|
levels = campaign.get('levels') or []
|
|
|
|
project = parse.getProjectFromReq(req)
|
|
|
|
sort = { 'version.major': -1, 'version.minor': -1 }
|
|
|
|
fetches = []
|
|
|
|
for level in _.values(levels)
|
|
|
|
query = { original: mongoose.Types.ObjectId(level.original) }
|
|
|
|
fetches.push Level.findOne(query).sort(sort).select(project)
|
|
|
|
levels = yield fetches
|
|
|
|
res.status(200).send((level.toObject({req: req}) for level in levels))
|
|
|
|
|
|
|
|
fetchOverworld: wrap (req, res) ->
|
|
|
|
project = parse.getProjectFromReq(req)
|
|
|
|
campaigns = yield Campaign.find({type: 'hero'}).select(project)
|
|
|
|
formatCampaign = (campaign) ->
|
|
|
|
campaign.toObject({req: req})
|
|
|
|
campaign.adjacentCampaigns = _.mapValues(campaign.adjacentCampaigns, (a) ->
|
|
|
|
_.pick(a, ['showIfUnlocked', 'color', 'name', 'description' ]))
|
|
|
|
for original, level of campaign.levels
|
|
|
|
campaign.levels[original] = _.pick level, ['locked', 'disabled', 'original', 'rewards', 'slug']
|
|
|
|
return campaign
|
2016-05-31 18:08:09 -04:00
|
|
|
res.status(200).send((formatCampaign(campaign) for campaign in campaigns))
|