Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 48 lines (40 sloc) 1.238 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'
3
3a40131 invites saved
Visnu Pitiyanuvath authored
4 InviteSchema = require './invite'
5 Invite = mongoose.model 'Invite'
33b3281 accepting invites
Visnu Pitiyanuvath authored
6 Person = mongoose.model 'Person'
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
12 emails:
13 type: [ mongoose.SchemaTypes.Email ]
14 validate: [
15 (v) -> 1 <= v.length
16 'min'
17 (v) -> v.length <= 4
18 'max'
19 ]
20 invites: [ InviteSchema ]
21 people_ids:
22 type: [ mongoose.Schema.ObjectId ]
23 index: true
d81de42 include timestamps
Visnu Pitiyanuvath authored
24 TeamSchema.plugin require('mongoose-types').useTimestamps
2ffa112 got teams to actually save
Visnu Pitiyanuvath authored
25
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
26 TeamSchema.method 'includes', (person) ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
27 _.any @people_ids, (id) -> id.equals(person.id)
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
28
33b3281 accepting invites
Visnu Pitiyanuvath authored
29 TeamSchema.method 'people', (callback) ->
30 Person.find id: { '$in': @people_ids }, callback
31
32 TeamSchema.method 'invited', (invite) ->
33 _.detect @invites, (i) -> i.code == invite
34
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
35 # create invites
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
36 TeamSchema.pre 'save', (next) ->
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
37 for email in @emails
38 unless _.detect(@invites, (i) -> i.email == email)
39 @invites.push new Invite(email: email)
40 _.invoke @invites, 'send'
e8d60a0 bring in postageapp
Visnu Pitiyanuvath authored
41 next()
b91e9f4 invites saving and emails sending
Visnu Pitiyanuvath authored
42 TeamSchema.post 'save', () ->
43 for invite in @invites
44 invite.remove() unless !invite or _.include(@emails, invite.email)
45 @save() if @isModified 'invites'
969876a start directing to team creation
Visnu Pitiyanuvath authored
46
3a40131 invites saved
Visnu Pitiyanuvath authored
47 mongoose.model 'Team', TeamSchema
Something went wrong with that request. Please try again.