Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 146 lines (131 sloc) 4.002 kB
969876a start directing to team creation
Visnu Pitiyanuvath authored
1 _ = require 'underscore'
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
2 mongoose = require 'mongoose'
dd5e11c team creator can always edit it [finishes #15746035]
Visnu Pitiyanuvath authored
3 rbytes = require 'rbytes'
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
4
3a40131 invites saved
Visnu Pitiyanuvath authored
5 InviteSchema = require './invite'
c2fac11 load deploys on teams/show
Visnu Pitiyanuvath authored
6 [Invite, Person, Deploy, Vote] = (mongoose.model m for m in ['Invite', 'Person', 'Deploy', 'Vote'])
3a40131 invites saved
Visnu Pitiyanuvath authored
7
8 TeamSchema = module.exports = new mongoose.Schema
f48d333 prep for slugs. #116
Visnu Pitiyanuvath authored
9 slug:
10 type: String
cb489c2 unique index on team slug
Visnu Pitiyanuvath authored
11 unique: true
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
12 name:
13 type: String
3a40131 invites saved
Visnu Pitiyanuvath authored
14 required: true
bbcc7c0 @gerad unique team name validation
gerad authored
15 unique: true
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
16 description: String
1264899 @gerad form for editing team project #135
gerad authored
17 project:
18 name: String
19 description: String
20 instructions: String
21 colophon: String
22 votable:
23 type: Boolean
24 default: true
25 technical: Boolean
3a40131 invites saved
Visnu Pitiyanuvath authored
26 emails:
27 type: [ mongoose.SchemaTypes.Email ]
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
28 validate: [ ((v) -> v.length <= 4), 'max' ]
3a40131 invites saved
Visnu Pitiyanuvath authored
29 invites: [ InviteSchema ]
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
30 peopleIds:
3a40131 invites saved
Visnu Pitiyanuvath authored
31 type: [ mongoose.Schema.ObjectId ]
32 index: true
9663b70 denormalize the last deploy onto team. closes #58.
Visnu Pitiyanuvath authored
33 lastDeploy: {}
dd5e11c team creator can always edit it [finishes #15746035]
Visnu Pitiyanuvath authored
34 code:
35 type: String
36 default: -> rbytes.randomBytes(12).toString('base64')
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
37 search: String
d81de42 include timestamps
Visnu Pitiyanuvath authored
38 TeamSchema.plugin require('mongoose-types').useTimestamps
4d8605a include description on team index
Visnu Pitiyanuvath authored
39 TeamSchema.index updatedAt: -1
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
40
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
41 # class methods
3096ba4 use team slugs in urls. closes #116.
Visnu Pitiyanuvath authored
42 TeamSchema.static 'findBySlug', (slug, rest...) ->
43 Team.findOne { slug: slug }, rest...
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
44 TeamSchema.static 'canRegister', (next) ->
280c941 cut off team registration
Visnu Pitiyanuvath authored
45 return next null, false, 0 # cut off team registration
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
46 Team.count {}, (err, count) ->
47 return next err if err
48 max = 330 + 1 # +1 because team fortnight labs doesn't count
49 next null, count < max, max - count
50 TeamSchema.static 'uniqueName', (name, next) ->
51 Team.count { name: name }, (err, count) ->
52 return next err if err
53 next null, !count
54
55 # instance methods
3096ba4 use team slugs in urls. closes #116.
Visnu Pitiyanuvath authored
56 TeamSchema.method 'toString', -> @slug or @id
0f5b918 team code can be used even when you're logged in [#15746035]
Visnu Pitiyanuvath authored
57 TeamSchema.method 'includes', (person, code) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
58 @code == code or person and _.any @peopleIds, (id) -> id.equals(person.id)
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
59 TeamSchema.method 'invited', (invite) ->
60 _.detect @invites, (i) -> i.code == invite
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
61
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
62 # associations
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
63 TeamSchema.method 'people', (next) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
64 Person.find _id: { '$in': @peopleIds }, next
872b253 @dannycoates added /teams/:code/deploys
dannycoates authored
65 TeamSchema.method 'deploys', (next) ->
66 Deploy.find teamId: @id, next
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
67 TeamSchema.method 'votes', (next) ->
68 Vote.find teamId: @id, next
33b3281 accepting invites
Visnu Pitiyanuvath authored
69
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
70 # validations
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
71
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
72 ## min people
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
73 TeamSchema.pre 'save', (next) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
74 if @peopleIds.length + @emails.length == 0
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
75 error = new mongoose.Document.ValidationError this
76 error.errors.emails = 'min'
77 next error
78 else
79 next()
80
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
81 ## max teams
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
82 TeamSchema.pre 'save', (next) ->
6d1d845 can't update a team when teams are maxed out
Visnu Pitiyanuvath authored
83 return next() unless @isNew
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
84 Team.canRegister (err, yeah) =>
85 return next err if err
86 if yeah
87 next()
88 else
89 error = new mongoose.Document.ValidationError this
90 error.errors._base = 'max'
91 next error
92
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
93 ## unique name
bbcc7c0 @gerad unique team name validation
gerad authored
94 TeamSchema.pre 'save', (next) ->
95 return next() unless @isNew
96 Team.uniqueName @name, (err, yeah) =>
97 return next err if err
98 if yeah
99 next()
100 else
101 error = new mongoose.Document.ValidationError this
102 error.errors.name = 'unique'
103 next error
104
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
105 # callbacks
106
107 ## create invites
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
108 TeamSchema.pre 'save', (next) ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
109 for email in @emails
110 unless _.detect(@invites, (i) -> i.email == email)
111 @invites.push new Invite(email: email)
112 _.invoke @invites, 'send'
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
113 next()
5b83583 terser coffee-script
Visnu Pitiyanuvath authored
114 TeamSchema.post 'save', ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
115 for invite in @invites
116 invite.remove() unless !invite or _.include(@emails, invite.email)
117 @save() if @isModified 'invites'
969876a start directing to team creation
Visnu Pitiyanuvath authored
118
217341d remove contestants when they get removed from a team. closes #142.
Visnu Pitiyanuvath authored
119 ## remove team members
120 TeamSchema.path('peopleIds').set (v) ->
121 v.init = @peopleIds
122 v
123 TeamSchema.pre 'save', (next) ->
124 toString = (i) -> i.toString()
125 o = @peopleIds.init.map toString
126 n = @peopleIds.map toString
127 Person.remove role: 'contestant', _id: { $in: _.difference(o, n) }, next
128 TeamSchema.pre 'remove', (next) ->
129 Person.remove role: 'contestant', _id: { $in: @peopleIds }, next
130
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
131 ## search index
132 TeamSchema.pre 'save', (next) ->
bc72ef2 index login names too
Visnu Pitiyanuvath authored
133 only = name: 1, location: 1, 'github.login': 1, 'twit.screenName': 1
134 Person.find _id: { '$in': @peopleIds }, only, (err, people) =>
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
135 return next err if err
136 @search =
137 """
138 #{@name}
139 #{@description}
bc72ef2 index login names too
Visnu Pitiyanuvath authored
140 #{_.pluck(people, 'login').join(';')}
141 #{_.pluck(people, 'location').join(';')}
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
142 """
143 next()
144
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
145 Team = mongoose.model 'Team', TeamSchema
Something went wrong with that request. Please try again.