-
Notifications
You must be signed in to change notification settings - Fork 4
/
shared.js
43 lines (35 loc) · 1.37 KB
/
shared.js
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
Today = new Meteor.Collection('Today')
Meals = new Meteor.Collection('Meals')
Eaters = new Meteor.Collection('Eaters', { transform: function (e) {
e.img = e.img || "http://www.gravatar.com/avatar/" + CryptoJS.MD5(e.name) + "?s=300&d=monsterid"
return e
}})
Eaters.create = function(opts){
if(typeof opts.name != 'string' || opts.name === '' || opts.name.match(/^\s+$/) ) throw new Error("name not string")
opts.status = opts.status || 'jail'
opts.servings = opts.servings || {'given':0,'received':0}
return Eaters.insert(opts)
}
Eaters.scoreSummary = function(eater){
if(!eater) return null
var score = eater.servings.given - eater.servings.received
if (score === 0) return "perfect"
if (score > 0) return "good"
if (score < 0) return "bad"
}
Meals.create = function(meal){
if(!meal) throw new Error("No meal provided")
if (meal.dish === '' || null) throw new Error("Can't create a meal with an empty dish!")
if (meal.date === '' || null) throw new Error("Not valid date!")
if (meal.chefs.length === 0) throw new Error("need chefs")
if (meal.eaters.length === 0) throw new Error("need eaters")
return Meals.insert(meal)
}
Meals.edit = function(id, meal){
if(!meal) throw new Error("No meal provided")
Meals.update(id, {$set: meal})
}
// returns todays date as 2014-02-09
todaysDate = function todaysDate() {
return new Date().toISOString().split('T')[0]
}