-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
160 lines (146 loc) · 4.78 KB
/
models.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function AddEphemeral(db, ephemeral, onSuccess, onFailure) {
// Save document to db
var new_message = db.Ephemeral(ephemeral);
new_message.save(function(err, new_message) {
if (err) { console.log("error saving message to db"); }
else {
// update total messages count
console.log("Added ephemeral successfully");
console.log(new_message);
UpdateCountsWithUpdatedValue(db, onSuccess, onFailure);
}
});
}
function ReadEphemeral(db, itemId, onSuccess, onFailure) {
db.Ephemeral.findById(itemId, function(err, ephemeral) {
if (err) { onFailure(err); }
else {
RemoveEphemeral(db, ephemeral, onSuccess, onFailure);
}
});
}
// Gets the ephemeral given the itemId and returns the found ephemeral
function GetEphemeral(db, itemId, onSuccess, onFailure) {
db.Ephemeral.findById(itemId, function(err, ephemeral) {
if (err) { onFailure(err); }
else { onSuccess(ephemeral); }
});
}
function FindAndRemoveEphemeral(db, ephemeralId, onSuccess) {
db.Ephemeral.findByIdAndRemove(
{ "_id": ephemeralId }, onSuccess );
}
function UpdateEmailSentFlag(db, ephemeral, onSuccess) {
ephemeral.email_sent = "true";
ephemeral.save(
function(err, results) {
if (err) {
console.log("error updating email sent flag");
console.log(err);
} else if (!!onSuccess) {
onSuccess(results);
}
});
}
// Gets number of future ephemerals (notification email not sent yet)
function GetNumUnsentEphemerals(db, onSuccess, onFailure) {
db.Ephemeral.find(
{ "email_sent": false },
function(err, ephemerals) {
if (err) { onFailure(err); }
else { onSuccess(ephemerals.length); }
}
);
}
// Get number of ephemerals waiting to be read
function GetNumSentEphemerals(db, onSuccess, onFailure) {
db.Ephemeral.find({ "email_sent": true },
function(err, ephemerals) {
if (err) { onFailure(err); }
else { onSuccess(ephemerals.length); }
}
);
}
// Query for ephemerals with overdue or due send dates
function GetEphemeralsDueForSending(db, onSuccess, onFailure) {
db.Ephemeral.find(
{ send_date: { $lte: new Date() } },
function(err, results) {
if (err) { onFailure(err); }
else {
onSuccess(results);
console.log("found " + results.length + " ephemerals");
}
}
);
}
// increments totalMessages count but doesn't return the updated count object
function UpdateCounts(db, onSuccess, onFailure) {
db.Globals.update(
{ name: "totalMessages" },
{ $inc: { value: 1} },
function(err, count, results) {
if(err) { onFailure(err); }
else { onSuccess(results); }
}
);
}
// Returns the actual object with updated totalMessages count.
function UpdateCountsWithUpdatedValue(db, onSuccess, onFailure) {
var query = { name: 'totalMessages' };
var update = { $inc:{value:1} };
var options = {};
db.Globals.findOneAndUpdate(query, update, options,
function (err, results) {
if (err) { onFailure(err); }
else { onSuccess(results); }
}
);
}
// Gets the total messages sent
function GetTotalMessageCount(db, onSuccess, onFailure) {
db.Globals.find({name: "totalMessages"}, function(err, results){
if(err) { onFailure(err); }
else { onSuccess(results[0] && results[0].value); }
});
}
// Defining the schema
exports.init = function(mongoose) {
var ephemeralSchema = mongoose.Schema({
message_id: String,
recipient: String,
content: String,
from_user: String,
send_date: {
type: Date
},
create_date: {
type: Date,
default: Date.now()
},
email_sent: {
type: Boolean,
default: false
}
});
// Collection of global counts
var globalsSchema = mongoose.Schema({
name: String,
value: Number,
});
return {
Ephemeral: mongoose.model('Ephemeral', ephemeralSchema),
Globals: mongoose.model('Globals', globalsSchema)
};
};
// Exports
exports.addEphemeral = AddEphemeral;
exports.getTotalMessageCount = GetTotalMessageCount;
exports.updateCounts = UpdateCountsWithUpdatedValue;
exports.readEphemeral = ReadEphemeral;
exports.getEphemeral = GetEphemeral;
exports.updateEmailSentFlag = UpdateEmailSentFlag;
exports.getNumSentEphemerals = GetNumSentEphemerals;
exports.getNumUnsentEphemerals = GetNumUnsentEphemerals;
exports.getEphemeralsDueForSending = GetEphemeralsDueForSending;
exports.findAndRemoveEphemeral = FindAndRemoveEphemeral;