@justair/justair-library 4.5.2 → 4.5.3-beta

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,329 +1,328 @@
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
- // A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
189
- corrections: {
190
- type: Map,
191
- of: correctionSchema,
192
- default: {},
193
- },
194
- correctionHistory: {
195
- type: [correctionHistorySchema],
196
- default: [],
197
- },
198
- applyCorrections: { type: Boolean, default: false },
199
- pausedParameters: {
200
- type: [{ parameter: String, timestamp: Date, isPaused: Boolean }],
201
- default: [],
202
- },
203
- },
204
- {
205
- timestamps: true,
206
- }
207
- );
208
-
209
- monitorsSchema.index({ gpsLocation: "2dsphere" });
210
- monitorsSchema.index({ monitorSupplier: 1 });
211
- monitorsSchema.index({ monitorIdFromSupplier: 1 });
212
- monitorsSchema.index({ monitorState: 1 });
213
-
214
- // Pre-hook to log single document deletions
215
- monitorsSchema.pre("findOneAndDelete", async function () {
216
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
217
- if (docToDelete) {
218
- console.log("Logging findOneAndDelete to monitor audit", docToDelete);
219
- const auditLog = new MonitorAudit({
220
- monitorId: docToDelete._id,
221
- orgId: docToDelete.sponsor,
222
- timeUpdated: docToDelete.updatedAt,
223
- monitorProperties: docToDelete.monitorProperties,
224
- monitorState: docToDelete.monitorState,
225
- monitorLocation: {
226
- type: "Point",
227
- coordinates: [
228
- docToDelete.monitorLongitude,
229
- docToDelete.monitorLatitude,
230
- ],
231
- },
232
- parameterThresholds: docToDelete.parameterThresholds,
233
- corrections: docToDelete.corrections,
234
- correctionHistory: docToDelete.correctionHistory,
235
- applyCorrections: docToDelete.applyCorrections,
236
- deletedAt: new Date(),
237
- });
238
- await auditLog.save();
239
- }
240
- });
241
-
242
- // Pre-hook to log multiple document deletions
243
- monitorsSchema.pre("deleteMany", async function () {
244
- console.log("deleteMany pre-hook triggered for monitors");
245
- const docsToDelete = await this.model.find(this.getFilter()).lean();
246
-
247
- if (docsToDelete.length) {
248
- console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
249
- const auditLogs = docsToDelete.map((doc) => ({
250
- monitorId: doc._id,
251
- orgId: doc.sponsor,
252
- timeUpdated: doc.updatedAt,
253
- monitorProperties: doc.monitorProperties,
254
- monitorState: doc.monitorState,
255
- monitorLocation: {
256
- type: "Point",
257
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
258
- },
259
- parameterThresholds: doc.parameterThresholds,
260
- corrections: doc.corrections,
261
- correctionHistory: doc.correctionHistory,
262
- applyCorrections: doc.applyCorrections,
263
- deletedAt: new Date(),
264
- }));
265
-
266
- await MonitorAudit.insertMany(auditLogs);
267
- }
268
- });
269
-
270
- // Pre-hook to log a single document deletion (for deleteOne)
271
- monitorsSchema.pre("deleteOne", async function () {
272
- console.log("deleteOne pre-hook triggered for monitors");
273
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
274
-
275
- if (docToDelete) {
276
- console.log("Logging deleteOne to monitor audit", docToDelete);
277
- const auditLog = new MonitorAudit({
278
- monitorId: docToDelete._id,
279
- orgId: docToDelete.sponsor,
280
- timeUpdated: docToDelete.updatedAt,
281
- monitorProperties: docToDelete.monitorProperties,
282
- monitorState: docToDelete.monitorState,
283
- monitorLocation: {
284
- type: "Point",
285
- coordinates: [
286
- docToDelete.monitorLongitude,
287
- docToDelete.monitorLatitude,
288
- ],
289
- },
290
- parameterThresholds: docToDelete.parameterThresholds,
291
- corrections: docToDelete.corrections,
292
- correctionHistory: docToDelete.correctionHistory,
293
- applyCorrections: docToDelete.applyCorrections,
294
- deletedAt: new Date(),
295
- });
296
- await auditLog.save();
297
- }
298
- });
299
-
300
- // Pre-hook to log multiple document updates
301
- monitorsSchema.pre("updateMany", async function () {
302
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
303
- if (docsToUpdate.length) {
304
- console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
305
- const auditLogs = docsToUpdate.map((doc) => ({
306
- monitorId: doc._id,
307
- orgId: doc.sponsor,
308
- timeUpdated: doc.updatedAt,
309
- monitorProperties: doc.monitorProperties,
310
- monitorState: doc.monitorState,
311
- monitorLocation: {
312
- type: "Point",
313
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
314
- },
315
- parameterThresholds: doc.parameterThresholds,
316
- corrections: doc.corrections,
317
- correctionHistory: doc.correctionHistory,
318
- applyCorrections: doc.applyCorrections,
319
- deletedAt: null, // Not a deletion, so this field is null
320
- }));
321
-
322
- await MonitorAudit.insertMany(auditLogs);
323
- }
324
- });
325
-
326
- // Create the Monitors model
327
- const Monitors = mongoose.model("Monitors", monitorsSchema);
328
-
329
- 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
+ completionPercentageThresholds: {
101
+ warning: Number,
102
+ invalid: Number,
103
+ },
104
+ corrections: {
105
+ type: Map,
106
+ of: correctionSchema,
107
+ default: {},
108
+ },
109
+ correctionHistory: {
110
+ type: [correctionHistorySchema],
111
+ default: [],
112
+ },
113
+ },
114
+ {
115
+ timestamps: true,
116
+ }
117
+ );
118
+
119
+ // Create the MonitorAudit model
120
+ const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
121
+
122
+ // Monitors Schema
123
+ const monitorsSchema = mongoose.Schema(
124
+ {
125
+ monitorCode: String,
126
+ monitorSupplier: {
127
+ type: String,
128
+ enum: [
129
+ "clarity",
130
+ "aeroqual",
131
+ "purple air",
132
+ "reference monitor",
133
+ "earthview",
134
+ "sensit",
135
+ "blue sky",
136
+ "aq mesh",
137
+ "quant aq",
138
+ "airgradient"
139
+ ],
140
+ },
141
+ monitorType: String,
142
+ monitorIdFromSupplier: String,
143
+ measurementUpdate: Date,
144
+ monitorProperties: Object,
145
+ isPrivate: { type: Boolean, default: false, required: true },
146
+ monitorState: {
147
+ type: String,
148
+ enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
149
+ },
150
+ monitorStateHistory: [Object],
151
+ monitorAlertStatus: {
152
+ type: String,
153
+ enum: [
154
+ "Good",
155
+ "Moderate",
156
+ "Unhealthy for SG",
157
+ "Unhealthy",
158
+ "Very Unhealthy",
159
+ "Hazardous",
160
+ "Bad",
161
+ ],
162
+ default: "Good",
163
+ },
164
+ sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
165
+ sponsorName: String,
166
+ monitorLatitude: Number,
167
+ monitorLongitude: Number,
168
+ gpsLocation: {
169
+ type: { type: String, enum: ["Point"], required: true },
170
+ coordinates: { type: [Number], required: true },
171
+ },
172
+ location: Object,
173
+ context: [String],
174
+ colocationDate: Date,
175
+ deploymentDate: Date,
176
+ subscriptionDate: Date,
177
+ parameters: [
178
+ {
179
+ type: String,
180
+ enum: parametersEnum,
181
+ },
182
+ ],
183
+ latestPM2_5: Number,
184
+ latestAQI_PM2_5: Number,
185
+ notes: [noteSchema],
186
+ calculatedAverages: [Object],
187
+ images: [String],
188
+ isActive: { type: Boolean, default: true },
189
+ parameterThresholds: 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: { type: [{ parameter: String, timestamp: Date, isPaused: Boolean }], default: [] }
202
+ },
203
+ {
204
+ timestamps: true,
205
+ }
206
+ );
207
+
208
+ monitorsSchema.index({ gpsLocation: "2dsphere" });
209
+ monitorsSchema.index({ monitorSupplier: 1 });
210
+ monitorsSchema.index({ monitorIdFromSupplier: 1 });
211
+ monitorsSchema.index({ monitorState: 1 });
212
+
213
+ // Pre-hook to log single document deletions
214
+ monitorsSchema.pre("findOneAndDelete", async function () {
215
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
216
+ if (docToDelete) {
217
+ console.log("Logging findOneAndDelete to monitor audit", docToDelete);
218
+ const auditLog = new MonitorAudit({
219
+ monitorId: docToDelete._id,
220
+ orgId: docToDelete.sponsor,
221
+ timeUpdated: docToDelete.updatedAt,
222
+ monitorProperties: docToDelete.monitorProperties,
223
+ monitorState: docToDelete.monitorState,
224
+ monitorLocation: {
225
+ type: "Point",
226
+ coordinates: [
227
+ docToDelete.monitorLongitude,
228
+ docToDelete.monitorLatitude,
229
+ ],
230
+ },
231
+ parameterThresholds: docToDelete.parameterThresholds,
232
+ corrections: docToDelete.corrections,
233
+ correctionHistory: docToDelete.correctionHistory,
234
+ applyCorrections: docToDelete.applyCorrections,
235
+ deletedAt: new Date(),
236
+ });
237
+ await auditLog.save();
238
+ }
239
+ });
240
+
241
+ // Pre-hook to log multiple document deletions
242
+ monitorsSchema.pre("deleteMany", async function () {
243
+ console.log("deleteMany pre-hook triggered for monitors");
244
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
245
+
246
+ if (docsToDelete.length) {
247
+ console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
248
+ const auditLogs = docsToDelete.map((doc) => ({
249
+ monitorId: doc._id,
250
+ orgId: doc.sponsor,
251
+ timeUpdated: doc.updatedAt,
252
+ monitorProperties: doc.monitorProperties,
253
+ monitorState: doc.monitorState,
254
+ monitorLocation: {
255
+ type: "Point",
256
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
257
+ },
258
+ parameterThresholds: doc.parameterThresholds,
259
+ corrections: doc.corrections,
260
+ correctionHistory: doc.correctionHistory,
261
+ applyCorrections: doc.applyCorrections,
262
+ deletedAt: new Date(),
263
+ }));
264
+
265
+ await MonitorAudit.insertMany(auditLogs);
266
+ }
267
+ });
268
+
269
+ // Pre-hook to log a single document deletion (for deleteOne)
270
+ monitorsSchema.pre("deleteOne", async function () {
271
+ console.log("deleteOne pre-hook triggered for monitors");
272
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
273
+
274
+ if (docToDelete) {
275
+ console.log("Logging deleteOne to monitor audit", docToDelete);
276
+ const auditLog = new MonitorAudit({
277
+ monitorId: docToDelete._id,
278
+ orgId: docToDelete.sponsor,
279
+ timeUpdated: docToDelete.updatedAt,
280
+ monitorProperties: docToDelete.monitorProperties,
281
+ monitorState: docToDelete.monitorState,
282
+ monitorLocation: {
283
+ type: "Point",
284
+ coordinates: [
285
+ docToDelete.monitorLongitude,
286
+ docToDelete.monitorLatitude,
287
+ ],
288
+ },
289
+ parameterThresholds: docToDelete.parameterThresholds,
290
+ corrections: docToDelete.corrections,
291
+ correctionHistory: docToDelete.correctionHistory,
292
+ applyCorrections: docToDelete.applyCorrections,
293
+ deletedAt: new Date(),
294
+ });
295
+ await auditLog.save();
296
+ }
297
+ });
298
+
299
+ // Pre-hook to log multiple document updates
300
+ monitorsSchema.pre("updateMany", async function () {
301
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
302
+ if (docsToUpdate.length) {
303
+ console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
304
+ const auditLogs = docsToUpdate.map((doc) => ({
305
+ monitorId: doc._id,
306
+ orgId: doc.sponsor,
307
+ timeUpdated: doc.updatedAt,
308
+ monitorProperties: doc.monitorProperties,
309
+ monitorState: doc.monitorState,
310
+ monitorLocation: {
311
+ type: "Point",
312
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
313
+ },
314
+ parameterThresholds: doc.parameterThresholds,
315
+ corrections: doc.corrections,
316
+ correctionHistory: doc.correctionHistory,
317
+ applyCorrections: doc.applyCorrections,
318
+ deletedAt: null, // Not a deletion, so this field is null
319
+ }));
320
+
321
+ await MonitorAudit.insertMany(auditLogs);
322
+ }
323
+ });
324
+
325
+ // Create the Monitors model
326
+ const Monitors = mongoose.model("Monitors", monitorsSchema);
327
+
328
+ export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };