Skip to content
Newer
Older
100644 122 lines (108 sloc) 3.22 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
11 index: 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
3a40131 invites saved
Visnu Pitiyanuvath authored
17 emails:
18 type: [ mongoose.SchemaTypes.Email ]
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
19 validate: [ ((v) -> v.length <= 4), 'max' ]
3a40131 invites saved
Visnu Pitiyanuvath authored
20 invites: [ InviteSchema ]
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
21 peopleIds:
3a40131 invites saved
Visnu Pitiyanuvath authored
22 type: [ mongoose.Schema.ObjectId ]
23 index: true
9663b70 denormalize the last deploy onto team. closes #58.
Visnu Pitiyanuvath authored
24 lastDeploy: {}
dd5e11c team creator can always edit it [finishes #15746035]
Visnu Pitiyanuvath authored
25 code:
26 type: String
27 default: -> rbytes.randomBytes(12).toString('base64')
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
28 search: String
d81de42 include timestamps
Visnu Pitiyanuvath authored
29 TeamSchema.plugin require('mongoose-types').useTimestamps
4d8605a include description on team index
Visnu Pitiyanuvath authored
30 TeamSchema.index updatedAt: -1
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
31
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
32 # class methods
33 TeamSchema.static 'canRegister', (next) ->
280c941 cut off team registration
Visnu Pitiyanuvath authored
34 return next null, false, 0 # cut off team registration
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
35 Team.count {}, (err, count) ->
36 return next err if err
37 max = 330 + 1 # +1 because team fortnight labs doesn't count
38 next null, count < max, max - count
39 TeamSchema.static 'uniqueName', (name, next) ->
40 Team.count { name: name }, (err, count) ->
41 return next err if err
42 next null, !count
43
44 # instance methods
0f5b918 team code can be used even when you're logged in [#15746035]
Visnu Pitiyanuvath authored
45 TeamSchema.method 'includes', (person, code) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
46 @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
47 TeamSchema.method 'invited', (invite) ->
48 _.detect @invites, (i) -> i.code == invite
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
49
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
50 # associations
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
51 TeamSchema.method 'people', (next) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
52 Person.find _id: { '$in': @peopleIds }, next
872b253 @dannycoates added /teams/:code/deploys
dannycoates authored
53 TeamSchema.method 'deploys', (next) ->
54 Deploy.find teamId: @id, next
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
55 TeamSchema.method 'votes', (next) ->
56 Vote.find teamId: @id, next
33b3281 accepting invites
Visnu Pitiyanuvath authored
57
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
58 # validations
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
59
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
60 ## min people
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
61 TeamSchema.pre 'save', (next) ->
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
62 if @peopleIds.length + @emails.length == 0
6bf7f73 require at least one email or person [finishes #15748057]
Visnu Pitiyanuvath authored
63 error = new mongoose.Document.ValidationError this
64 error.errors.emails = 'min'
65 next error
66 else
67 next()
68
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
69 ## max teams
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
70 TeamSchema.pre 'save', (next) ->
6d1d845 can't update a team when teams are maxed out
Visnu Pitiyanuvath authored
71 return next() unless @isNew
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
72 Team.canRegister (err, yeah) =>
73 return next err if err
74 if yeah
75 next()
76 else
77 error = new mongoose.Document.ValidationError this
78 error.errors._base = 'max'
79 next error
80
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
81 ## unique name
bbcc7c0 @gerad unique team name validation
gerad authored
82 TeamSchema.pre 'save', (next) ->
83 return next() unless @isNew
84 Team.uniqueName @name, (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.name = 'unique'
91 next error
92
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
93 # callbacks
94
95 ## create invites
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
96 TeamSchema.pre 'save', (next) ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
97 for email in @emails
98 unless _.detect(@invites, (i) -> i.email == email)
99 @invites.push new Invite(email: email)
100 _.invoke @invites, 'send'
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
101 next()
5b83583 terser coffee-script
Visnu Pitiyanuvath authored
102 TeamSchema.post 'save', ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
103 for invite in @invites
104 invite.remove() unless !invite or _.include(@emails, invite.email)
105 @save() if @isModified 'invites'
969876a start directing to team creation
Visnu Pitiyanuvath authored
106
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
107 ## search index
108 TeamSchema.pre 'save', (next) ->
bc72ef2 index login names too
Visnu Pitiyanuvath authored
109 only = name: 1, location: 1, 'github.login': 1, 'twit.screenName': 1
110 Person.find _id: { '$in': @peopleIds }, only, (err, people) =>
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
111 return next err if err
112 @search =
113 """
114 #{@name}
115 #{@description}
bc72ef2 index login names too
Visnu Pitiyanuvath authored
116 #{_.pluck(people, 'login').join(';')}
117 #{_.pluck(people, 'location').join(';')}
a712da5 searchable teams. needs migration. closes #42.
Visnu Pitiyanuvath authored
118 """
119 next()
120
7bcb422 stop team registration at 200 [finishes #15726989]
Visnu Pitiyanuvath authored
121 Team = mongoose.model 'Team', TeamSchema
Something went wrong with that request. Please try again.