Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 41 lines (31 sloc) 1.188 kb
c7544ff @visnup automate increasing team limits
visnup authored
1 mongoose = require 'mongoose'
2
3 TeamLimitSchema = module.exports = new mongoose.Schema
4 limit:
5 type: Number
6 required: true
7 effectiveAt:
8 type: Date
9 required: true
10 TeamLimitSchema.plugin require('../lib/use-timestamps')
11 TeamLimitSchema.index effectiveAt: -1
12
13 # class methods
14
15 lastLimit = null
16 TeamLimitSchema.static 'current', (next) ->
17 now = new Date
18
19 # only check every hour, unless we're near 0:00/12:00 UTC
20 if lastLimit?.updatedAt > now - (1000*60*60) and not @aroundMeridian(now)
21 next null, lastLimit.limit
22 else
99e9099 @visnup make sure not to get an empty team limit
visnup authored
23 latest = { effectiveAt: { $lte: now }, limit: { $ne: null } }
24 TeamLimit.findOne latest, {}, { sort: [[ 'effectiveAt', -1 ]] }, (err, limit) ->
c7544ff @visnup automate increasing team limits
visnup authored
25 return next err if err
26
27 limit ||= new TeamLimit limit: 0, effectiveAt: now # default to 0
28 limit.updatedAt = now
29 limit.save()
30
31 lastLimit = limit
32
33 next null, limit.limit
34
35 TeamLimitSchema.static 'aroundMeridian', (now) ->
36 [h, m] = [now.getUTCHours(), now.getUTCMinutes()]
37 (h == 11 && m > 54) || (h == 12 && m < 5) || # 11:55 - 12:05
38 (h == 23 && m > 54) || (h == 0 && m < 5) # 23:55 - 00:05
39
40 TeamLimit = mongoose.model 'TeamLimit', TeamLimitSchema
Something went wrong with that request. Please try again.