@justair/justair-library 4.0.0-alpha → 4.1.0-alpha

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justair/justair-library",
3
- "version": "4.0.0-alpha",
3
+ "version": "4.1.0-alpha",
4
4
  "description": "JustAir Internal Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1,5 +1,13 @@
1
1
  import mongoose from "mongoose";
2
2
 
3
+ const correctionSnapshotSchema = new mongoose.Schema(
4
+ {
5
+ equationType: String,
6
+ dateCreated: Date,
7
+ },
8
+ { _id: false } // Not generating separate _id for each correction
9
+ );
10
+
3
11
  // Audit Schema
4
12
  const auditSchema = mongoose.Schema(
5
13
  {
@@ -10,6 +18,13 @@ const auditSchema = mongoose.Schema(
10
18
  measurements: Object,
11
19
  monitorState: String,
12
20
  normalizedMeasurements: Object,
21
+ flags: Object,
22
+ appliedCorrections: {
23
+ type: Map,
24
+ of: correctionSnapshotSchema,
25
+ default: {},
26
+ },
27
+ correctedNormalizedMeasurements: Object,
13
28
  },
14
29
  {
15
30
  timestamps: true,
@@ -27,6 +42,13 @@ const measurementsSchema = mongoose.Schema(
27
42
  measurements: Object,
28
43
  monitorState: String,
29
44
  normalizedMeasurements: Object,
45
+ flags: Object,
46
+ appliedCorrections: {
47
+ type: Map,
48
+ of: correctionSnapshotSchema,
49
+ default: {},
50
+ },
51
+ correctedNormalizedMeasurements: Object,
30
52
  },
31
53
  {
32
54
  timestamps: true,
@@ -47,6 +69,11 @@ measurementsSchema.pre("findOneAndDelete", async function () {
47
69
  timeUpdated: docToDelete.timeUpdated,
48
70
  measurements: docToDelete.measurements,
49
71
  monitorState: docToDelete.monitorState,
72
+ normalizedMeasurements: docToDelete.normalizedMeasurements,
73
+ flags: docToDelete.flags,
74
+ appliedCorrections: docToDelete.appliedCorrections,
75
+ correctedNormalizedMeasurements:
76
+ docToDelete.correctedNormalizedMeasurements,
50
77
  deletedAt: new Date(),
51
78
  });
52
79
  await auditLog.save();
@@ -66,6 +93,10 @@ measurementsSchema.pre("deleteMany", async function () {
66
93
  timeUpdated: doc.timeUpdated,
67
94
  measurements: doc.measurements,
68
95
  monitorState: doc.monitorState,
96
+ normalizedMeasurements: doc.normalizedMeasurements,
97
+ flags: doc.flags,
98
+ appliedCorrections: doc.appliedCorrections,
99
+ correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
69
100
  deletedAt: new Date(),
70
101
  }));
71
102
 
@@ -86,6 +117,11 @@ measurementsSchema.pre("deleteOne", async function () {
86
117
  timeUpdated: docToDelete.timeUpdated,
87
118
  measurements: docToDelete.measurements,
88
119
  monitorState: docToDelete.monitorState,
120
+ normalizedMeasurements: docToDelete.normalizedMeasurements,
121
+ flags: docToDelete.flags,
122
+ appliedCorrections: docToDelete.appliedCorrections,
123
+ correctedNormalizedMeasurements:
124
+ docToDelete.correctedNormalizedMeasurements,
89
125
  deletedAt: new Date(),
90
126
  });
91
127
  await auditLog.save();
@@ -103,6 +139,11 @@ measurementsSchema.pre("findOneAndUpdate", async function () {
103
139
  timeUpdated: docToUpdate.timeUpdated,
104
140
  measurements: docToUpdate.measurements,
105
141
  monitorState: docToUpdate.monitorState,
142
+ normalizedMeasurements: docToUpdate.normalizedMeasurements,
143
+ flags: docToUpdate.flags,
144
+ appliedCorrections: docToUpdate.appliedCorrections,
145
+ correctedNormalizedMeasurements:
146
+ docToUpdate.correctedNormalizedMeasurements,
106
147
  deletedAt: null, // No deletion happening, so set it to null or undefined
107
148
  });
108
149
  await auditLog.save();
@@ -120,6 +161,10 @@ measurementsSchema.pre("updateMany", async function () {
120
161
  timeUpdated: doc.timeUpdated,
121
162
  measurements: doc.measurements,
122
163
  monitorState: doc.monitorState,
164
+ normalizedMeasurements: doc.normalizedMeasurements,
165
+ flags: doc.flags,
166
+ appliedCorrections: doc.appliedCorrections,
167
+ correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
123
168
  deletedAt: null, // No deletion happening
124
169
  }));
125
170
 
@@ -28,6 +28,17 @@ const monitorAuditSchema = mongoose.Schema(
28
28
  type: { type: String, enum: ["Point"], required: true },
29
29
  coordinates: { type: [Number], required: true },
30
30
  },
31
+ parameterThresholds: Object,
32
+ corrections: {
33
+ type: Map,
34
+ of: correctionSchema,
35
+ default: {},
36
+ },
37
+ correctionHistory: {
38
+ type: [correctionHistorySchema],
39
+ default: [],
40
+ },
41
+ applyCorrections: { type: Boolean, default: false },
31
42
  },
32
43
  {
33
44
  timestamps: true,
@@ -37,6 +48,46 @@ const monitorAuditSchema = mongoose.Schema(
37
48
  // Create the MonitorAudit model
38
49
  const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
39
50
 
51
+ const correctionSchema = mongoose.Schema(
52
+ {
53
+ equationType: {
54
+ type: String,
55
+ required: true,
56
+ },
57
+ variables: {
58
+ required: true,
59
+ type: Object,
60
+ },
61
+ dateCreated: {
62
+ type: Date,
63
+ default: Date.now,
64
+ },
65
+ },
66
+ { _id: false } // no separate _id for sub-docs
67
+ );
68
+
69
+ const correctionHistorySchema = mongoose.Schema(
70
+ {
71
+ pollutant: {
72
+ type: String,
73
+ required: true,
74
+ },
75
+ oldValue: {
76
+ type: correctionSchema,
77
+ default: undefined,
78
+ },
79
+ newValue: {
80
+ type: correctionSchema,
81
+ default: undefined,
82
+ },
83
+ changedAt: {
84
+ type: Date,
85
+ default: Date.now,
86
+ },
87
+ },
88
+ { _id: false }
89
+ );
90
+
40
91
  // Monitors Schema
41
92
  const monitorsSchema = mongoose.Schema(
42
93
  {
@@ -104,6 +155,17 @@ const monitorsSchema = mongoose.Schema(
104
155
  images: [String],
105
156
  isActive: { type: Boolean, default: true },
106
157
  parameterThresholds: Object,
158
+ // A Map, keyed by pollutant (e.g. "PM2_5"), storing Correction sub-docs
159
+ corrections: {
160
+ type: Map,
161
+ of: correctionSchema,
162
+ default: {},
163
+ },
164
+ correctionHistory: {
165
+ type: [correctionHistorySchema],
166
+ default: [],
167
+ },
168
+ applyCorrections: { type: Boolean, default: false },
107
169
  },
108
170
  {
109
171
  timestamps: true,
@@ -133,6 +195,10 @@ monitorsSchema.pre("findOneAndDelete", async function () {
133
195
  docToDelete.monitorLatitude,
134
196
  ],
135
197
  },
198
+ parameterThresholds: docToDelete.parameterThresholds,
199
+ corrections: docToDelete.corrections,
200
+ correctionHistory: docToDelete.correctionHistory,
201
+ applyCorrections: docToDelete.applyCorrections,
136
202
  deletedAt: new Date(),
137
203
  });
138
204
  await auditLog.save();
@@ -156,6 +222,10 @@ monitorsSchema.pre("deleteMany", async function () {
156
222
  type: "Point",
157
223
  coordinates: [doc.monitorLongitude, doc.monitorLatitude],
158
224
  },
225
+ parameterThresholds: doc.parameterThresholds,
226
+ corrections: doc.corrections,
227
+ correctionHistory: doc.correctionHistory,
228
+ applyCorrections: doc.applyCorrections,
159
229
  deletedAt: new Date(),
160
230
  }));
161
231
 
@@ -183,6 +253,10 @@ monitorsSchema.pre("deleteOne", async function () {
183
253
  docToDelete.monitorLatitude,
184
254
  ],
185
255
  },
256
+ parameterThresholds: docToDelete.parameterThresholds,
257
+ corrections: docToDelete.corrections,
258
+ correctionHistory: docToDelete.correctionHistory,
259
+ applyCorrections: docToDelete.applyCorrections,
186
260
  deletedAt: new Date(),
187
261
  });
