Corrected statistic logic, wrote test case

This commit is contained in:
Ruben Vereecken 2014-06-28 17:44:07 +02:00
parent bb1c07570d
commit a367082cc4
6 changed files with 54 additions and 13 deletions

View file

@ -22,6 +22,7 @@ PatchSchema = c.object({title:'Patch', required:['target', 'delta', 'commitMessa
})
_wasPending: type: 'boolean'
_newlyAccepted: type: 'boolean'
})
c.extendBasicProperties(PatchSchema, 'patch')

View file

@ -1,7 +1,6 @@
CocoView = require 'views/kinds/CocoView'
template = require 'templates/editor/delta'
deltasLib = require 'lib/deltas'
window.delta = deltasLib
TEXTDIFF_OPTIONS =
baseTextName: "Old"

View file

@ -54,11 +54,15 @@ PatchSchema.methods.isMiscPatch = ->
expanded = deltas.flattenDelta @get('delta')
_.some expanded, (delta) -> 'i18n' not in delta.dataPath
# Keep track of when a patch is pending. Accepted patches can be rejected still.
# Keep track of when a patch is pending and newly approved.
PatchSchema.path('status').set (newVal) ->
@set '_wasPending', @status is 'pending' and newVal isnt 'pending'
@set '_newlyAccepted', newVal is 'accepted' and not @get('_newlyAccepted') # Only true on the first accept
newVal
PatchSchema.methods.isNewlyAccepted = -> @get('_newlyAccepted')
PatchSchema.methods.wasPending = -> @get '_wasPending'
PatchSchema.pre 'save', (next) ->
User = require '../users/User'
userID = @get('creator').toHexString()

View file

@ -49,13 +49,23 @@ PatchHandler = class PatchHandler extends Handler
if newStatus is 'withdrawn'
return @sendUnauthorizedError(res) unless req.user.get('_id').equals patch.get('creator')
# newly accepted
if newStatus is 'accepted' and patch.get '_wasPending'
patch.set 'status', newStatus
# Only increment statistics upon very first accept
if patch.isNewlyAccepted()
accepter = req.user.get 'id'
User.incrementStat accepter, 'stats.'
submitter = patch.get 'creator'
User.incrementStat accepter, 'stats.patchesAccepted'
# TODO maybe merge these increments together
if patch.isTranslationPatch()
User.incrementStat submitter, 'stats.totalTranslationPatches'
User.incrementStat submitter, User.statsMapping.translations[targetModel.modelName]
if patch.isMiscPatch()
User.incrementStat submitter, 'stats.totalMiscPatches'
User.incrementStat submitter, User.statsMapping.misc[targetModel.modelName]
# these require callbacks
patch.set 'status', newStatus
patch.save (err) =>
log.error err if err?
target.update {$pull:{patches:patch.get('_id')}}, {}, ->

View file

@ -115,6 +115,19 @@ UserSchema.statics.statsMapping =
'level.component': 'stats.levelComponentEdits'
'level.system': 'stats.levelSystemEdits'
'thang.type': 'stats.thangTypeEdits'
translations:
article: 'stats.articleTranslationPatches'
level: 'stats.levelTranslationPatches'
'level.component': 'stats.levelComponentTranslationPatches'
'level.system': 'stats.levelSystemTranslationPatches'
'thang.type': 'stats.thangTypeTranslationPatches'
misc:
article: 'stats.articleMiscPatches'
level: 'stats.levelMiscPatches'
'level.component': 'stats.levelComponentMiscPatches'
'level.system': 'stats.levelSystemMiscPatches'
'thang.type': 'stats.thangTypeMiscPatches'
UserSchema.statics.incrementStat = (id, statName, done, inc=1) ->
update = $inc: {}

View file

@ -40,7 +40,7 @@ describe '/db/patch', ->
expect(body.creator).toBe(joe.id)
patches[0] = body
done()
it 'adds a patch to the target document', (done) ->
Article.findOne({}).exec (err, article) ->
expect(article.toObject().patches[0]).toBeDefined()
@ -110,10 +110,24 @@ describe '/db/patch', ->
expect(article.get('status')).toBe 'accepted'
done()
it 'keeps track of amount of submitted and accepted patches', (done) ->
loginJoe (joe) ->
User.findById joe.get('id'), (err, guy) ->
expect(err).toBeNull()
expect(guy.get 'stats.patchesSubmitted').toBe 1
expect(guy.get 'stats.patchesContributed').toBe 1
expect(guy.get 'stats.totalMiscPatches').toBe 1
expect(guy.get 'stats.articleMiscPatches').toBe 1
expect(guy.get 'stats.totalTranslationPatches').toBeUndefined()
done()
it 'does not allow the recipient to withdraw the pull request', (done) ->
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
request.put {uri: statusURL, json: {status:'withdrawn'}}, (err, res, body) ->
expect(res.statusCode).toBe(403)
Patch.findOne({}).exec (err, article) ->
expect(article.get('status')).toBe 'accepted'
done()
loginAdmin ->
statusURL = getURL("/db/patch/#{patches[0]._id}/status")
request.put {uri: statusURL, json: {status:'withdrawn'}}, (err, res, body) ->
expect(res.statusCode).toBe(403)
Patch.findOne({}).exec (err, article) ->
expect(article.get('status')).toBe 'accepted'
done()