Skip to content

HTTPS clone URL

Subversion checkout URL

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