188
262
  await auditLog.save();
@@ -204,6 +278,10 @@ monitorsSchema.pre("updateMany", async function () {
204
278
  type: "Point",
205
279
  coordinates: [doc.monitorLongitude, doc.monitorLatitude],
206
280
  },
281
+ parameterThresholds: doc.parameterThresholds,
282
+ corrections: doc.corrections,
283
+ correctionHistory: doc.correctionHistory,
284
+ applyCorrections: doc.applyCorrections,
207
285
  deletedAt: null, // Not a deletion, so this field is null
208
286
  }));
209
287
 
@@ -1,6 +1,26 @@
1
- import mongoose from "mongoose";
1
+ import mongoose, { Schema } from "mongoose";
2
2
 
3
- const organizationsSchema = mongoose.Schema({
3
+ const correctionSchema = new Schema(
4
+ {
5
+ correctionName: String,
6
+ equationType: {
7
+ type: String,
8
+ required: true,
9
+ },
10
+ variables: {
11
+ type: Object,
12
+ required: true,
13
+ },
14
+ dateCreated: {
15
+ type: Date,
16
+ default: Date.now,
17
+ },
18
+ },
19
+ { _id: false }
20
+ );
21
+
22
+ const organizationsSchema = mongoose.Schema(
23
+ {
4
24
  name: String,
5
25
  website: String,
6
26
  location: Object,
@@ -10,15 +30,17 @@ const organizationsSchema = mongoose.Schema({
10
30
  orgDescription: String,
11
31
  orgLogo: String,
12
32
  customAlertLevels: [Object],
13
- connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
33
+ connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
14
34
  communityMessages: [Object],
15
35
  weeklyReportData: [Object],
16
- isActive: { type: Boolean, default: true }
17
- },
18
- {
19
- timestamps: true
20
- });
36
+ isActive: { type: Boolean, default: true },
37
+ correctionRules: [correctionSchema],
38
+ },
39
+ {
40
+ timestamps: true,
41
+ }
42
+ );
21
43
 
22
- const Organizations = mongoose.model('Organizations', organizationsSchema);
44
+ const Organizations = mongoose.model("Organizations", organizationsSchema);
23
45
 
24
- export {organizationsSchema, Organizations};
46
+ export { organizationsSchema, Organizations };