@justair/justair-library 4.7.1 → 4.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README +146 -146
- package/package.json +28 -28
- package/src/config/db.js +116 -116
- package/src/config/logger.js +44 -44
- package/src/index.js +113 -113
- package/src/models/admin.js +30 -30
- package/src/models/alerts.js +221 -221
- package/src/models/announcements.js +31 -31
- package/src/models/apiKey.js +22 -22
- package/src/models/configurations.js +17 -17
- package/src/models/contexts.js +13 -13
- package/src/models/dataCompleteness.js +22 -22
- package/src/models/events.js +123 -123
- package/src/models/features.js +14 -14
- package/src/models/jobs.js +49 -49
- package/src/models/lightmonitors.js +28 -28
- package/src/models/measurements.js +251 -251
- package/src/models/monitorRequests.js +25 -25
- package/src/models/monitorSuppliers.js +21 -21
- package/src/models/monitorType.js +40 -0
- package/src/models/monitors.js +350 -350
- package/src/models/organizations.js +84 -84
- package/src/models/parameters.js +11 -11
- package/src/models/referenceMonitorInfo.js +18 -18
- package/src/models/tests/admin.test.js +42 -42
- package/src/models/tests/configurations.test.js +44 -44
- package/src/models/tests/measurements.test.js +46 -46
- package/src/models/tests/monitorRequests.test.js +46 -46
- package/src/models/tests/monitors.test.js +62 -62
- package/src/models/tests/organizations.test.js +51 -51
- package/src/models/tests/users.test.js +54 -54
- package/src/models/usageMetrics.js +28 -28
- package/src/models/users.js +55 -55
- package/tsconfig.json +10 -10
package/src/models/monitors.js
CHANGED
|
@@ -1,350 +1,350 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
const parametersEnum = [
|
|
3
|
-
"NO2",
|
|
4
|
-
"SO2",
|
|
5
|
-
"PM2.5",
|
|
6
|
-
"PM10",
|
|
7
|
-
"Temperature",
|
|
8
|
-
"Humidity",
|
|
9
|
-
"OZONE",
|
|
10
|
-
"VOC",
|
|
11
|
-
"CO",
|
|
12
|
-
"NO",
|
|
13
|
-
"PM1",
|
|
14
|
-
"WS And Direction",
|
|
15
|
-
"DP",
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
const noteSchema = mongoose.Schema({
|
|
19
|
-
note: {
|
|
20
|
-
type: String,
|
|
21
|
-
required: true,
|
|
22
|
-
},
|
|
23
|
-
type: {
|
|
24
|
-
type: String,
|
|
25
|
-
required: true,
|
|
26
|
-
},
|
|
27
|
-
monitorState: {
|
|
28
|
-
type: String,
|
|
29
|
-
required: true,
|
|
30
|
-
},
|
|
31
|
-
adminId: {
|
|
32
|
-
type: mongoose.Types.ObjectId,
|
|
33
|
-
ref: "Admin",
|
|
34
|
-
required: true,
|
|
35
|
-
},
|
|
36
|
-
adminName: {
|
|
37
|
-
type: String,
|
|
38
|
-
},
|
|
39
|
-
date: {
|
|
40
|
-
type: Date,
|
|
41
|
-
default: Date.now,
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const correctionSchema = mongoose.Schema(
|
|
46
|
-
{
|
|
47
|
-
equationType: {
|
|
48
|
-
type: String,
|
|
49
|
-
required: true,
|
|
50
|
-
},
|
|
51
|
-
variables: {
|
|
52
|
-
required: true,
|
|
53
|
-
type: Object,
|
|
54
|
-
},
|
|
55
|
-
dateCreated: {
|
|
56
|
-
type: Date,
|
|
57
|
-
default: Date.now,
|
|
58
|
-
},
|
|
59
|
-
applyCorrection: { type: Boolean, default: true },
|
|
60
|
-
},
|
|
61
|
-
{ _id: false } // no separate _id for sub-docs
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const correctionHistorySchema = mongoose.Schema(
|
|
65
|
-
{
|
|
66
|
-
pollutant: {
|
|
67
|
-
type: String,
|
|
68
|
-
required: true,
|
|
69
|
-
},
|
|
70
|
-
oldValue: {
|
|
71
|
-
type: correctionSchema,
|
|
72
|
-
default: undefined,
|
|
73
|
-
},
|
|
74
|
-
newValue: {
|
|
75
|
-
type: correctionSchema,
|
|
76
|
-
default: undefined,
|
|
77
|
-
},
|
|
78
|
-
changedAt: {
|
|
79
|
-
type: Date,
|
|
80
|
-
default: Date.now,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
{ _id: false }
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
// Monitor Audit Schema
|
|
87
|
-
const monitorAuditSchema = mongoose.Schema(
|
|
88
|
-
{
|
|
89
|
-
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
90
|
-
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
91
|
-
timeUpdated: Date,
|
|
92
|
-
deletedAt: { type: Date, default: Date.now }, // Only populated on delete
|
|
93
|
-
monitorProperties: Object, // Stores properties of the monitor
|
|
94
|
-
monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
|
|
95
|
-
monitorLocation: {
|
|
96
|
-
// Monitor GPS location (lat, lon)
|
|
97
|
-
type: { type: String, enum: ["Point"], required: true },
|
|
98
|
-
coordinates: { type: [Number], required: true },
|
|
99
|
-
},
|
|
100
|
-
parameterThresholds: Object,
|
|
101
|
-
corrections: {
|
|
102
|
-
type: Map,
|
|
103
|
-
of: correctionSchema,
|
|
104
|
-
default: {},
|
|
105
|
-
},
|
|
106
|
-
correctionHistory: {
|
|
107
|
-
type: [correctionHistorySchema],
|
|
108
|
-
default: [],
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
timestamps: true,
|
|
113
|
-
}
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
// Create the MonitorAudit model
|
|
117
|
-
const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
|
|
118
|
-
|
|
119
|
-
// Monitors Schema
|
|
120
|
-
const monitorsSchema = mongoose.Schema(
|
|
121
|
-
{
|
|
122
|
-
monitorCode: String,
|
|
123
|
-
monitorSupplier: {
|
|
124
|
-
type: String,
|
|
125
|
-
enum: [
|
|
126
|
-
"clarity",
|
|
127
|
-
"aeroqual",
|
|
128
|
-
"purple air",
|
|
129
|
-
"reference monitor",
|
|
130
|
-
"earthview",
|
|
131
|
-
"sensit",
|
|
132
|
-
"blue sky",
|
|
133
|
-
"aq mesh",
|
|
134
|
-
"quant aq",
|
|
135
|
-
"airGradient",
|
|
136
|
-
"oizom",
|
|
137
|
-
"metOne",
|
|
138
|
-
],
|
|
139
|
-
},
|
|
140
|
-
monitorType: String,
|
|
141
|
-
monitorIdFromSupplier: String,
|
|
142
|
-
measurementUpdate: Date,
|
|
143
|
-
monitorProperties: Object,
|
|
144
|
-
isPrivate: { type: Boolean, default: false, required: true },
|
|
145
|
-
monitorState: {
|
|
146
|
-
type: String,
|
|
147
|
-
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
148
|
-
},
|
|
149
|
-
monitorStateHistory: [Object],
|
|
150
|
-
monitorAlertStatus: {
|
|
151
|
-
type: String,
|
|
152
|
-
enum: [
|
|
153
|
-
"Good",
|
|
154
|
-
"Moderate",
|
|
155
|
-
"Unhealthy for SG",
|
|
156
|
-
"Unhealthy",
|
|
157
|
-
"Very Unhealthy",
|
|
158
|
-
"Hazardous",
|
|
159
|
-
"Bad",
|
|
160
|
-
],
|
|
161
|
-
default: "Good",
|
|
162
|
-
},
|
|
163
|
-
sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
164
|
-
sponsorName: String,
|
|
165
|
-
monitorLatitude: Number,
|
|
166
|
-
monitorLongitude: Number,
|
|
167
|
-
gpsLocation: {
|
|
168
|
-
type: { type: String, enum: ["Point"], required: true },
|
|
169
|
-
coordinates: { type: [Number], required: true },
|
|
170
|
-
},
|
|
171
|
-
location: Object,
|
|
172
|
-
context: [String],
|
|
173
|
-
colocationDate: Date,
|
|
174
|
-
deploymentDate: Date,
|
|
175
|
-
subscriptionDate: Date,
|
|
176
|
-
parameters: [
|
|
177
|
-
{
|
|
178
|
-
type: String,
|
|
179
|
-
enum: parametersEnum,
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
latestPM2_5: Number,
|
|
183
|
-
latestAQI_PM2_5: Number,
|
|
184
|
-
notes: [noteSchema],
|
|
185
|
-
calculatedAverages: [Object],
|
|
186
|
-
images: [String],
|
|
187
|
-
isActive: { type: Boolean, default: true },
|
|
188
|
-
parameterThresholds: Object,
|
|
189
|
-
completionPercentageThresholds: Object,
|
|
190
|
-
// A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
|
|
191
|
-
corrections: {
|
|
192
|
-
type: Map,
|
|
193
|
-
of: correctionSchema,
|
|
194
|
-
default: {},
|
|
195
|
-
},
|
|
196
|
-
correctionHistory: {
|
|
197
|
-
type: [correctionHistorySchema],
|
|
198
|
-
default: [],
|
|
199
|
-
},
|
|
200
|
-
applyCorrections: { type: Boolean, default: false },
|
|
201
|
-
pausedParameters: {
|
|
202
|
-
type: [{ parameter: String, timestamp: Date, isPaused: Boolean }],
|
|
203
|
-
default: [],
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
{
|
|
207
|
-
timestamps: true,
|
|
208
|
-
}
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
// Geographic queries - already exists
|
|
212
|
-
monitorsSchema.index({ gpsLocation: "2dsphere" });
|
|
213
|
-
|
|
214
|
-
// Sponsor-based queries
|
|
215
|
-
monitorsSchema.index({ sponsor: 1, isPrivate: 1, isActive: 1 });
|
|
216
|
-
|
|
217
|
-
// Location-based filtering
|
|
218
|
-
monitorsSchema.index({ "location.city": 1, "location.state": 1 });
|
|
219
|
-
monitorsSchema.index({ "location.neighborhood": 1 });
|
|
220
|
-
monitorsSchema.index({ "location.county": 1 });
|
|
221
|
-
|
|
222
|
-
// Query parameter filtering
|
|
223
|
-
monitorsSchema.index({ monitorSupplier: 1, monitorState: 1 });
|
|
224
|
-
monitorsSchema.index({ context: 1 });
|
|
225
|
-
monitorsSchema.index({ parameters: 1 });
|
|
226
|
-
|
|
227
|
-
// Monitor lookups by supplier
|
|
228
|
-
monitorsSchema.index({ sponsor: 1, monitorSupplier: 1 });
|
|
229
|
-
|
|
230
|
-
// Keep existing single-field indexes for backward compatibility
|
|
231
|
-
monitorsSchema.index({ monitorSupplier: 1 });
|
|
232
|
-
monitorsSchema.index({ monitorIdFromSupplier: 1 });
|
|
233
|
-
monitorsSchema.index({ monitorState: 1 });
|
|
234
|
-
|
|
235
|
-
// Pre-hook to log single document deletions
|
|
236
|
-
monitorsSchema.pre("findOneAndDelete", async function () {
|
|
237
|
-
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
238
|
-
if (docToDelete) {
|
|
239
|
-
console.log("Logging findOneAndDelete to monitor audit", docToDelete);
|
|
240
|
-
const auditLog = new MonitorAudit({
|
|
241
|
-
monitorId: docToDelete._id,
|
|
242
|
-
orgId: docToDelete.sponsor,
|
|
243
|
-
timeUpdated: docToDelete.updatedAt,
|
|
244
|
-
monitorProperties: docToDelete.monitorProperties,
|
|
245
|
-
monitorState: docToDelete.monitorState,
|
|
246
|
-
monitorLocation: {
|
|
247
|
-
type: "Point",
|
|
248
|
-
coordinates: [
|
|
249
|
-
docToDelete.monitorLongitude,
|
|
250
|
-
docToDelete.monitorLatitude,
|
|
251
|
-
],
|
|
252
|
-
},
|
|
253
|
-
parameterThresholds: docToDelete.parameterThresholds,
|
|
254
|
-
corrections: docToDelete.corrections,
|
|
255
|
-
correctionHistory: docToDelete.correctionHistory,
|
|
256
|
-
applyCorrections: docToDelete.applyCorrections,
|
|
257
|
-
deletedAt: new Date(),
|
|
258
|
-
});
|
|
259
|
-
await auditLog.save();
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// Pre-hook to log multiple document deletions
|
|
264
|
-
monitorsSchema.pre("deleteMany", async function () {
|
|
265
|
-
console.log("deleteMany pre-hook triggered for monitors");
|
|
266
|
-
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
267
|
-
|
|
268
|
-
if (docsToDelete.length) {
|
|
269
|
-
console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
|
|
270
|
-
const auditLogs = docsToDelete.map((doc) => ({
|
|
271
|
-
monitorId: doc._id,
|
|
272
|
-
orgId: doc.sponsor,
|
|
273
|
-
timeUpdated: doc.updatedAt,
|
|
274
|
-
monitorProperties: doc.monitorProperties,
|
|
275
|
-
monitorState: doc.monitorState,
|
|
276
|
-
monitorLocation: {
|
|
277
|
-
type: "Point",
|
|
278
|
-
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
279
|
-
},
|
|
280
|
-
parameterThresholds: doc.parameterThresholds,
|
|
281
|
-
corrections: doc.corrections,
|
|
282
|
-
correctionHistory: doc.correctionHistory,
|
|
283
|
-
applyCorrections: doc.applyCorrections,
|
|
284
|
-
deletedAt: new Date(),
|
|
285
|
-
}));
|
|
286
|
-
|
|
287
|
-
await MonitorAudit.insertMany(auditLogs);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
// Pre-hook to log a single document deletion (for deleteOne)
|
|
292
|
-
monitorsSchema.pre("deleteOne", async function () {
|
|
293
|
-
console.log("deleteOne pre-hook triggered for monitors");
|
|
294
|
-
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
295
|
-
|
|
296
|
-
if (docToDelete) {
|
|
297
|
-
console.log("Logging deleteOne to monitor audit", docToDelete);
|
|
298
|
-
const auditLog = new MonitorAudit({
|
|
299
|
-
monitorId: docToDelete._id,
|
|
300
|
-
orgId: docToDelete.sponsor,
|
|
301
|
-
timeUpdated: docToDelete.updatedAt,
|
|
302
|
-
monitorProperties: docToDelete.monitorProperties,
|
|
303
|
-
monitorState: docToDelete.monitorState,
|
|
304
|
-
monitorLocation: {
|
|
305
|
-
type: "Point",
|
|
306
|
-
coordinates: [
|
|
307
|
-
docToDelete.monitorLongitude,
|
|
308
|
-
docToDelete.monitorLatitude,
|
|
309
|
-
],
|
|
310
|
-
},
|
|
311
|
-
parameterThresholds: docToDelete.parameterThresholds,
|
|
312
|
-
corrections: docToDelete.corrections,
|
|
313
|
-
correctionHistory: docToDelete.correctionHistory,
|
|
314
|
-
applyCorrections: docToDelete.applyCorrections,
|
|
315
|
-
deletedAt: new Date(),
|
|
316
|
-
});
|
|
317
|
-
await auditLog.save();
|
|
318
|
-
}
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
// Pre-hook to log multiple document updates
|
|
322
|
-
monitorsSchema.pre("updateMany", async function () {
|
|
323
|
-
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
324
|
-
if (docsToUpdate.length) {
|
|
325
|
-
console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
|
|
326
|
-
const auditLogs = docsToUpdate.map((doc) => ({
|
|
327
|
-
monitorId: doc._id,
|
|
328
|
-
orgId: doc.sponsor,
|
|
329
|
-
timeUpdated: doc.updatedAt,
|
|
330
|
-
monitorProperties: doc.monitorProperties,
|
|
331
|
-
monitorState: doc.monitorState,
|
|
332
|
-
monitorLocation: {
|
|
333
|
-
type: "Point",
|
|
334
|
-
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
335
|
-
},
|
|
336
|
-
parameterThresholds: doc.parameterThresholds,
|
|
337
|
-
corrections: doc.corrections,
|
|
338
|
-
correctionHistory: doc.correctionHistory,
|
|
339
|
-
applyCorrections: doc.applyCorrections,
|
|
340
|
-
deletedAt: null, // Not a deletion, so this field is null
|
|
341
|
-
}));
|
|
342
|
-
|
|
343
|
-
await MonitorAudit.insertMany(auditLogs);
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
// Create the Monitors model
|
|
348
|
-
const Monitors = mongoose.model("Monitors", monitorsSchema);
|
|
349
|
-
|
|
350
|
-
export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
const parametersEnum = [
|
|
3
|
+
"NO2",
|
|
4
|
+
"SO2",
|
|
5
|
+
"PM2.5",
|
|
6
|
+
"PM10",
|
|
7
|
+
"Temperature",
|
|
8
|
+
"Humidity",
|
|
9
|
+
"OZONE",
|
|
10
|
+
"VOC",
|
|
11
|
+
"CO",
|
|
12
|
+
"NO",
|
|
13
|
+
"PM1",
|
|
14
|
+
"WS And Direction",
|
|
15
|
+
"DP",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const noteSchema = mongoose.Schema({
|
|
19
|
+
note: {
|
|
20
|
+
type: String,
|
|
21
|
+
required: true,
|
|
22
|
+
},
|
|
23
|
+
type: {
|
|
24
|
+
type: String,
|
|
25
|
+
required: true,
|
|
26
|
+
},
|
|
27
|
+
monitorState: {
|
|
28
|
+
type: String,
|
|
29
|
+
required: true,
|
|
30
|
+
},
|
|
31
|
+
adminId: {
|
|
32
|
+
type: mongoose.Types.ObjectId,
|
|
33
|
+
ref: "Admin",
|
|
34
|
+
required: true,
|
|
35
|
+
},
|
|
36
|
+
adminName: {
|
|
37
|
+
type: String,
|
|
38
|
+
},
|
|
39
|
+
date: {
|
|
40
|
+
type: Date,
|
|
41
|
+
default: Date.now,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const correctionSchema = mongoose.Schema(
|
|
46
|
+
{
|
|
47
|
+
equationType: {
|
|
48
|
+
type: String,
|
|
49
|
+
required: true,
|
|
50
|
+
},
|
|
51
|
+
variables: {
|
|
52
|
+
required: true,
|
|
53
|
+
type: Object,
|
|
54
|
+
},
|
|
55
|
+
dateCreated: {
|
|
56
|
+
type: Date,
|
|
57
|
+
default: Date.now,
|
|
58
|
+
},
|
|
59
|
+
applyCorrection: { type: Boolean, default: true },
|
|
60
|
+
},
|
|
61
|
+
{ _id: false } // no separate _id for sub-docs
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const correctionHistorySchema = mongoose.Schema(
|
|
65
|
+
{
|
|
66
|
+
pollutant: {
|
|
67
|
+
type: String,
|
|
68
|
+
required: true,
|
|
69
|
+
},
|
|
70
|
+
oldValue: {
|
|
71
|
+
type: correctionSchema,
|
|
72
|
+
default: undefined,
|
|
73
|
+
},
|
|
74
|
+
newValue: {
|
|
75
|
+
type: correctionSchema,
|
|
76
|
+
default: undefined,
|
|
77
|
+
},
|
|
78
|
+
changedAt: {
|
|
79
|
+
type: Date,
|
|
80
|
+
default: Date.now,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{ _id: false }
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Monitor Audit Schema
|
|
87
|
+
const monitorAuditSchema = mongoose.Schema(
|
|
88
|
+
{
|
|
89
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
90
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
91
|
+
timeUpdated: Date,
|
|
92
|
+
deletedAt: { type: Date, default: Date.now }, // Only populated on delete
|
|
93
|
+
monitorProperties: Object, // Stores properties of the monitor
|
|
94
|
+
monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
|
|
95
|
+
monitorLocation: {
|
|
96
|
+
// Monitor GPS location (lat, lon)
|
|
97
|
+
type: { type: String, enum: ["Point"], required: true },
|
|
98
|
+
coordinates: { type: [Number], required: true },
|
|
99
|
+
},
|
|
100
|
+
parameterThresholds: Object,
|
|
101
|
+
corrections: {
|
|
102
|
+
type: Map,
|
|
103
|
+
of: correctionSchema,
|
|
104
|
+
default: {},
|
|
105
|
+
},
|
|
106
|
+
correctionHistory: {
|
|
107
|
+
type: [correctionHistorySchema],
|
|
108
|
+
default: [],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
timestamps: true,
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
// Create the MonitorAudit model
|
|
117
|
+
const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
|
|
118
|
+
|
|
119
|
+
// Monitors Schema
|
|
120
|
+
const monitorsSchema = mongoose.Schema(
|
|
121
|
+
{
|
|
122
|
+
monitorCode: String,
|
|
123
|
+
monitorSupplier: {
|
|
124
|
+
type: String,
|
|
125
|
+
enum: [
|
|
126
|
+
"clarity",
|
|
127
|
+
"aeroqual",
|
|
128
|
+
"purple air",
|
|
129
|
+
"reference monitor",
|
|
130
|
+
"earthview",
|
|
131
|
+
"sensit",
|
|
132
|
+
"blue sky",
|
|
133
|
+
"aq mesh",
|
|
134
|
+
"quant aq",
|
|
135
|
+
"airGradient",
|
|
136
|
+
"oizom",
|
|
137
|
+
"metOne",
|
|
138
|
+
],
|
|
139
|
+
},
|
|
140
|
+
monitorType: String,
|
|
141
|
+
monitorIdFromSupplier: String,
|
|
142
|
+
measurementUpdate: Date,
|
|
143
|
+
monitorProperties: Object,
|
|
144
|
+
isPrivate: { type: Boolean, default: false, required: true },
|
|
145
|
+
monitorState: {
|
|
146
|
+
type: String,
|
|
147
|
+
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
148
|
+
},
|
|
149
|
+
monitorStateHistory: [Object],
|
|
150
|
+
monitorAlertStatus: {
|
|
151
|
+
type: String,
|
|
152
|
+
enum: [
|
|
153
|
+
"Good",
|
|
154
|
+
"Moderate",
|
|
155
|
+
"Unhealthy for SG",
|
|
156
|
+
"Unhealthy",
|
|
157
|
+
"Very Unhealthy",
|
|
158
|
+
"Hazardous",
|
|
159
|
+
"Bad",
|
|
160
|
+
],
|
|
161
|
+
default: "Good",
|
|
162
|
+
},
|
|
163
|
+
sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
164
|
+
sponsorName: String,
|
|
165
|
+
monitorLatitude: Number,
|
|
166
|
+
monitorLongitude: Number,
|
|
167
|
+
gpsLocation: {
|
|
168
|
+
type: { type: String, enum: ["Point"], required: true },
|
|
169
|
+
coordinates: { type: [Number], required: true },
|
|
170
|
+
},
|
|
171
|
+
location: Object,
|
|
172
|
+
context: [String],
|
|
173
|
+
colocationDate: Date,
|
|
174
|
+
deploymentDate: Date,
|
|
175
|
+
subscriptionDate: Date,
|
|
176
|
+
parameters: [
|
|
177
|
+
{
|
|
178
|
+
type: String,
|
|
179
|
+
enum: parametersEnum,
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
latestPM2_5: Number,
|
|
183
|
+
latestAQI_PM2_5: Number,
|
|
184
|
+
notes: [noteSchema],
|
|
185
|
+
calculatedAverages: [Object],
|
|
186
|
+
images: [String],
|
|
187
|
+
isActive: { type: Boolean, default: true },
|
|
188
|
+
parameterThresholds: Object,
|
|
189
|
+
completionPercentageThresholds: Object,
|
|
190
|
+
// A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
|
|
191
|
+
corrections: {
|
|
192
|
+
type: Map,
|
|
193
|
+
of: correctionSchema,
|
|
194
|
+
default: {},
|
|
195
|
+
},
|
|
196
|
+
correctionHistory: {
|
|
197
|
+
type: [correctionHistorySchema],
|
|
198
|
+
default: [],
|
|
199
|
+
},
|
|
200
|
+
applyCorrections: { type: Boolean, default: false },
|
|
201
|
+
pausedParameters: {
|
|
202
|
+
type: [{ parameter: String, timestamp: Date, isPaused: Boolean }],
|
|
203
|
+
default: [],
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
timestamps: true,
|
|
208
|
+
}
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
// Geographic queries - already exists
|
|
212
|
+
monitorsSchema.index({ gpsLocation: "2dsphere" });
|
|
213
|
+
|
|
214
|
+
// Sponsor-based queries
|
|
215
|
+
monitorsSchema.index({ sponsor: 1, isPrivate: 1, isActive: 1 });
|
|
216
|
+
|
|
217
|
+
// Location-based filtering
|
|
218
|
+
monitorsSchema.index({ "location.city": 1, "location.state": 1 });
|
|
219
|
+
monitorsSchema.index({ "location.neighborhood": 1 });
|
|
220
|
+
monitorsSchema.index({ "location.county": 1 });
|
|
221
|
+
|
|
222
|
+
// Query parameter filtering
|
|
223
|
+
monitorsSchema.index({ monitorSupplier: 1, monitorState: 1 });
|
|
224
|
+
monitorsSchema.index({ context: 1 });
|
|
225
|
+
monitorsSchema.index({ parameters: 1 });
|
|
226
|
+
|
|
227
|
+
// Monitor lookups by supplier
|
|
228
|
+
monitorsSchema.index({ sponsor: 1, monitorSupplier: 1 });
|
|
229
|
+
|
|
230
|
+
// Keep existing single-field indexes for backward compatibility
|
|
231
|
+
monitorsSchema.index({ monitorSupplier: 1 });
|
|
232
|
+
monitorsSchema.index({ monitorIdFromSupplier: 1 });
|
|
233
|
+
monitorsSchema.index({ monitorState: 1 });
|
|
234
|
+
|
|
235
|
+
// Pre-hook to log single document deletions
|
|
236
|
+
monitorsSchema.pre("findOneAndDelete", async function () {
|
|
237
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
238
|
+
if (docToDelete) {
|
|
239
|
+
console.log("Logging findOneAndDelete to monitor audit", docToDelete);
|
|
240
|
+
const auditLog = new MonitorAudit({
|
|
241
|
+
monitorId: docToDelete._id,
|
|
242
|
+
orgId: docToDelete.sponsor,
|
|
243
|
+
timeUpdated: docToDelete.updatedAt,
|
|
244
|
+
monitorProperties: docToDelete.monitorProperties,
|
|
245
|
+
monitorState: docToDelete.monitorState,
|
|
246
|
+
monitorLocation: {
|
|
247
|
+
type: "Point",
|
|
248
|
+
coordinates: [
|
|
249
|
+
docToDelete.monitorLongitude,
|
|
250
|
+
docToDelete.monitorLatitude,
|
|
251
|
+
],
|
|
252
|
+
},
|
|
253
|
+
parameterThresholds: docToDelete.parameterThresholds,
|
|
254
|
+
corrections: docToDelete.corrections,
|
|
255
|
+
correctionHistory: docToDelete.correctionHistory,
|
|
256
|
+
applyCorrections: docToDelete.applyCorrections,
|
|
257
|
+
deletedAt: new Date(),
|
|
258
|
+
});
|
|
259
|
+
await auditLog.save();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Pre-hook to log multiple document deletions
|
|
264
|
+
monitorsSchema.pre("deleteMany", async function () {
|
|
265
|
+
console.log("deleteMany pre-hook triggered for monitors");
|
|
266
|
+
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
267
|
+
|
|
268
|
+
if (docsToDelete.length) {
|
|
269
|
+
console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
|
|
270
|
+
const auditLogs = docsToDelete.map((doc) => ({
|
|
271
|
+
monitorId: doc._id,
|
|
272
|
+
orgId: doc.sponsor,
|
|
273
|
+
timeUpdated: doc.updatedAt,
|
|
274
|
+
monitorProperties: doc.monitorProperties,
|
|
275
|
+
monitorState: doc.monitorState,
|
|
276
|
+
monitorLocation: {
|
|
277
|
+
type: "Point",
|
|
278
|
+
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
279
|
+
},
|
|
280
|
+
parameterThresholds: doc.parameterThresholds,
|
|
281
|
+
corrections: doc.corrections,
|
|
282
|
+
correctionHistory: doc.correctionHistory,
|
|
283
|
+
applyCorrections: doc.applyCorrections,
|
|
284
|
+
deletedAt: new Date(),
|
|
285
|
+
}));
|
|
286
|
+
|
|
287
|
+
await MonitorAudit.insertMany(auditLogs);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Pre-hook to log a single document deletion (for deleteOne)
|
|
292
|
+
monitorsSchema.pre("deleteOne", async function () {
|
|
293
|
+
console.log("deleteOne pre-hook triggered for monitors");
|
|
294
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
295
|
+
|
|
296
|
+
if (docToDelete) {
|
|
297
|
+
console.log("Logging deleteOne to monitor audit", docToDelete);
|
|
298
|
+
const auditLog = new MonitorAudit({
|
|
299
|
+
monitorId: docToDelete._id,
|
|
300
|
+
orgId: docToDelete.sponsor,
|
|
301
|
+
timeUpdated: docToDelete.updatedAt,
|
|
302
|
+
monitorProperties: docToDelete.monitorProperties,
|
|
303
|
+
monitorState: docToDelete.monitorState,
|
|
304
|
+
monitorLocation: {
|
|
305
|
+
type: "Point",
|
|
306
|
+
coordinates: [
|
|
307
|
+
docToDelete.monitorLongitude,
|
|
308
|
+
docToDelete.monitorLatitude,
|
|
309
|
+
],
|
|
310
|
+
},
|
|
311
|
+
parameterThresholds: docToDelete.parameterThresholds,
|
|
312
|
+
corrections: docToDelete.corrections,
|
|
313
|
+
correctionHistory: docToDelete.correctionHistory,
|
|
314
|
+
applyCorrections: docToDelete.applyCorrections,
|
|
315
|
+
deletedAt: new Date(),
|
|
316
|
+
});
|
|
317
|
+
await auditLog.save();
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// Pre-hook to log multiple document updates
|
|
322
|
+
monitorsSchema.pre("updateMany", async function () {
|
|
323
|
+
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
324
|
+
if (docsToUpdate.length) {
|
|
325
|
+
console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
|
|
326
|
+
const auditLogs = docsToUpdate.map((doc) => ({
|
|
327
|
+
monitorId: doc._id,
|
|
328
|
+
orgId: doc.sponsor,
|
|
329
|
+
timeUpdated: doc.updatedAt,
|
|
330
|
+
monitorProperties: doc.monitorProperties,
|
|
331
|
+
monitorState: doc.monitorState,
|
|
332
|
+
monitorLocation: {
|
|
333
|
+
type: "Point",
|
|
334
|
+
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
335
|
+
},
|
|
336
|
+
parameterThresholds: doc.parameterThresholds,
|
|
337
|
+
corrections: doc.corrections,
|
|
338
|
+
correctionHistory: doc.correctionHistory,
|
|
339
|
+
applyCorrections: doc.applyCorrections,
|
|
340
|
+
deletedAt: null, // Not a deletion, so this field is null
|
|
341
|
+
}));
|
|
342
|
+
|
|
343
|
+
await MonitorAudit.insertMany(auditLogs);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// Create the Monitors model
|
|
348
|
+
const Monitors = mongoose.model("Monitors", monitorsSchema);
|
|
349
|
+
|
|
350
|
+
export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };
|