-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
215 lines (188 loc) · 6.08 KB
/
controller.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//const data = require("./data");
const data = require("./data_asp_nyc");
const data2022 = require("./data_asp_nyc_2022");
const data2023 = require("./data_asp_nyc_2023");
const data2024 = require("./data_asp_nyc_2024");
// Logic behind the functionalities
var serverTime = "America/New_York"; // America/New_York /
// current datetime string inAmerica/New_York timezone
let local_datetime_str = new Date().toLocaleString("en-US", { timeZone: serverTime });
// create new Date object
//let date_local = new Date(local_datetime_str);
let d = new Date(local_datetime_str);
// Months
let allMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"];
let monthTxt = allMonths[d.getMonth()]; // May
// year as (YYYY) format
let year = d.getFullYear();
// month as (MM) format
let month = ("0" + (d.getMonth() + 1)).slice(-2);
// date as (DD) format
let date = ("0" + d.getDate()).slice(-2);
// date time in YYYY-MM-DD format
//let date_time = year + "-" + month + "-" + date;
let today = month + "/" + date + "/" + year; // "11/13/2022"
//console.log(today);
//
// Yesterday
const y = new Date(d);
//console.log(y);
y.setDate(y.getDate() - 1);
//let y_dd = String(y.getDate()).padStart(1, '0'); // 17
// date as (DD) format
let y_date = ("0" + y.getDate()).slice(-2);
//let y_day = weekday[y.getDay()]; // Tuesday
//let y_month = allMonths[y.getMonth()]; // May
let y_mm = String(y.getMonth() + 1).padStart(2, '0'); // 11
let y_yyyy = y.getFullYear(); // 2022
let yesterday = y_mm +'/'+ y_date +'/'+ y_yyyy; // -> 11/12/2022
//console.log(yesterday);
// Tomorrow
const t = new Date(d);
t.setDate(t.getDate() + 1);
//let t_dd = String(t.getDate()).padStart(1, '0'); // 19
// date as (DD) format
let t_date = ("0" + t.getDate()).slice(-2);
//let t_day = weekday[t.getDay()]; // Thursday
//let t_month = allMonths[t.getMonth()]; // May
let t_mm = String(t.getMonth() + 1).padStart(2, '0'); // 11
let t_yyyy = t.getFullYear(); // 2022
let tomorrow = t_mm +'/'+ t_date +'/'+ t_yyyy; // -> 11/14/2022
//console.log(tomorrow);
// Next 7 days start & end
const n = new Date(d);
n.setDate(n.getDate() + 7);
let n_dd = String(n.getDate()).padStart(2, '0'); // 19
//let n_day = weekday[n.getDay()]; // Thursday
//let n_month = allMonths[n.getMonth()]; // May
let n_mm = String(n.getMonth() + 1).padStart(2, '0'); // 11
let n_yyyy = n.getFullYear(); // 2022
let end_day = n_mm +'/'+ n_dd +'/'+ n_yyyy; // -> May 19, 2022
//console.log(end_day);
const index_start = data.map(i => i.fullDate).indexOf(today);
//console.log(index);
const index_end = data.map(i => i.fullDate).indexOf(end_day);
//const id_num = index_end + 1;
//console.log(id_num);
//const start_index = data.map(i => i.dateNum).indexOf("01/01");
//console.log(start_index);
//const end_index = data.map(i => i.dateNum).indexOf("12/31");
//console.log(end_index);
class Controller {
// getting all data
async getAllData2022() {
// return all data
return new Promise((resolve, _) => resolve(data2022));
}
// getting all data
async getAllData2023() {
// return all data
return new Promise((resolve, _) => resolve(data2023));
}
// getting all data
async getAllData2024() {
// return all data
return new Promise((resolve, _) => resolve(data2024));
}
//
// getting a single data
async getSingleData(X) {
return new Promise((resolve, reject) => {
// get the data
let singleData = data.find((Y) => Y.dateMMDDYY === X);
if (singleData) {
// return the data
resolve(singleData);
} else {
// return an error
reject(`Object with id ${X} not found `);
}
});
}
//
// getting the next 7 days data
async get7Data() {
return new Promise((resolve, reject) => {
// get the data
// slice from 1..3 - add 1 as the end index is not included
const data_7_days = data.slice(index_start, index_end);
if (data_7_days) {
// return the data
resolve(data_7_days);
} else {
// return an error
reject(`Object 7 days data not found `);
}
});
}
//
// getting today data
async getTodayData() {
return new Promise((resolve, reject) => {
// get the data
let todayData = data.filter(function(todayIn) {
return todayIn.fullDate == today; });
// console.log(todayData);
//
if (todayData) {
// return the data
resolve(todayData);
} else {
// return an error
reject(`Today object not found `);
}
});
}
// getting yesterday data
async getYesterdayData() {
return new Promise((resolve, reject) => {
// get the data
let yesterdayData = data.filter(function(yesterdayIn) {
return yesterdayIn.fullDate == yesterday; });
// console.log(yesterdayData);
if (yesterdayData) {
// return the data
resolve(yesterdayData);
} else {
// return an error
reject(`Yesterday object not found `);
}
});
}
// getting tomorrow data
async getTomorrowData() {
return new Promise((resolve, reject) => {
// get the data
let tomorrowData = data.filter(function(tomorrowIn) {
return tomorrowIn.fullDate == tomorrow; });
// console.log(tomorrowData);
if (tomorrowData) {
// return the data
resolve(tomorrowData);
} else {
// return an error
reject(`Tomorrow object not found `);
}
});
}
//
// getting holiday data
async getHolidayData() {
return new Promise((resolve, reject) => {
// get the data
let holidayData = data.filter(function(holidayIn) {
return holidayIn.aspRules == "Suspended"; });
// console.log(holidayData);
//
if (holidayData) {
// return the data
resolve(holidayData);
} else {
// return an error
reject(`Holiday object not found `);
}
});
}
// add above
}
module.exports = Controller;