Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

invites saving and emails sending

  • Loading branch information...
commit b91e9f4ab75c717b51bb4eeda90b47c05a2813be 1 parent 3a40131
Visnu Pitiyanuvath visnup authored
2  controllers/teams.coffee
View
@@ -52,8 +52,6 @@ app.put '/teams/:id', (req, res, next) ->
_.extend team, req.body
team.save (err) ->
if err
- throw err unless err.errors
- console.log require('util').inspect(err, false, null)
res.render2 'teams/edit', team: team, errors: err.errors
else
res.redirect "/teams/#{team.id}"
17 models/invite.coffee
View
@@ -1,10 +1,27 @@
mongoose = require 'mongoose'
rbytes = require 'rbytes'
+util = require 'util'
+env = require '../config/env'
+postageapp = require('postageapp')(env.secrets.postageapp)
InviteSchema = module.exports = new mongoose.Schema
email: String
+ sent:
+ type: Boolean
+ default: no
code:
type: String
default: () -> rbytes.randomBytes(12).toString('base64')
+InviteSchema.method 'send', (person) ->
+ unless @sent
+ util.log "Sending 'teams_new' to '#{@email}'"
+ team = @parentArray._parent
+ postageapp.apiCall @email, 'teams_new', null, 'all@nodeknockout.com',
+ team_id: team.id
+ team_name: team.name
+ #person_github_login: person.github.login
+ invite_code: @code
+ @sent = yes
+
mongoose.model 'Invite', InviteSchema
2  models/person.coffee
View
@@ -16,6 +16,6 @@ PersonSchema.plugin auth,
redirectPath: '/people/me'
PersonSchema.method 'team', (callback) ->
Team = mongoose.model 'Team'
- Team.find people_ids: this.id, callback
+ Team.find people_ids: @id, callback
Person = mongoose.model 'Person', PersonSchema
23 models/team.coffee
View
@@ -1,7 +1,5 @@
_ = require 'underscore'
mongoose = require 'mongoose'
-env = require '../config/env'
-postageapp = require('postageapp')(env.secrets.postageapp)
InviteSchema = require './invite'
Invite = mongoose.model 'Invite'
@@ -25,19 +23,18 @@ TeamSchema = module.exports = new mongoose.Schema
TeamSchema.plugin require('mongoose-types').useTimestamps
TeamSchema.method 'includes', (person) ->
- _.any this.people_ids, (id) -> id.equals(person.id)
+ _.any @people_ids, (id) -> id.equals(person.id)
-# create stub people? and send emails
+# create invites
TeamSchema.pre 'save', (next) ->
- this.invites = for email in this.emails
- _.detect(this.invites, (i) -> i.email == email) or new Invite email: email
- ###
- postageapp.apiCall this.emails, 'teams_new', null, 'all@nodeknockout.com',
- team_id: this.id
- team_name: this.name
- person_github_login: 'visnup'
- invite_code: 'awef'
- ###
+ for email in @emails
+ unless _.detect(@invites, (i) -> i.email == email)
+ @invites.push new Invite(email: email)
+ _.invoke @invites, 'send'
next()
+TeamSchema.post 'save', () ->
+ for invite in @invites
+ invite.remove() unless !invite or _.include(@emails, invite.email)
+ @save() if @isModified 'invites'
mongoose.model 'Team', TeamSchema
5 views/teams/show.jade
View
@@ -1,4 +1,3 @@
-h1= team.get('name')
+h1= team.name
-p
- a( href: '/teams/' + team.id + '/edit' ) Edit
+p: a.button( href: '/teams/' + team.id + '/edit' ) Edit
Please sign in to comment.
Something went wrong with that request. Please try again.