🐛Fix course solutions level ordering

This commit is contained in:
Matt Lott 2016-08-11 22:26:47 -07:00
parent e91a15338e
commit 5a6bed96b2
2 changed files with 8 additions and 3 deletions

View file

@ -24,10 +24,15 @@ module.exports =
campaign = yield Campaign.findById course.get('campaignID')
throw new errors.NotFound('Campaign not found.') unless campaign
levelOriginals = (mongoose.Types.ObjectId(levelID) for levelID of campaign.get('levels'))
# TODO: why does campaign.get('levels') return opposite order from direct db query?
sortedLevelIDs = _.keys campaign.get('levels')
sortedLevelIDs.reverse()
levelOriginals = (mongoose.Types.ObjectId(levelID) for levelID in sortedLevelIDs)
query = { original: { $in: levelOriginals }, slug: { $exists: true }}
select = {documentation: 1, intro: 1, name: 1, slug: 1, thangs: 1}
select = {documentation: 1, intro: 1, name: 1, original: 1, slug: 1, thangs: 1}
levels = yield Level.find(query).select(select).lean()
levels.sort((a, b) -> sortedLevelIDs.indexOf(a.original + '') - sortedLevelIDs.indexOf(b.original + ''))
res.status(200).send(levels)
fetchNextLevel: wrap (req, res) ->

View file

@ -150,8 +150,8 @@ describe 'GET /db/course/:handle/level-solutions', ->
paredLevelB = _.pick(res.body, 'name', 'original', 'type')
campaignJSONA = { name: 'Campaign A', levels: {} }
campaignJSONA.levels[paredLevelA.original] = paredLevelA
campaignJSONA.levels[paredLevelB.original] = paredLevelB
campaignJSONA.levels[paredLevelA.original] = paredLevelA
[res, body] = yield request.postAsync({uri: getURL('/db/campaign'), json: campaignJSONA})
@campaignA = yield Campaign.findById(res.body._id)