Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 116 lines (102 sloc) 3.08 kb
969876a Visnu Pitiyanuvath start directing to team creation
visnup authored
1 _ = require 'underscore'
2ffa112 Visnu Pitiyanuvath got teams to actually save
visnup authored
2 mongoose = require 'mongoose'
dd5e11c Visnu Pitiyanuvath team creator can always edit it [finishes #15746035]
visnup authored
3 rbytes = require 'rbytes'
2ffa112 Visnu Pitiyanuvath got teams to actually save
visnup authored
4
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
5 InviteSchema = require './invite'
c2fac11 Visnu Pitiyanuvath load deploys on teams/show
visnup authored
6 [Invite, Person, Deploy, Vote] = (mongoose.model m for m in ['Invite', 'Person', 'Deploy', 'Vote'])
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
7
8 TeamSchema = module.exports = new mongoose.Schema
2ffa112 Visnu Pitiyanuvath got teams to actually save
visnup authored
9 name:
10 type: String
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
11 required: true
bbcc7c0 Gerad Suyderhoud unique team name validation
gerad authored
12 unique: true
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
13 description: String
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
14 emails:
15 type: [ mongoose.SchemaTypes.Email ]
6bf7f73 Visnu Pitiyanuvath require at least one email or person [finishes #15748057]
visnup authored
16 validate: [ ((v) -> v.length <= 4), 'max' ]
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
17 invites: [ InviteSchema ]
5d8ced7 Visnu Pitiyanuvath camelcase the db fields [finishes #16099221]
visnup authored
18 peopleIds:
3a40131 Visnu Pitiyanuvath invites saved
visnup authored
19 type: [ mongoose.Schema.ObjectId ]
20 index: true
9663b70 Visnu Pitiyanuvath denormalize the last deploy onto team. closes #58.
visnup authored
21 lastDeploy: {}
dd5e11c Visnu Pitiyanuvath team creator can always edit it [finishes #15746035]
visnup authored
22 code:
23 type: String
24 default: -> rbytes.randomBytes(12).toString('base64')
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
25 search: String
d81de42 Visnu Pitiyanuvath include timestamps
visnup authored
26 TeamSchema.plugin require('mongoose-types').useTimestamps
4d8605a Visnu Pitiyanuvath include description on team index
visnup authored
27 TeamSchema.index updatedAt: -1
2ffa112 Visnu Pitiyanuvath got teams to actually save
visnup authored
28
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup 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 Visnu Pitiyanuvath team code can be used even when you're logged in [#15746035]
visnup authored
41 TeamSchema.method 'includes', (person, code) ->
5d8ced7 Visnu Pitiyanuvath camelcase the db fields [finishes #16099221]
visnup authored
42 @code == code or person and _.any @peopleIds, (id) -> id.equals(person.id)
d65c50f Visnu Pitiyanuvath move voting out into its own REST-y controller. #33.
visnup authored
43 TeamSchema.method 'invited', (invite) ->
44 _.detect @invites, (i) -> i.code == invite
e8d60a0 Visnu Pitiyanuvath bring in postageapp
visnup authored
45
d65c50f Visnu Pitiyanuvath move voting out into its own REST-y controller. #33.
visnup authored
46 # associations
7bcb422 Visnu Pitiyanuvath stop team registration at 200 [finishes #15726989]
visnup authored
47 TeamSchema.method 'people', (next) ->
5d8ced7 Visnu Pitiyanuvath camelcase the db fields [finishes #16099221]
visnup authored
48 Person.find _id: { '$in': @peopleIds }, next
872b253 Danny Coates added /teams/:code/deploys
dannycoates authored
49 TeamSchema.method 'deploys', (next) ->
50 Deploy.find teamId: @id, next
d65c50f Visnu Pitiyanuvath move voting out into its own REST-y controller. #33.
visnup authored
51 TeamSchema.method 'votes', (next) ->
52 Vote.find teamId: @id, next
33b3281 Visnu Pitiyanuvath accepting invites
visnup authored
53
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
54 # validations
7bcb422 Visnu Pitiyanuvath stop team registration at 200 [finishes #15726989]
visnup authored
55
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
56 ## min people
6bf7f73 Visnu Pitiyanuvath require at least one email or person [finishes #15748057]
visnup authored
57 TeamSchema.pre 'save', (next) ->
5d8ced7 Visnu Pitiyanuvath camelcase the db fields [finishes #16099221]
visnup authored
58 if @peopleIds.length + @emails.length == 0
6bf7f73 Visnu Pitiyanuvath require at least one email or person [finishes #15748057]
visnup authored
59 error = new mongoose.Document.ValidationError this
60 error.errors.emails = 'min'
61 next error
62 else
63 next()
64
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
65 ## max teams
7bcb422 Visnu Pitiyanuvath stop team registration at 200 [finishes #15726989]
visnup authored
66 TeamSchema.pre 'save', (next) ->
6d1d845 Visnu Pitiyanuvath can't update a team when teams are maxed out
visnup authored
67 return next() unless @isNew
7bcb422 Visnu Pitiyanuvath stop team registration at 200 [finishes #15726989]
visnup 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 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
77 ## unique name
bbcc7c0 Gerad Suyderhoud 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 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup authored
89 # callbacks
90
91 ## create invites
e8d60a0 Visnu Pitiyanuvath bring in postageapp
visnup authored
92 TeamSchema.pre 'save', (next) ->
b91e9f4 Visnu Pitiyanuvath invites saving and emails sending
visnup 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 Visnu Pitiyanuvath bring in postageapp
visnup authored
97 next()
5b83583 Visnu Pitiyanuvath terser coffee-script
visnup authored
98 TeamSchema.post 'save', ->
b91e9f4 Visnu Pitiyanuvath invites saving and emails sending
visnup authored
99 for invite in @invites
100 invite.remove() unless !invite or _.include(@emails, invite.email)
101 @save() if @isModified 'invites'
969876a Visnu Pitiyanuvath start directing to team creation
visnup authored
102
a712da5 Visnu Pitiyanuvath searchable teams. needs migration. closes #42.
visnup 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 Visnu Pitiyanuvath stop team registration at 200 [finishes #15726989]
visnup authored
115 Team = mongoose.model 'Team', TeamSchema
Something went wrong with that request. Please try again.