@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,240 +1,246 @@
1
- import mongoose from "mongoose";
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
-
11
- // Annotation Schema
12
- const annotationSchema = new mongoose.Schema(
13
- {
14
- measurementIdentifier: {
15
- type: String,
16
- required: true,
17
- trim: true,
18
- // Examples: "PM2_5", "PM10", "NO2", "O3", etc.
19
- },
20
- comment: {
21
- type: String,
22
- required: true,
23
- trim: true,
24
- },
25
- adminId: {
26
- type: mongoose.Types.ObjectId,
27
- ref: "Admin",
28
- required: true,
29
- },
30
- timestamp: {
31
- type: Date,
32
- default: Date.now,
33
- required: true,
34
- },
35
- },
36
- {
37
- _id: true,
38
- timestamps: false,
39
- }
40
- );
41
-
42
- // Add index for efficient querying
43
- annotationSchema.index({ measurementIdentifier: 1, timestamp: -1 });
44
-
45
- // Audit Schema
46
- const auditSchema = mongoose.Schema(
47
- {
48
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
49
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
50
- timeUpdated: Date,
51
- deletedAt: { type: Date, default: Date.now },
52
- measurements: Object,
53
- monitorState: String,
54
- monitorSupplier: String,
55
- normalizedMeasurements: Object,
56
- flags: Object,
57
- appliedCorrections: {
58
- type: Map,
59
- of: correctionSnapshotSchema,
60
- default: {},
61
- },
62
- correctedNormalizedMeasurements: Object,
63
- annotations: [annotationSchema], // Include annotations in audit trail
64
- },
65
- {
66
- timestamps: true,
67
- }
68
- );
69
-
70
- const Audit = mongoose.model("Audit", auditSchema);
71
-
72
- // Measurements Schema
73
- const measurementsSchema = mongoose.Schema(
74
- {
75
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
76
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
77
- timeUpdated: Date,
78
- measurements: Object,
79
- monitorState: String,
80
- monitorSupplier: String,
81
- normalizedMeasurements: Object,
82
- flags: Object,
83
- appliedCorrections: {
84
- type: Map,
85
- of: correctionSnapshotSchema,
86
- default: {},
87
- },
88
- correctedNormalizedMeasurements: Object,
89
- annotations: {
90
- type: [annotationSchema],
91
- default: [],
92
- },
93
- },
94
- {
95
- timestamps: true,
96
- }
97
- );
98
-
99
- measurementsSchema.index({ monitorId: 1 });
100
- measurementsSchema.index({ timeUpdated: 1 });
101
-
102
- measurementsSchema.index({
103
- "annotations.measurementIdentifier": 1,
104
- "annotations.timestamp": -1,
105
- });
106
- measurementsSchema.index({ "annotations.userId": 1 });
107
-
108
- // Pre-hook to log single document deletions
109
- measurementsSchema.pre("findOneAndDelete", async function () {
110
- const docToDelete = await this.model.findOne(this.getFilter());
111
- if (docToDelete) {
112
- console.log("Logging findOneAndDelete to audit", docToDelete);
113
- const auditLog = new Audit({
114
- monitorId: docToDelete.monitorId,
115
- orgId: docToDelete.orgId,
116
- timeUpdated: docToDelete.timeUpdated,
117
- measurements: docToDelete.measurements,
118
- monitorState: docToDelete.monitorState,
119
- monitorSupplier: docToDelete.monitorSupplier,
120
- normalizedMeasurements: docToDelete.normalizedMeasurements,
121
- flags: docToDelete.flags,
122
- appliedCorrections: docToDelete.appliedCorrections,
123
- correctedNormalizedMeasurements:
124
- docToDelete.correctedNormalizedMeasurements,
125
- annotations: docToDelete.annotations, // Include annotations in audit
126
- deletedAt: new Date(),
127
- });
128
- await auditLog.save();
129
- }
130
- });
131
-
132
- // Pre-hook to log multiple document deletions
133
- measurementsSchema.pre("deleteMany", async function () {
134
- console.log("deleteMany pre-hook triggered");
135
- const docsToDelete = await this.model.find(this.getFilter()).lean();
136
-
137
- if (docsToDelete.length) {
138
- console.log(`Logging ${docsToDelete.length} documents to audit`);
139
- const auditLogs = docsToDelete.map((doc) => ({
140
- monitorId: doc.monitorId,
141
- orgId: doc.orgId,
142
- timeUpdated: doc.timeUpdated,
143
- measurements: doc.measurements,
144
- monitorState: doc.monitorState,
145
- monitorSupplier: doc.monitorSupplier,
146
- normalizedMeasurements: doc.normalizedMeasurements,
147
- flags: doc.flags,
148
- appliedCorrections: doc.appliedCorrections,
149
- correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
150
- annotations: doc.annotations, // Include annotations in audit
151
- deletedAt: new Date(),
152
- }));
153
-
154
- await Audit.insertMany(auditLogs);
155
- }
156
- });
157
-
158
- // Pre-hook to log a single document deletion (for deleteOne)
159
- measurementsSchema.pre("deleteOne", async function () {
160
- console.log("deleteOne pre-hook triggered");
161
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
162
-
163
- if (docToDelete) {
164
- console.log("Logging deleteOne to audit", docToDelete);
165
- const auditLog = new Audit({
166
- monitorId: docToDelete.monitorId,
167
- orgId: docToDelete.orgId,
168
- timeUpdated: docToDelete.timeUpdated,
169
- measurements: docToDelete.measurements,
170
- monitorState: docToDelete.monitorState,
171
- monitorSupplier: docToDelete.monitorSupplier,
172
- normalizedMeasurements: docToDelete.normalizedMeasurements,
173
- flags: docToDelete.flags,
174
- appliedCorrections: docToDelete.appliedCorrections,
175
- correctedNormalizedMeasurements:
176
- docToDelete.correctedNormalizedMeasurements,
177
- annotations: docToDelete.annotations, // Include annotations in audit
178
- deletedAt: new Date(),
179
- });
180
- await auditLog.save();
181
- }
182
- });
183
-
184
- // Pre-hook to log single document updates
185
- measurementsSchema.pre("findOneAndUpdate", async function () {
186
- const docToUpdate = await this.model.findOne(this.getFilter()).lean();
187
- if (docToUpdate) {
188
- console.log("Logging findOneAndUpdate to audit", docToUpdate);
189
- const auditLog = new Audit({
190
- monitorId: docToUpdate.monitorId,
191
- orgId: docToUpdate.orgId,
192
- timeUpdated: docToUpdate.timeUpdated,
193
- measurements: docToUpdate.measurements,
194
- monitorState: docToUpdate.monitorState,
195
- monitorSupplier: docToUpdate.monitorSupplier,
196
- normalizedMeasurements: docToUpdate.normalizedMeasurements,
197
- flags: docToUpdate.flags,
198
- appliedCorrections: docToUpdate.appliedCorrections,
199
- correctedNormalizedMeasurements:
200
- docToUpdate.correctedNormalizedMeasurements,
201
- annotations: docToUpdate.annotations, // Include annotations in audit
202
- deletedAt: null, // No deletion happening, so set it to null or undefined
203
- });
204
- await auditLog.save();
205
- }
206
- });
207
-
208
- // Pre-hook to log multiple document updates
209
- measurementsSchema.pre("updateMany", async function () {
210
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
211
- if (docsToUpdate.length) {
212
- console.log(`Logging ${docsToUpdate.length} documents to audit`);
213
- const auditLogs = docsToUpdate.map((doc) => ({
214
- monitorId: doc.monitorId,
215
- orgId: doc.orgId,
216
- timeUpdated: doc.timeUpdated,
217
- measurements: doc.measurements,
218
- monitorState: doc.monitorState,
219
- monitorSupplier: doc.monitorSupplier,
220
- normalizedMeasurements: doc.normalizedMeasurements,
221
- flags: doc.flags,
222
- appliedCorrections: doc.appliedCorrections,
223
- correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
224
- annotations: doc.annotations, // Include annotations in audit
225
- deletedAt: null, // No deletion happening
226
- }));
227
-
228
- await Audit.insertMany(auditLogs);
229
- }
230
- });
231
-
232
- const Measurements = mongoose.model("Measurements", measurementsSchema);
233
-
234
- export {
235
- measurementsSchema,
236
- Measurements,
237
- Audit,
238
- auditSchema,
239
- annotationSchema,
240
- };
1
+ import mongoose from "mongoose";
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
+
11
+ // Annotation Schema
12
+ const annotationSchema = new mongoose.Schema(
13
+ {
14
+ measurementIdentifier: {
15
+ type: String,
16
+ required: true,
17
+ trim: true,
18
+ // Examples: "PM2_5", "PM10", "NO2", "O3", etc.
19
+ },
20
+ comment: {
21
+ type: String,
22
+ required: true,
23
+ trim: true,
24
+ },
25
+ adminId: {
26
+ type: mongoose.Types.ObjectId,
27
+ ref: "Admin",
28
+ required: true,
29
+ },
30
+ timestamp: {
31
+ type: Date,
32
+ default: Date.now,
33
+ required: true,
34
+ },
35
+ },
36
+ {
37
+ _id: true,
38
+ timestamps: false,
39
+ }
40
+ );
41
+
42
+ // Add index for efficient querying
43
+ annotationSchema.index({ measurementIdentifier: 1, timestamp: -1 });
44
+
45
+ // Audit Schema
46
+ const auditSchema = mongoose.Schema(
47
+ {
48
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
49
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
50
+ timeUpdated: Date,
51
+ deletedAt: { type: Date, default: Date.now },
52
+ measurements: Object,
53
+ monitorState: String,
54
+ monitorSupplier: String,
55
+ normalizedMeasurements: Object,
56
+ flags: Object,
57
+ appliedCorrections: {
58
+ type: Map,
59
+ of: correctionSnapshotSchema,
60
+ default: {},
61
+ },
62
+ correctedNormalizedMeasurements: Object,
63
+ annotations: [annotationSchema], // Include annotations in audit trail
64
+ },
65
+ {
66
+ timestamps: true,
67
+ }
68
+ );
69
+
70
+ const Audit = mongoose.model("Audit", auditSchema);
71
+
72
+ // Measurements Schema
73
+ const measurementsSchema = mongoose.Schema(
74
+ {
75
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
76
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
77
+ timeUpdated: Date,
78
+ measurements: Object,
79
+ monitorState: String,
80
+ monitorSupplier: String,
81
+ normalizedMeasurements: Object,
82
+ flags: Object,
83
+ appliedCorrections: {
84
+ type: Map,
85
+ of: correctionSnapshotSchema,
86
+ default: {},
87
+ },
88
+ correctedNormalizedMeasurements: Object,
89
+ annotations: {
90
+ type: [annotationSchema],
91
+ default: [],
92
+ },
93
+ },
94
+ {
95
+ timestamps: true,
96
+ }
97
+ );
98
+
99
+ // CRITICAL: Add compound index for monitorId + timeUpdated
100
+ // This significantly improves performance for date range queries on specific monitors
101
+ measurementsSchema.index({ monitorId: 1, timeUpdated: -1 });
102
+
103
+ // Keep the individual indexes as they may still be useful for other queries
104
+ measurementsSchema.index({ monitorId: 1 });
105
+ measurementsSchema.index({ timeUpdated: 1 });
106
+
107
+ // Annotation-related indexes
108
+ measurementsSchema.index({
109
+ "annotations.measurementIdentifier": 1,
110
+ "annotations.timestamp": -1,
111
+ });
112
+ measurementsSchema.index({ "annotations.userId": 1 });
113
+
114
+ // Pre-hook to log single document deletions
115
+ measurementsSchema.pre("findOneAndDelete", async function () {
116
+ const docToDelete = await this.model.findOne(this.getFilter());
117
+ if (docToDelete) {
118
+ console.log("Logging findOneAndDelete to audit", docToDelete);
119
+ const auditLog = new Audit({
120
+ monitorId: docToDelete.monitorId,
121
+ orgId: docToDelete.orgId,
122
+ timeUpdated: docToDelete.timeUpdated,
123
+ measurements: docToDelete.measurements,
124
+ monitorState: docToDelete.monitorState,
125
+ monitorSupplier: docToDelete.monitorSupplier,
126
+ normalizedMeasurements: docToDelete.normalizedMeasurements,
127
+ flags: docToDelete.flags,
128
+ appliedCorrections: docToDelete.appliedCorrections,
129
+ correctedNormalizedMeasurements:
130
+ docToDelete.correctedNormalizedMeasurements,
131
+ annotations: docToDelete.annotations, // Include annotations in audit
132
+ deletedAt: new Date(),
133
+ });
134
+ await auditLog.save();
135
+ }
136
+ });
137
+
138
+ // Pre-hook to log multiple document deletions
139
+ measurementsSchema.pre("deleteMany", async function () {
140
+ console.log("deleteMany pre-hook triggered");
141
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
142
+
143
+ if (docsToDelete.length) {
144
+ console.log(`Logging ${docsToDelete.length} documents to audit`);
145
+ const auditLogs = docsToDelete.map((doc) => ({
146
+ monitorId: doc.monitorId,
147
+ orgId: doc.orgId,
148
+ timeUpdated: doc.timeUpdated,
149
+ measurements: doc.measurements,
150
+ monitorState: doc.monitorState,
151
+ monitorSupplier: doc.monitorSupplier,
152
+ normalizedMeasurements: doc.normalizedMeasurements,
153
+ flags: doc.flags,
154
+ appliedCorrections: doc.appliedCorrections,
155
+ correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
156
+ annotations: doc.annotations, // Include annotations in audit
157
+ deletedAt: new Date(),
158
+ }));
159
+
160
+ await Audit.insertMany(auditLogs);
161
+ }
162
+ });
163
+
164
+ // Pre-hook to log a single document deletion (for deleteOne)
165
+ measurementsSchema.pre("deleteOne", async function () {
166
+ console.log("deleteOne pre-hook triggered");
167
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
168
+
169
+ if (docToDelete) {
170
+ console.log("Logging deleteOne to audit", docToDelete);
171
+ const auditLog = new Audit({
172
+ monitorId: docToDelete.monitorId,
173
+ orgId: docToDelete.orgId,
174
+ timeUpdated: docToDelete.timeUpdated,
175
+ measurements: docToDelete.measurements,
176
+ monitorState: docToDelete.monitorState,
177
+ monitorSupplier: docToDelete.monitorSupplier,
178
+ normalizedMeasurements: docToDelete.normalizedMeasurements,
179
+ flags: docToDelete.flags,
180
+ appliedCorrections: docToDelete.appliedCorrections,
181
+ correctedNormalizedMeasurements:
182
+ docToDelete.correctedNormalizedMeasurements,
183
+ annotations: docToDelete.annotations, // Include annotations in audit
184
+ deletedAt: new Date(),
185
+ });
186
+ await auditLog.save();
187
+ }
188
+ });
189
+
190
+ // Pre-hook to log single document updates
191
+ measurementsSchema.pre("findOneAndUpdate", async function () {
192
+ const docToUpdate = await this.model.findOne(this.getFilter()).lean();
193
+ if (docToUpdate) {
194
+ console.log("Logging findOneAndUpdate to audit", docToUpdate);
195
+ const auditLog = new Audit({
196
+ monitorId: docToUpdate.monitorId,
197
+ orgId: docToUpdate.orgId,
198
+ timeUpdated: docToUpdate.timeUpdated,
199
+ measurements: docToUpdate.measurements,
200
+ monitorState: docToUpdate.monitorState,
201
+ monitorSupplier: docToUpdate.monitorSupplier,
202
+ normalizedMeasurements: docToUpdate.normalizedMeasurements,
203
+ flags: docToUpdate.flags,
204
+ appliedCorrections: docToUpdate.appliedCorrections,
205
+ correctedNormalizedMeasurements:
206
+ docToUpdate.correctedNormalizedMeasurements,
207
+ annotations: docToUpdate.annotations, // Include annotations in audit
208
+ deletedAt: null, // No deletion happening, so set it to null or undefined
209
+ });
210
+ await auditLog.save();
211
+ }
212
+ });
213
+
214
+ // Pre-hook to log multiple document updates
215
+ measurementsSchema.pre("updateMany", async function () {
216
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
217
+ if (docsToUpdate.length) {
218
+ console.log(`Logging ${docsToUpdate.length} documents to audit`);
219
+ const auditLogs = docsToUpdate.map((doc) => ({
220
+ monitorId: doc.monitorId,
221
+ orgId: doc.orgId,
222
+ timeUpdated: doc.timeUpdated,
223
+ measurements: doc.measurements,
224
+ monitorState: doc.monitorState,
225
+ monitorSupplier: doc.monitorSupplier,
226
+ normalizedMeasurements: doc.normalizedMeasurements,
227
+ flags: doc.flags,
228
+ appliedCorrections: doc.appliedCorrections,
229
+ correctedNormalizedMeasurements: doc.correctedNormalizedMeasurements,
230
+ annotations: doc.annotations, // Include annotations in audit
231
+ deletedAt: null, // No deletion happening
232
+ }));
233
+
234
+ await Audit.insertMany(auditLogs);
235
+ }
236
+ });
237
+
238
+ const Measurements = mongoose.model("Measurements", measurementsSchema);
239
+
240
+ export {
241
+ measurementsSchema,
242
+ Measurements,
243
+ Audit,
244
+ auditSchema,
245
+ annotationSchema,
246
+ };
@@ -1,25 +1,25 @@
1
- import mongoose from "mongoose";
2
-
3
- const monitorRequestsSchema = mongoose.Schema(
4
- {
5
- monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
6
- monitorOwnerOrgName: String,
7
- monitorReceiverOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
8
- monitorReceiverOrgName: String,
9
- monitorList: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
10
- status: {
11
- type: String,
12
- enum: ["Requested", "Approved", "Denied", "Cancel", "Deleted"],
13
- },
14
- },
15
- {
16
- timestamps: true,
17
- }
18
- );
19
-
20
- const MonitorRequests = mongoose.model(
21
- "MonitorRequests",
22
- monitorRequestsSchema
23
- );
24
-
25
- export { monitorRequestsSchema, MonitorRequests };
1
+ import mongoose from "mongoose";
2
+
3
+ const monitorRequestsSchema = mongoose.Schema(
4
+ {
5
+ monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
6
+ monitorOwnerOrgName: String,
7
+ monitorReceiverOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
8
+ monitorReceiverOrgName: String,
9
+ monitorList: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
10
+ status: {
11
+ type: String,
12
+ enum: ["Requested", "Approved", "Denied", "Cancel", "Deleted"],
13
+ },
14
+ },
15
+ {
16
+ timestamps: true,
17
+ }
18
+ );
19
+
20
+ const MonitorRequests = mongoose.model(
21
+ "MonitorRequests",
22
+ monitorRequestsSchema
23
+ );
24
+
25
+ export { monitorRequestsSchema, MonitorRequests };
@@ -1,21 +1,21 @@
1
- import mongoose from "mongoose";
2
-
3
- const monitorSuppliersSchema = mongoose.Schema(
4
- {
5
- name: String,
6
- supplierTypes: [String],
7
- authenticationStructure: [String],
8
- defaultParameterThresholds: Object,
9
- defaultCompletenessThresholdCount: Number,
10
- },
11
- {
12
- timestamps: true,
13
- }
14
- );
15
-
16
- const MonitorSuppliers = mongoose.model(
17
- "MonitorSuppliers",
18
- monitorSuppliersSchema
19
- );
20
-
21
- export { monitorSuppliersSchema, MonitorSuppliers };
1
+ import mongoose from "mongoose";
2
+
3
+ const monitorSuppliersSchema = mongoose.Schema(
4
+ {
5
+ name: String,
6
+ supplierTypes: [String],
7
+ authenticationStructure: [String],
8
+ defaultParameterThresholds: Object,
9
+ defaultCompletenessThresholdCount: Number,
10
+ },
11
+ {
12
+ timestamps: true,
13
+ }
14
+ );
15
+
16
+ const MonitorSuppliers = mongoose.model(
17
+ "MonitorSuppliers",
18
+ monitorSuppliersSchema
19
+ );
20
+
21
+ export { monitorSuppliersSchema, MonitorSuppliers };