-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
120 lines (109 loc) · 3.33 KB
/
database.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
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
var _ = require('underscore');
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
mongoose.connect( process.env.MONGOHQ_URL || 'mongodb://localhost/donorManagementSystem');
exports.disconnect = function() {
mongoose.disconnect();
}
var UserSchema = new Schema({
name : String,
email: String,
username : String,
password: String
});
exports.UserSchema = UserSchema;
var RemarkSchema = new Schema({
active : {type: Boolean, default:true},
name : String,
text : String,
date : Date,
target : {type: ObjectId},
user : {type: ObjectId, ref: 'users'}
});
exports.RemarkSchema = RemarkSchema;
var PostSchema = new Schema({
date : Date,
subject : String,
user : {type: ObjectId, ref: 'users'},
medium : String,
message : String
});
var DonorSchema = new Schema({
name : { type: String, required: true },
email: { type: String, required: false },
company : String,
street : String,
town : String,
zipcode : String,
country : String,
personalInterests : String,
telephone : String,
birthday : Date,
website : String,
donationDates : [Date], // TODO: Allow for ranges
communicationLog : [{type: ObjectId, ref: 'posts'}],
remarks: [{type: ObjectId, ref: 'remarks'}],
user: {type: ObjectId, ref: 'users'},
isDonor : {type:Boolean, default:true}
});
exports.DonorSchema = DonorSchema;
var GroupSchema = new Schema({
user : {type: ObjectId, ref: 'users'},
name : String,
description : String,
donors : [{type: ObjectId, ref: 'donors'}],
remarks: [{type: ObjectId, ref: 'remarks'}],
rules : String, // TODO: Create some format for rules
communicationLog : [{type: ObjectId, ref: 'posts'}],
isDonor : {type:Boolean, default:false} // to check
});
exports.GroupSchema = GroupSchema;
var DonationSchema = new Schema({
amount : Number,
date : Date,
donor : {type: ObjectId, ref: 'donors'}, // DonorSchema
remarks: [{type: ObjectId, ref: 'remarks'}],
user : {type: ObjectId, ref: 'users'} // UserSchema
});
exports.DonationSchema = DonationSchema;
var DonationRequestSchema = new Schema({
name : String,
donors : [{type: ObjectId, ref: 'donors'}],
groups : [{type: ObjectId, ref: 'groups'}],
message : String, // TODO: Handle pictures etc.
sentDate : Date,
subject : String,
user : {type: ObjectId, ref: 'users'} // UserSchema
});
DonationRequestSchema.virtual('numberOfTargets')
.get(function () {
var receiver = this.donors;
this.groups.forEach(function(grp){
grp.donors.forEach(function(donorId){
var found = false;
receiver.forEach(function(d){
if(d==donorId){
found = true;
return true;
}
});
if (found == false){
receiver.push(donorId);
}
});
});
return receiver.length;
});
DonationRequestSchema.virtual('sent')
.get(function () {
return (this.sentDate == null || this.sentDate > Date.now() ) ?
"No" : "Yes";
});
exports.DonationRequestSchema = DonationRequestSchema;
exports.Remark = mongoose.model('remarks', RemarkSchema);
exports.Post = mongoose.model('posts', PostSchema);
exports.Donor = mongoose.model('donors', DonorSchema);
exports.User = mongoose.model('users', UserSchema);
exports.Group = mongoose.model('groups', GroupSchema);
exports.DonationRequest = mongoose.model('requests', DonationRequestSchema);