Skip to content
Newer
Older
100644 136 lines (122 sloc) 4.1 KB
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
1 _ = require 'underscore'
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
2 mongoose = require 'mongoose'
3 ObjectId = mongoose.Schema.ObjectId
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
4 Person = mongoose.model 'Person'
cc0a08d @gerad refactor message sending into the vote model
gerad authored
5 util = require 'util'
6 env = require '../config/env'
7 postageapp = require('postageapp')(env.secrets.postageapp)
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
8
a58f1ce disallow 0 or >6 value votes
Visnu Pitiyanuvath authored
9 Dimension =
10 type: Number
11 validate: [ (v) ->
12 v == null || 0 < v < 6
13 , 'out of bounds' ]
14
48b2e5f @gerad reply to votes #34
gerad authored
15 ReplySchema = new mongoose.Schema
16 personId:
17 type: ObjectId
18 required: true
39f7514 style replies
Visnu Pitiyanuvath authored
19 person: {}
48b2e5f @gerad reply to votes #34
gerad authored
20 message: String
21
a34ab42 @gerad reply sends email
gerad authored
22 # instance methods
23 ReplySchema.method 'notifyPeople', (vote) ->
24 peopleIds = vote.team.peopleIds.concat vote.personId
25 Person.find { _id: ($in: peopleIds) }, (err, people) =>
26 throw err if err
27 isCommenter = (p) => @personId.equals p.id
28 commenter = _.detect people, isCommenter
29 others = _.reject people, isCommenter
30
31 for person in others
32 template = "replied_#{if vote.personId.equals(person.id) then 'judge' else 'team'}"
33 util.log "Sending '#{template}' to '#{person.email}'".yellow
34 postageapp.apiCall person.email, template, null, 'all@nodeknockout.com',
35 vote_id: vote.id
36 person_id: commenter.id
37 person_name: commenter.name
38 message: @message
39 team_id: vote.team.slug
40 entry_name: vote.team.entry.name
41
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
42 VoteSchema = module.exports = new mongoose.Schema
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
43 personId:
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
44 type: ObjectId
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
45 required: true
5d8ced7 camelcase the db fields [finishes #16099221]
Visnu Pitiyanuvath authored
46 teamId:
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
47 type: ObjectId
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
48 required: true
49 type:
50 type: String
51 required: true
52 enum: Person.ROLES
53 comment: String
a58f1ce disallow 0 or >6 value votes
Visnu Pitiyanuvath authored
54 utility: Dimension
55 design: Dimension
56 innovation: Dimension
57 completeness: Dimension
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
58 audit:
59 remoteAddress: String
60 remotePort: Number
61 userAgent: String
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
62 referrer: String
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
63 accept: String
64 requestAt: Number
65 hoverAt: Number
48b2e5f @gerad reply to votes #34
gerad authored
66 replies: [ReplySchema]
2883465 @gerad replace broken mongoose-types useTimestamps plugin
gerad authored
67 VoteSchema.plugin require('../lib/use-timestamps')
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
68
d65c50f move voting out into its own REST-y controller. #33.
Visnu Pitiyanuvath authored
69 # one vote per person-team-type
70 VoteSchema.index { personId: 1, teamId: 1, type: 1 }, { unique: true }
71 VoteSchema.index { personId: 1, updatedAt: -1 }
72 VoteSchema.index { teamId: 1, updatedAt: -1 }
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
73
48b2e5f @gerad reply to votes #34
gerad authored
74 # class methods
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
75 VoteSchema.static 'dimensions',
76 [ 'utility', 'design', 'innovation', 'completeness' ]
6875707 label utility/fun correctly
Visnu Pitiyanuvath authored
77 VoteSchema.static 'label', (dimension) ->
78 switch dimension
79 when 'utility' then 'Utility/Fun'
80 when 'design' then 'Design'
81 when 'innovation' then 'Innovation'
82 when 'completeness' then 'Completeness'
48b2e5f @gerad reply to votes #34
gerad authored
83 VoteSchema.static 'Reply', -> Reply
84
85 # instance methods
86 VoteSchema.method 'replyable', (person) ->
87 @personId.equals(person.id) or
88 @team.includes(person)
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
89
cc0a08d @gerad refactor message sending into the vote model
gerad authored
90 VoteSchema.method 'notifyTeam', ->
91 @team.people (err, people) =>
92 throw err if err
93
94 @person.team (err, voterTeam) =>
95 throw err if err
96
97 for person in people
98 util.log "Sending 'voted_on_by_#{@type}' to '#{person.email}'".yellow
99 postageapp.apiCall person.email, "voted_on_by_#{@type}", null, 'all@nodeknockout.com',
100 vote_id: @id
101 person_id: @person.id
102 person_name: @person.name
103 utility_score: @utility
104 design_score: @design
105 innovation_score: @innovation
106 completeness_score: @completeness
107 comment: @comment
108 team_id: @team.slug
109 entry_name: @team.entry.name
110 person_team_id: voterTeam?.slug
111 person_entry_name: voterTeam?.entry?.name
112
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
113 # associations
114 VoteSchema.static 'people', (votes, next) ->
115 peopleIds = _.pluck votes, 'personId'
44bd3b6 see votes by a person
Visnu Pitiyanuvath authored
116 return next() if peopleIds.length == 0
af19125 correct time tag, with humanish readable title
Visnu Pitiyanuvath authored
117 # TODO only need certain fields probably; make `only` an argument
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
118 Person.find _id: { '$in': peopleIds }, (err, people) ->
119 return next err if err
120 people = _.reduce people, ((h, p) -> h[p.id] = p; h), {}
121 _.each votes, (v) -> v.person = people[v.personId]
122 next()
44bd3b6 see votes by a person
Visnu Pitiyanuvath authored
123 VoteSchema.static 'teams', (votes, next) ->
124 teamIds = _.pluck votes, 'teamId'
125 return next() if teamIds.length == 0
126 # TODO only need certain fields probably; make `only` an argument
c2fac11 load deploys on teams/show
Visnu Pitiyanuvath authored
127 Team = mongoose.model 'Team'
44bd3b6 see votes by a person
Visnu Pitiyanuvath authored
128 Team.find _id: { '$in': teamIds }, (err, teams) ->
129 return next err if err
130 teams = _.reduce teams, ((h, t) -> h[t.id] = t; h), {}
131 _.each votes, (v) -> v.team = teams[v.teamId]
132 next()
b2a1b82 save and view votes
Visnu Pitiyanuvath authored
133
48b2e5f @gerad reply to votes #34
gerad authored
134 Reply = mongoose.model 'Reply', ReplySchema
dc65408 @dshaw Upvote model. Still needs accessor for page display.
dshaw authored
135 Vote = mongoose.model 'Vote', VoteSchema
Something went wrong with that request. Please try again.