-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.coffee
More file actions
143 lines (127 loc) · 3.64 KB
/
models.coffee
File metadata and controls
143 lines (127 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
mongoose = require 'mongoose'
Schema = mongoose.Schema
######################
# User
######################
UserSchema = new Schema
created_on:
type: Date
default: Date.now
email:
type: String
unique: true
set: (email) ->
return email.toLowerCase()
hashed_password:
type: String
#set: this.encryptPassword
salt: String
orders: [OrderSchema]
stacks: [StackSchema]
stack_number:
type: Number
default: 0
card_number:
type: Number
default: 0
UserSchema.method 'toLower', (email) ->
return email.toLowerCase()
UserSchema.method 'authenticate', (plainText) ->
return this.encryptPassword(plainText) is this.hashed_password
UserSchema.method 'makeSalt', ->
return Math.round(new Date().valueOf() * Math.random()) + ''
UserSchema.method 'encryptPassword', (password) ->
return crypto.createHmac('sha1', this.salt).update(password).digest('hex')
UserSchema.method 'createStacks', (user, fn) ->
# Initialize stacks and flashcards.
# This method should only ever be called once for this user.
# TODO: Raise error if user has stacks/cards assigned already.
Flashcard.find {}, (err, cards) ->
stack = new Stack
position: 0
for card in cards
flashcard = new Flashcard
stack_id: stack._id
position: card.position
question: card.question
type: card.type
page_number: card.page_number
topic: card.topic
flashcard.save()
user.stacks.push stack
user.save()
fn user
UserSchema.method 'getCurrentStack', (user) ->
for stack in user.stacks
# We have to cast here because stack_number is an Object
# and position is a Number.
if Number(stack.position) is Number(user.stack_number)
return stack
UserSchema.method 'getCurrentFlashcard', (user, fn) ->
stack = user.getCurrentStack user
filter_params = {stack_id: stack._id, position: user.card_number}
Flashcard.findOne filter_params, (err, card) ->
fn card
UserSchema.method 'getNextFlashcard', (user, fn) ->
user.card_number += 1
user.save()
user.getCurrentFlashcard user, (card) ->
if card
fn card
else
# If we can't find a card we have reached the last card in this stack.
# Return the first card in the next stack.
user.stack_number += 1
user.card_number = 0
user.save()
user.getCurrentFlashcard user, (card) ->
fn card
######################
# Order
######################
OrderSchema = new Schema
product: String
created_on:
type: Date
default: Date.now
######################
# Stack
######################
StackSchema = new Schema
position: Number
######################
# Flashcard
######################
FlashcardSchema = new Schema
position: Number
stack_id: Schema.ObjectId
# question text
question: String
# type could be free response or matching
type: String
# page number in First Aid book
page_number: Number
# medical topic area
topic: String
answers: [AnswerSchema]
######################
# Answer
######################
AnswerSchema = new Schema
created_on:
type: Date
default: Date.now
# how the user rated his response
rating: Number
# answer text (not sure how this will be formatted for matching)
answer: String
mongoose.model 'Answer', AnswerSchema
mongoose.model 'Flashcard', FlashcardSchema
mongoose.model 'Order', OrderSchema
mongoose.model 'Stack', StackSchema
mongoose.model 'User', UserSchema
exports.Answer = Answer = mongoose.model 'Answer'
exports.Flashcard = Flashcard = mongoose.model 'Flashcard'
exports.Order = Order = mongoose.model 'Order'
exports.Stack = Stack = mongoose.model 'Stack'
exports.User = User = mongoose.model 'User'