forked from parse-community/parse-server-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunityTotals.js
More file actions
141 lines (130 loc) · 4.61 KB
/
CommunityTotals.js
File metadata and controls
141 lines (130 loc) · 4.61 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
var dateUtil = require('./DateUtil.js');
exports.getAllTimeTotals = function(community, callback) {
//retrieve the AllTimeTotals object, create with defaults if necessary
var CommunityAllTimeTotals = Parse.Object.extend("CommunityAllTimeTotals");
var totalQuery = new Parse.Query(CommunityAllTimeTotals);
totalQuery.equalTo("community", community);
totalQuery.first({
success: function(object) {
var allTimeTotals = object;
if (!allTimeTotals) {
console.log("CommunityAllTimeTotals not found, creating");
allTimeTotals = new CommunityAllTimeTotals();
allTimeTotals.set("addedUsers", 0);
allTimeTotals.set("removedUsers", 0);
allTimeTotals.set("trips", 0);
allTimeTotals.set("distanceTravelled", 0);
allTimeTotals.set("minutesTravelled", 0);
allTimeTotals.set("missedSMSCount", 0);
allTimeTotals.set("missedCallCount", 0);
allTimeTotals.set("missedOtherCount", 0);
allTimeTotals.set("community", community);
allTimeTotals.save();
}
callback(allTimeTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
}
exports.getDailyTotals = function(community, date, callback, failCallback) {
//retrieve the dailyTotals object, create with defaults if necessary
var CommunityDailyTotals = Parse.Object.extend("CommunityDailyTotals");
var totalQuery = new Parse.Query(CommunityDailyTotals);
var queryDate = new Date(date);
queryDate.setHours(0, 0, 0, 0); //only year, month, day remain
totalQuery.equalTo("date", queryDate);
totalQuery.equalTo("community", community);
totalQuery.first({
success: function(object) {
var dailyTotals = object;
if (!dailyTotals) {
console.log("CommunityDailyTotals not found, creating");
dailyTotals = new CommunityDailyTotals();
dailyTotals.set("date", queryDate);
dailyTotals.set("addedUsers", 0);
dailyTotals.set("removedUsers", 0);
dailyTotals.set("trips", 0);
dailyTotals.set("distanceTravelled", 0);
dailyTotals.set("minutesTravelled", 0);
dailyTotals.set("missedSMSCount", 0);
dailyTotals.set("missedCallCount", 0);
dailyTotals.set("missedOtherCount", 0);
dailyTotals.set("community", community);
dailyTotals.save();
}
callback(dailyTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
if (failCallback) {
failCallback(error);
}
}
});
}
exports.getMonthlyTotals = function(community, date, callback, failCallback) {
//retrieve the monthlyTotals object, create with defaults if necessary
var CommunityMonthlyTotals = Parse.Object.extend("CommunityMonthlyTotals");
var totalQuery = new Parse.Query(CommunityMonthlyTotals);
var queryDate = new Date(date);
queryDate.setHours(0, 0, 0, 0);
queryDate.setDate(1); //only year, month remain
totalQuery.equalTo("date", queryDate);
totalQuery.equalTo("community", community);
totalQuery.first({
success: function(object) {
var monthlyTotals = object;
if (!monthlyTotals) {
console.log("CommunityMonthlyTotals not found, creating");
monthlyTotals = new CommunityMonthlyTotals();
monthlyTotals.set("date", queryDate);
monthlyTotals.set("addedUsers", 0);
monthlyTotals.set("removedUsers", 0);
monthlyTotals.set("trips", 0);
monthlyTotals.set("distanceTravelled", 0);
monthlyTotals.set("minutesTravelled", 0);
monthlyTotals.set("missedSMSCount", 0);
monthlyTotals.set("missedCallCount", 0);
monthlyTotals.set("missedOtherCount", 0);
monthlyTotals.set("community", community);
monthlyTotals.save();
}
callback(monthlyTotals);
},
error: function(error) {
console.error("Got an error " + error.code + " : " + error.message);
if (failCallback) {
failCallback(error);
}
}
});
}
exports.updateTotals = function(trip, totals) {
if (!trip.get("hidden")) {
totals.increment("trips");
var distanceTravelled = trip.get("distanceTravelled");
if (distanceTravelled) {
totals.increment("distanceTravelled", distanceTravelled);
}
var startTime = trip.get("startTime");
var endTime = trip.get("endTime");
if (startTime && endTime) {
totals.increment("minutesTravelled", dateUtil.getMinutesBetweenDates(startTime, endTime));
}
var missedSMSCount = trip.get("missedSMSCount");
if (missedSMSCount) {
totals.increment("missedSMSCount", missedSMSCount);
}
var missedCallCount = trip.get("missedCallCount");
if (missedCallCount) {
totals.increment("missedCallCount", missedCallCount);
}
var missedOtherCount = trip.get("missedOtherCount");
if (missedOtherCount) {
totals.increment("missedOtherCount", missedOtherCount);
}
totals.save();
}
}