@justair/justair-library 4.5.3 → 4.6.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.
@@ -1,325 +1,330 @@
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
- ];
16
-
17
- const noteSchema = mongoose.Schema({
18
- note: {
19
- type: String,
20
- required: true,
21
- },
22
- type: {
23
- type: String,
24
- required: true,
25
- },
26
- monitorState: {
27
- type: String,
28
- required: true,
29
- },
30
- adminId: {
31
- type: mongoose.Types.ObjectId,
32
- ref: "Admin",
33
- required: true,
34
- },
35
- adminName: {
36
- type: String,
37
- },
38
- date: {
39
- type: Date,
40
- default: Date.now,
41
- },
42
- });
43
-
44
- const correctionSchema = mongoose.Schema(
45
- {
46
- equationType: {
47
- type: String,
48
- required: true,
49
- },
50
- variables: {
51
- required: true,
52
- type: Object,
53
- },
54
- dateCreated: {
55
- type: Date,
56
- default: Date.now,
57
- },
58
- applyCorrection: { type: Boolean, default: true },
59
- },
60
- { _id: false } // no separate _id for sub-docs
61
- );
62
-
63
- const correctionHistorySchema = mongoose.Schema(
64
- {
65
- pollutant: {
66
- type: String,
67
- required: true,
68
- },
69
- oldValue: {
70
- type: correctionSchema,
71
- default: undefined,
72
- },
73
- newValue: {
74
- type: correctionSchema,
75
- default: undefined,
76
- },
77
- changedAt: {
78
- type: Date,
79
- default: Date.now,
80
- },
81
- },
82
- { _id: false }
83
- );
84
-
85
- // Monitor Audit Schema
86
- const monitorAuditSchema = mongoose.Schema(
87
- {
88
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
89
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
90
- timeUpdated: Date,
91
- deletedAt: { type: Date, default: Date.now }, // Only populated on delete
92
- monitorProperties: Object, // Stores properties of the monitor
93
- monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
94
- monitorLocation: {
95
- // Monitor GPS location (lat, lon)
96
- type: { type: String, enum: ["Point"], required: true },
97
- coordinates: { type: [Number], required: true },
98
- },
99
- parameterThresholds: Object,
100
- corrections: {
101
- type: Map,
102
- of: correctionSchema,
103
- default: {},
104
- },
105
- correctionHistory: {
106
- type: [correctionHistorySchema],
107
- default: [],
108
- },
109
- },
110
- {
111
- timestamps: true,
112
- }
113
- );
114
-
115
- // Create the MonitorAudit model
116
- const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
117
-
118
- // Monitors Schema
119
- const monitorsSchema = mongoose.Schema(
120
- {
121
- monitorCode: String,
122
- monitorSupplier: {
123
- type: String,
124
- enum: [
125
- "clarity",
126
- "aeroqual",
127
- "purple air",
128
- "reference monitor",
129
- "earthview",
130
- "sensit",
131
- "blue sky",
132
- "aq mesh",
133
- "quant aq",
134
- "airgradient"
135
- ],
136
- },
137
- monitorType: String,
138
- monitorIdFromSupplier: String,
139
- measurementUpdate: Date,
140
- monitorProperties: Object,
141
- isPrivate: { type: Boolean, default: false, required: true },
142
- monitorState: {
143
- type: String,
144
- enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
145
- },
146
- monitorStateHistory: [Object],
147
- monitorAlertStatus: {
148
- type: String,
149
- enum: [
150
- "Good",
151
- "Moderate",
152
- "Unhealthy for SG",
153
- "Unhealthy",
154
- "Very Unhealthy",
155
- "Hazardous",
156
- "Bad",
157
- ],
158
- default: "Good",
159
- },
160
- sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
161
- sponsorName: String,
162
- monitorLatitude: Number,
163
- monitorLongitude: Number,
164
- gpsLocation: {
165
- type: { type: String, enum: ["Point"], required: true },
166
- coordinates: { type: [Number], required: true },
167
- },
168
- location: Object,
169
- context: [String],
170
- colocationDate: Date,
171
- deploymentDate: Date,
172
- subscriptionDate: Date,
173
- parameters: [
174
- {
175
- type: String,
176
- enum: parametersEnum,
177
- },
178
- ],
179
- latestPM2_5: Number,
180
- latestAQI_PM2_5: Number,
181
- notes: [noteSchema],
182
- calculatedAverages: [Object],
183
- images: [String],
184
- isActive: { type: Boolean, default: true },
185
- parameterThresholds: Object,
186
- completionPercentageThresholds: Object,
187
- // A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
188
- corrections: {
189
- type: Map,
190
- of: correctionSchema,
191
- default: {},
192
- },
193
- correctionHistory: {
194
- type: [correctionHistorySchema],
195
- default: [],
196
- },
197
- applyCorrections: { type: Boolean, default: false },
198
- pausedParameters: { type: [{ parameter: String, timestamp: Date, isPaused: Boolean }], default: [] }
199
- },
200
- {
201
- timestamps: true,
202
- }
203
- );
204
-
205
- monitorsSchema.index({ gpsLocation: "2dsphere" });
206
- monitorsSchema.index({ monitorSupplier: 1 });
207
- monitorsSchema.index({ monitorIdFromSupplier: 1 });
208
- monitorsSchema.index({ monitorState: 1 });
209
-
210
- // Pre-hook to log single document deletions
211
- monitorsSchema.pre("findOneAndDelete", async function () {
212
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
213
- if (docToDelete) {
214
- console.log("Logging findOneAndDelete to monitor audit", docToDelete);
215
- const auditLog = new MonitorAudit({
216
- monitorId: docToDelete._id,
217
- orgId: docToDelete.sponsor,
218
- timeUpdated: docToDelete.updatedAt,
219
- monitorProperties: docToDelete.monitorProperties,
220
- monitorState: docToDelete.monitorState,
221
- monitorLocation: {
222
- type: "Point",
223
- coordinates: [
224
- docToDelete.monitorLongitude,
225
- docToDelete.monitorLatitude,
226
- ],
227
- },
228
- parameterThresholds: docToDelete.parameterThresholds,
229
- corrections: docToDelete.corrections,
230
- correctionHistory: docToDelete.correctionHistory,
231
- applyCorrections: docToDelete.applyCorrections,
232
- deletedAt: new Date(),
233
- });
234
- await auditLog.save();
235
- }
236
- });
237
-
238
- // Pre-hook to log multiple document deletions
239
- monitorsSchema.pre("deleteMany", async function () {
240
- console.log("deleteMany pre-hook triggered for monitors");
241
- const docsToDelete = await this.model.find(this.getFilter()).lean();
242
-
243
- if (docsToDelete.length) {
244
- console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
245
- const auditLogs = docsToDelete.map((doc) => ({
246
- monitorId: doc._id,
247
- orgId: doc.sponsor,
248
- timeUpdated: doc.updatedAt,
249
- monitorProperties: doc.monitorProperties,
250
- monitorState: doc.monitorState,
251
- monitorLocation: {
252
- type: "Point",
253
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
254
- },
255
- parameterThresholds: doc.parameterThresholds,
256
- corrections: doc.corrections,
257
- correctionHistory: doc.correctionHistory,
258
- applyCorrections: doc.applyCorrections,
259
- deletedAt: new Date(),
260
- }));
261
-
262
- await MonitorAudit.insertMany(auditLogs);
263
- }
264
- });
265
-
266
- // Pre-hook to log a single document deletion (for deleteOne)
267
- monitorsSchema.pre("deleteOne", async function () {
268
- console.log("deleteOne pre-hook triggered for monitors");
269
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
270
-
271
- if (docToDelete) {
272
- console.log("Logging deleteOne to monitor audit", docToDelete);
273
- const auditLog = new MonitorAudit({
274
- monitorId: docToDelete._id,
275
- orgId: docToDelete.sponsor,
276
- timeUpdated: docToDelete.updatedAt,
277
- monitorProperties: docToDelete.monitorProperties,
278
- monitorState: docToDelete.monitorState,
279
- monitorLocation: {
280
- type: "Point",
281
- coordinates: [
282
- docToDelete.monitorLongitude,
283
- docToDelete.monitorLatitude,
284
- ],
285
- },
286
- parameterThresholds: docToDelete.parameterThresholds,
287
- corrections: docToDelete.corrections,
288
- correctionHistory: docToDelete.correctionHistory,
289
- applyCorrections: docToDelete.applyCorrections,
290
- deletedAt: new Date(),
291
- });
292
- await auditLog.save();
293
- }
294
- });
295
-
296
- // Pre-hook to log multiple document updates
297
- monitorsSchema.pre("updateMany", async function () {
298
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
299
- if (docsToUpdate.length) {
300
- console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
301
- const auditLogs = docsToUpdate.map((doc) => ({
302
- monitorId: doc._id,
303
- orgId: doc.sponsor,
304
- timeUpdated: doc.updatedAt,
305
- monitorProperties: doc.monitorProperties,
306
- monitorState: doc.monitorState,
307
- monitorLocation: {
308
- type: "Point",
309
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
310
- },
311
- parameterThresholds: doc.parameterThresholds,
312
- corrections: doc.corrections,
313
- correctionHistory: doc.correctionHistory,
314
- applyCorrections: doc.applyCorrections,
315
- deletedAt: null, // Not a deletion, so this field is null
316
- }));
317
-
318
- await MonitorAudit.insertMany(auditLogs);
319
- }
320
- });
321
-
322
- // Create the Monitors model
323
- const Monitors = mongoose.model("Monitors", monitorsSchema);
324
-
325
- 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
+ ];
16
+
17
+ const noteSchema = mongoose.Schema({
18
+ note: {
19
+ type: String,
20
+ required: true,
21
+ },
22
+ type: {
23
+ type: String,
24
+ required: true,
25
+ },
26
+ monitorState: {
27
+ type: String,
28
+ required: true,
29
+ },
30
+ adminId: {
31
+ type: mongoose.Types.ObjectId,
32
+ ref: "Admin",
33
+ required: true,
34
+ },
35
+ adminName: {
36
+ type: String,
37
+ },
38
+ date: {
39
+ type: Date,
40
+ default: Date.now,
41
+ },
42
+ });
43
+
44
+ const correctionSchema = mongoose.Schema(
45
+ {
46
+ equationType: {
47
+ type: String,
48
+ required: true,
49
+ },
50
+ variables: {
51
+ required: true,
52
+ type: Object,
53
+ },
54
+ dateCreated: {
55
+ type: Date,
56
+ default: Date.now,
57
+ },
58
+ applyCorrection: { type: Boolean, default: true },
59
+ },
60
+ { _id: false } // no separate _id for sub-docs
61
+ );
62
+
63
+ const correctionHistorySchema = mongoose.Schema(
64
+ {
65
+ pollutant: {
66
+ type: String,
67
+ required: true,
68
+ },
69
+ oldValue: {
70
+ type: correctionSchema,
71
+ default: undefined,
72
+ },
73
+ newValue: {
74
+ type: correctionSchema,
75
+ default: undefined,
76
+ },
77
+ changedAt: {
78
+ type: Date,
79
+ default: Date.now,
80
+ },
81
+ },
82
+ { _id: false }
83
+ );
84
+
85
+ // Monitor Audit Schema
86
+ const monitorAuditSchema = mongoose.Schema(
87
+ {
88
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
89
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
90
+ timeUpdated: Date,
91
+ deletedAt: { type: Date, default: Date.now }, // Only populated on delete
92
+ monitorProperties: Object, // Stores properties of the monitor
93
+ monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
94
+ monitorLocation: {
95
+ // Monitor GPS location (lat, lon)
96
+ type: { type: String, enum: ["Point"], required: true },
97
+ coordinates: { type: [Number], required: true },
98
+ },
99
+ parameterThresholds: Object,
100
+ corrections: {
101
+ type: Map,
102
+ of: correctionSchema,
103
+ default: {},
104
+ },
105
+ correctionHistory: {
106
+ type: [correctionHistorySchema],
107
+ default: [],
108
+ },
109
+ },
110
+ {
111
+ timestamps: true,
112
+ }
113
+ );
114
+
115
+ // Create the MonitorAudit model
116
+ const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
117
+
118
+ // Monitors Schema
119
+ const monitorsSchema = mongoose.Schema(
120
+ {
121
+ monitorCode: String,
122
+ monitorSupplier: {
123
+ type: String,
124
+ enum: [
125
+ "clarity",
126
+ "aeroqual",
127
+ "purple air",
128
+ "reference monitor",
129
+ "earthview",
130
+ "sensit",
131
+ "blue sky",
132
+ "aq mesh",
133
+ "quant aq",
134
+ "airGradient",
135
+ "oizom",
136
+ "metOne",
137
+ ],
138
+ },
139
+ monitorType: String,
140
+ monitorIdFromSupplier: String,
141
+ measurementUpdate: Date,
142
+ monitorProperties: Object,
143
+ isPrivate: { type: Boolean, default: false, required: true },
144
+ monitorState: {
145
+ type: String,
146
+ enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
147
+ },
148
+ monitorStateHistory: [Object],
149
+ monitorAlertStatus: {
150
+ type: String,
151
+ enum: [
152
+ "Good",
153
+ "Moderate",
154
+ "Unhealthy for SG",
155
+ "Unhealthy",
156
+ "Very Unhealthy",
157
+ "Hazardous",
158
+ "Bad",
159
+ ],
160
+ default: "Good",
161
+ },
162
+ sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
163
+ sponsorName: String,
164
+ monitorLatitude: Number,
165
+ monitorLongitude: Number,
166
+ gpsLocation: {
167
+ type: { type: String, enum: ["Point"], required: true },
168
+ coordinates: { type: [Number], required: true },
169
+ },
170
+ location: Object,
171
+ context: [String],
172
+ colocationDate: Date,
173
+ deploymentDate: Date,
174
+ subscriptionDate: Date,
175
+ parameters: [
176
+ {
177
+ type: String,
178
+ enum: parametersEnum,
179
+ },
180
+ ],
181
+ latestPM2_5: Number,
182
+ latestAQI_PM2_5: Number,
183
+ notes: [noteSchema],
184
+ calculatedAverages: [Object],
185
+ images: [String],
186
+ isActive: { type: Boolean, default: true },
187
+ parameterThresholds: Object,
188
+ completionPercentageThresholds: Object,
189
+ // A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
190
+ corrections: {
191
+ type: Map,
192
+ of: correctionSchema,
193
+ default: {},
194
+ },
195
+ correctionHistory: {
196
+ type: [correctionHistorySchema],
197
+ default: [],
198
+ },
199
+ applyCorrections: { type: Boolean, default: false },
200
+ pausedParameters: {
201
+ type: [{ parameter: String, timestamp: Date, isPaused: Boolean }],
202
+ default: [],
203
+ },
204
+ },
205
+ {
206
+ timestamps: true,
207
+ }
208
+ );
209
+
210
+ monitorsSchema.index({ gpsLocation: "2dsphere" });
211
+ monitorsSchema.index({ monitorSupplier: 1 });
212
+ monitorsSchema.index({ monitorIdFromSupplier: 1 });
213
+ monitorsSchema.index({ monitorState: 1 });
214
+
215
+ // Pre-hook to log single document deletions
216
+ monitorsSchema.pre("findOneAndDelete", async function () {
217
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
218
+ if (docToDelete) {
219
+ console.log("Logging findOneAndDelete to monitor audit", docToDelete);
220
+ const auditLog = new MonitorAudit({
221
+ monitorId: docToDelete._id,
222
+ orgId: docToDelete.sponsor,
223
+ timeUpdated: docToDelete.updatedAt,
224
+ monitorProperties: docToDelete.monitorProperties,
225
+ monitorState: docToDelete.monitorState,
226
+ monitorLocation: {
227
+ type: "Point",
228
+ coordinates: [
229
+ docToDelete.monitorLongitude,
230
+ docToDelete.monitorLatitude,
231
+ ],
232
+ },
233
+ parameterThresholds: docToDelete.parameterThresholds,
234
+ corrections: docToDelete.corrections,
235
+ correctionHistory: docToDelete.correctionHistory,
236
+ applyCorrections: docToDelete.applyCorrections,
237
+ deletedAt: new Date(),
238
+ });
239
+ await auditLog.save();
240
+ }
241
+ });
242
+
243
+ // Pre-hook to log multiple document deletions
244
+ monitorsSchema.pre("deleteMany", async function () {
245
+ console.log("deleteMany pre-hook triggered for monitors");
246
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
247
+
248
+ if (docsToDelete.length) {
249
+ console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
250
+ const auditLogs = docsToDelete.map((doc) => ({
251
+ monitorId: doc._id,
252
+ orgId: doc.sponsor,
253
+ timeUpdated: doc.updatedAt,
254
+ monitorProperties: doc.monitorProperties,
255
+ monitorState: doc.monitorState,
256
+ monitorLocation: {
257
+ type: "Point",
258
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
259
+ },
260
+ parameterThresholds: doc.parameterThresholds,
261
+ corrections: doc.corrections,
262
+ correctionHistory: doc.correctionHistory,
263
+ applyCorrections: doc.applyCorrections,
264
+ deletedAt: new Date(),
265
+ }));
266
+
267
+ await MonitorAudit.insertMany(auditLogs);
268
+ }
269
+ });
270
+
271
+ // Pre-hook to log a single document deletion (for deleteOne)
272
+ monitorsSchema.pre("deleteOne", async function () {
273
+ console.log("deleteOne pre-hook triggered for monitors");
274
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
275
+
276
+ if (docToDelete) {
277
+ console.log("Logging deleteOne to monitor audit", docToDelete);
278
+ const auditLog = new MonitorAudit({
279
+ monitorId: docToDelete._id,
280
+ orgId: docToDelete.sponsor,
281
+ timeUpdated: docToDelete.updatedAt,
282
+ monitorProperties: docToDelete.monitorProperties,
283
+ monitorState: docToDelete.monitorState,
284
+ monitorLocation: {
285
+ type: "Point",
286
+ coordinates: [
287
+ docToDelete.monitorLongitude,
288
+ docToDelete.monitorLatitude,
289
+ ],
290
+ },
291
+ parameterThresholds: docToDelete.parameterThresholds,
292
+ corrections: docToDelete.corrections,
293
+ correctionHistory: docToDelete.correctionHistory,
294
+ applyCorrections: docToDelete.applyCorrections,
295
+ deletedAt: new Date(),
296
+ });
297
+ await auditLog.save();
298
+ }
299
+ });
300
+
301
+ // Pre-hook to log multiple document updates
302
+ monitorsSchema.pre("updateMany", async function () {
303
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
304
+ if (docsToUpdate.length) {
305
+ console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
306
+ const auditLogs = docsToUpdate.map((doc) => ({
307
+ monitorId: doc._id,
308
+ orgId: doc.sponsor,
309
+ timeUpdated: doc.updatedAt,
310
+ monitorProperties: doc.monitorProperties,
311
+ monitorState: doc.monitorState,
312
+ monitorLocation: {
313
+ type: "Point",
314
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
315
+ },
316
+ parameterThresholds: doc.parameterThresholds,
317
+ corrections: doc.corrections,
318
+ correctionHistory: doc.correctionHistory,
319
+ applyCorrections: doc.applyCorrections,
320
+ deletedAt: null, // Not a deletion, so this field is null
321
+ }));
322
+
323
+ await MonitorAudit.insertMany(auditLogs);
324
+ }
325
+ });
326
+
327
+ // Create the Monitors model
328
+ const Monitors = mongoose.model("Monitors", monitorsSchema);
329
+
330
+ export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };