@justair/justair-library 3.4.1 → 3.4.2-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.
@@ -1,217 +1,217 @@
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
- // Monitor Audit Schema
18
- const monitorAuditSchema = mongoose.Schema(
19
- {
20
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
21
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
22
- timeUpdated: Date,
23
- deletedAt: { type: Date, default: Date.now }, // Only populated on delete
24
- monitorProperties: Object, // Stores properties of the monitor
25
- monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
26
- monitorLocation: {
27
- // Monitor GPS location (lat, lon)
28
- type: { type: String, enum: ["Point"], required: true },
29
- coordinates: { type: [Number], required: true },
30
- },
31
- },
32
- {
33
- timestamps: true,
34
- }
35
- );
36
-
37
- // Create the MonitorAudit model
38
- const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
39
-
40
- // Monitors Schema
41
- const monitorsSchema = mongoose.Schema(
42
- {
43
- monitorCode: String,
44
- monitorSupplier: {
45
- type: String,
46
- enum: [
47
- "clarity",
48
- "aeroqual",
49
- "purple air",
50
- "reference monitor",
51
- "earthview",
52
- "sensit",
53
- "blue sky",
54
- "aq mesh",
55
- "quant aq",
56
- ],
57
- },
58
- monitorType: String,
59
- monitorIdFromSupplier: String,
60
- measurementUpdate: Date,
61
- monitorProperties: Object,
62
- isPrivate: { type: Boolean, default: false, required: true },
63
- monitorState: {
64
- type: String,
65
- enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
66
- },
67
- monitorStateHistory: [Object],
68
- monitorAlertStatus: {
69
- type: String,
70
- enum: [
71
- "Good",
72
- "Moderate",
73
- "Unhealthy for SG",
74
- "Unhealthy",
75
- "Very Unhealthy",
76
- "Hazardous",
77
- "Bad",
78
- ],
79
- default: "Good",
80
- },
81
- sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
82
- sponsorName: String,
83
- monitorLatitude: Number,
84
- monitorLongitude: Number,
85
- gpsLocation: {
86
- type: { type: String, enum: ["Point"], required: true },
87
- coordinates: { type: [Number], required: true },
88
- },
89
- location: Object,
90
- context: [String],
91
- colocationDate: Date,
92
- deploymentDate: Date,
93
- subscriptionDate: Date,
94
- parameters: [
95
- {
96
- type: String,
97
- enum: parametersEnum,
98
- },
99
- ],
100
- latestPM2_5: Number,
101
- latestAQI_PM2_5: Number,
102
- notes: [Object],
103
- calculatedAverages: [Object],
104
- images: [String],
105
- isActive: { type: Boolean, default: true },
106
- parameterThresholds: Object,
107
- },
108
- {
109
- timestamps: true,
110
- }
111
- );
112
-
113
- monitorsSchema.index({ gpsLocation: "2dsphere" });
114
- monitorsSchema.index({ monitorSupplier: 1 });
115
- monitorsSchema.index({ monitorIdFromSupplier: 1 });
116
- monitorsSchema.index({ monitorState: 1 });
117
-
118
- // Pre-hook to log single document deletions
119
- monitorsSchema.pre("findOneAndDelete", async function () {
120
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
121
- if (docToDelete) {
122
- console.log("Logging findOneAndDelete to monitor audit", docToDelete);
123
- const auditLog = new MonitorAudit({
124
- monitorId: docToDelete._id,
125
- orgId: docToDelete.sponsor,
126
- timeUpdated: docToDelete.updatedAt,
127
- monitorProperties: docToDelete.monitorProperties,
128
- monitorState: docToDelete.monitorState,
129
- monitorLocation: {
130
- type: "Point",
131
- coordinates: [
132
- docToDelete.monitorLongitude,
133
- docToDelete.monitorLatitude,
134
- ],
135
- },
136
- deletedAt: new Date(),
137
- });
138
- await auditLog.save();
139
- }
140
- });
141
-
142
- // Pre-hook to log multiple document deletions
143
- monitorsSchema.pre("deleteMany", async function () {
144
- console.log("deleteMany pre-hook triggered for monitors");
145
- const docsToDelete = await this.model.find(this.getFilter()).lean();
146
-
147
- if (docsToDelete.length) {
148
- console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
149
- const auditLogs = docsToDelete.map((doc) => ({
150
- monitorId: doc._id,
151
- orgId: doc.sponsor,
152
- timeUpdated: doc.updatedAt,
153
- monitorProperties: doc.monitorProperties,
154
- monitorState: doc.monitorState,
155
- monitorLocation: {
156
- type: "Point",
157
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
158
- },
159
- deletedAt: new Date(),
160
- }));
161
-
162
- await MonitorAudit.insertMany(auditLogs);
163
- }
164
- });
165
-
166
- // Pre-hook to log a single document deletion (for deleteOne)
167
- monitorsSchema.pre("deleteOne", async function () {
168
- console.log("deleteOne pre-hook triggered for monitors");
169
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
170
-
171
- if (docToDelete) {
172
- console.log("Logging deleteOne to monitor audit", docToDelete);
173
- const auditLog = new MonitorAudit({
174
- monitorId: docToDelete._id,
175
- orgId: docToDelete.sponsor,
176
- timeUpdated: docToDelete.updatedAt,
177
- monitorProperties: docToDelete.monitorProperties,
178
- monitorState: docToDelete.monitorState,
179
- monitorLocation: {
180
- type: "Point",
181
- coordinates: [
182
- docToDelete.monitorLongitude,
183
- docToDelete.monitorLatitude,
184
- ],
185
- },
186
- deletedAt: new Date(),
187
- });
188
- await auditLog.save();
189
- }
190
- });
191
-
192
- // Pre-hook to log multiple document updates
193
- monitorsSchema.pre("updateMany", async function () {
194
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
195
- if (docsToUpdate.length) {
196
- console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
197
- const auditLogs = docsToUpdate.map((doc) => ({
198
- monitorId: doc._id,
199
- orgId: doc.sponsor,
200
- timeUpdated: doc.updatedAt,
201
- monitorProperties: doc.monitorProperties,
202
- monitorState: doc.monitorState,
203
- monitorLocation: {
204
- type: "Point",
205
- coordinates: [doc.monitorLongitude, doc.monitorLatitude],
206
- },
207
- deletedAt: null, // Not a deletion, so this field is null
208
- }));
209
-
210
- await MonitorAudit.insertMany(auditLogs);
211
- }
212
- });
213
-
214
- // Create the Monitors model
215
- const Monitors = mongoose.model("Monitors", monitorsSchema);
216
-
217
- 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
+ // Monitor Audit Schema
18
+ const monitorAuditSchema = mongoose.Schema(
19
+ {
20
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
21
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
22
+ timeUpdated: Date,
23
+ deletedAt: { type: Date, default: Date.now }, // Only populated on delete
24
+ monitorProperties: Object, // Stores properties of the monitor
25
+ monitorState: String, // Tracks the state of the monitor (e.g., "Deployed", "Maintenance")
26
+ monitorLocation: {
27
+ // Monitor GPS location (lat, lon)
28
+ type: { type: String, enum: ["Point"], required: true },
29
+ coordinates: { type: [Number], required: true },
30
+ },
31
+ },
32
+ {
33
+ timestamps: true,
34
+ }
35
+ );
36
+
37
+ // Create the MonitorAudit model
38
+ const MonitorAudit = mongoose.model("MonitorAudit", monitorAuditSchema);
39
+
40
+ // Monitors Schema
41
+ const monitorsSchema = mongoose.Schema(
42
+ {
43
+ monitorCode: String,
44
+ monitorSupplier: {
45
+ type: String,
46
+ enum: [
47
+ "clarity",
48
+ "aeroqual",
49
+ "purple air",
50
+ "reference monitor",
51
+ "earthview",
52
+ "sensit",
53
+ "blue sky",
54
+ "aq mesh",
55
+ "quant aq",
56
+ ],
57
+ },
58
+ monitorType: String,
59
+ monitorIdFromSupplier: String,
60
+ measurementUpdate: Date,
61
+ monitorProperties: Object,
62
+ isPrivate: { type: Boolean, default: false, required: true },
63
+ monitorState: {
64
+ type: String,
65
+ enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
66
+ },
67
+ monitorStateHistory: [Object],
68
+ monitorAlertStatus: {
69
+ type: String,
70
+ enum: [
71
+ "Good",
72
+ "Moderate",
73
+ "Unhealthy for SG",
74
+ "Unhealthy",
75
+ "Very Unhealthy",
76
+ "Hazardous",
77
+ "Bad",
78
+ ],
79
+ default: "Good",
80
+ },
81
+ sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
82
+ sponsorName: String,
83
+ monitorLatitude: Number,
84
+ monitorLongitude: Number,
85
+ gpsLocation: {
86
+ type: { type: String, enum: ["Point"], required: true },
87
+ coordinates: { type: [Number], required: true },
88
+ },
89
+ location: Object,
90
+ context: [String],
91
+ colocationDate: Date,
92
+ deploymentDate: Date,
93
+ subscriptionDate: Date,
94
+ parameters: [
95
+ {
96
+ type: String,
97
+ enum: parametersEnum,
98
+ },
99
+ ],
100
+ latestPM2_5: Number,
101
+ latestAQI_PM2_5: Number,
102
+ notes: [Object],
103
+ calculatedAverages: [Object],
104
+ images: [String],
105
+ isActive: { type: Boolean, default: true },
106
+ parameterThresholds: Object,
107
+ },
108
+ {
109
+ timestamps: true,
110
+ }
111
+ );
112
+
113
+ monitorsSchema.index({ gpsLocation: "2dsphere" });
114
+ monitorsSchema.index({ monitorSupplier: 1 });
115
+ monitorsSchema.index({ monitorIdFromSupplier: 1 });
116
+ monitorsSchema.index({ monitorState: 1 });
117
+
118
+ // Pre-hook to log single document deletions
119
+ monitorsSchema.pre("findOneAndDelete", async function () {
120
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
121
+ if (docToDelete) {
122
+ console.log("Logging findOneAndDelete to monitor audit", docToDelete);
123
+ const auditLog = new MonitorAudit({
124
+ monitorId: docToDelete._id,
125
+ orgId: docToDelete.sponsor,
126
+ timeUpdated: docToDelete.updatedAt,
127
+ monitorProperties: docToDelete.monitorProperties,
128
+ monitorState: docToDelete.monitorState,
129
+ monitorLocation: {
130
+ type: "Point",
131
+ coordinates: [
132
+ docToDelete.monitorLongitude,
133
+ docToDelete.monitorLatitude,
134
+ ],
135
+ },
136
+ deletedAt: new Date(),
137
+ });
138
+ await auditLog.save();
139
+ }
140
+ });
141
+
142
+ // Pre-hook to log multiple document deletions
143
+ monitorsSchema.pre("deleteMany", async function () {
144
+ console.log("deleteMany pre-hook triggered for monitors");
145
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
146
+
147
+ if (docsToDelete.length) {
148
+ console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
149
+ const auditLogs = docsToDelete.map((doc) => ({
150
+ monitorId: doc._id,
151
+ orgId: doc.sponsor,
152
+ timeUpdated: doc.updatedAt,
153
+ monitorProperties: doc.monitorProperties,
154
+ monitorState: doc.monitorState,
155
+ monitorLocation: {
156
+ type: "Point",
157
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
158
+ },
159
+ deletedAt: new Date(),
160
+ }));
161
+
162
+ await MonitorAudit.insertMany(auditLogs);
163
+ }
164
+ });
165
+
166
+ // Pre-hook to log a single document deletion (for deleteOne)
167
+ monitorsSchema.pre("deleteOne", async function () {
168
+ console.log("deleteOne pre-hook triggered for monitors");
169
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
170
+
171
+ if (docToDelete) {
172
+ console.log("Logging deleteOne to monitor audit", docToDelete);
173
+ const auditLog = new MonitorAudit({
174
+ monitorId: docToDelete._id,
175
+ orgId: docToDelete.sponsor,
176
+ timeUpdated: docToDelete.updatedAt,
177
+ monitorProperties: docToDelete.monitorProperties,
178
+ monitorState: docToDelete.monitorState,
179
+ monitorLocation: {
180
+ type: "Point",
181
+ coordinates: [
182
+ docToDelete.monitorLongitude,
183
+ docToDelete.monitorLatitude,
184
+ ],
185
+ },
186
+ deletedAt: new Date(),
187
+ });
188
+ await auditLog.save();
189
+ }
190
+ });
191
+
192
+ // Pre-hook to log multiple document updates
193
+ monitorsSchema.pre("updateMany", async function () {
194
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
195
+ if (docsToUpdate.length) {
196
+ console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
197
+ const auditLogs = docsToUpdate.map((doc) => ({
198
+ monitorId: doc._id,
199
+ orgId: doc.sponsor,
200
+ timeUpdated: doc.updatedAt,
201
+ monitorProperties: doc.monitorProperties,
202
+ monitorState: doc.monitorState,
203
+ monitorLocation: {
204
+ type: "Point",
205
+ coordinates: [doc.monitorLongitude, doc.monitorLatitude],
206
+ },
207
+ deletedAt: null, // Not a deletion, so this field is null
208
+ }));
209
+
210
+ await MonitorAudit.insertMany(auditLogs);
211
+ }
212
+ });
213
+
214
+ // Create the Monitors model
215
+ const Monitors = mongoose.model("Monitors", monitorsSchema);
216
+
217
+ export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };
@@ -1,24 +1,24 @@
1
- import mongoose from "mongoose";
2
-
3
- const organizationsSchema = mongoose.Schema({
4
- name: String,
5
- website: String,
6
- location: Object,
7
- authorized: { type: Boolean, default: false },
8
- orgAPIKey: [Object],
9
- orgCode: String,
10
- orgDescription: String,
11
- orgLogo: String,
12
- customAlertLevels: [Object],
13
- connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
14
- communityMessages: [Object],
15
- weeklyReportData: [Object],
16
- isActive: { type: Boolean, default: true }
17
- },
18
- {
19
- timestamps: true
20
- });
21
-
22
- const Organizations = mongoose.model('Organizations', organizationsSchema);
23
-
1
+ import mongoose from "mongoose";
2
+
3
+ const organizationsSchema = mongoose.Schema({
4
+ name: String,
5
+ website: String,
6
+ location: Object,
7
+ authorized: { type: Boolean, default: false },
8
+ orgAPIKey: [Object],
9
+ orgCode: String,
10
+ orgDescription: String,
11
+ orgLogo: String,
12
+ customAlertLevels: [Object],
13
+ connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
14
+ communityMessages: [Object],
15
+ weeklyReportData: [Object],
16
+ isActive: { type: Boolean, default: true }
17
+ },
18
+ {
19
+ timestamps: true
20
+ });
21
+
22
+ const Organizations = mongoose.model('Organizations', organizationsSchema);
23
+
24
24
  export {organizationsSchema, Organizations};
@@ -1,12 +1,12 @@
1
- import mongoose from "mongoose";
2
-
3
- const parametersSchema = mongoose.Schema({
4
- name: String
5
- },
6
- {
7
- timestamps: true
8
- });
9
-
10
- const Parameters = mongoose.model("Parameters", parametersSchema);
11
-
1
+ import mongoose from "mongoose";
2
+
3
+ const parametersSchema = mongoose.Schema({
4
+ name: String
5
+ },
6
+ {
7
+ timestamps: true
8
+ });
9
+
10
+ const Parameters = mongoose.model("Parameters", parametersSchema);
11
+
12
12
  export {parametersSchema, Parameters};
@@ -1,32 +1,32 @@
1
- import mongoose from "mongoose";
2
-
3
- const qaNotificationsSchema = mongoose.Schema(
4
- {
5
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
6
- issue: String,
7
- reportedAt: Date,
8
- reportedBy: String,
9
- resolvedAt: Date,
10
- resolvedBy: String,
11
- resolution: String,
12
- statusHistory: [Object],
13
- type: String,
14
- status: {
15
- type: String,
16
- enum: ["Open", "Resolved", "Cancelled", "Needs Review"],
17
- },
18
- },
19
- {
20
- timestamps: true,
21
- }
22
- );
23
-
24
- const QANotifications = mongoose.model(
25
- "QANotifications",
26
- qaNotificationsSchema
27
- );
28
-
29
- qaNotificationsSchema.index({ monitorId: 1 });
30
- qaNotificationsSchema.index({ createdAt: 1 });
31
-
32
- export { qaNotificationsSchema, QANotifications };
1
+ import mongoose from "mongoose";
2
+
3
+ const qaNotificationsSchema = mongoose.Schema(
4
+ {
5
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
6
+ issue: String,
7
+ reportedAt: Date,
8
+ reportedBy: String,
9
+ resolvedAt: Date,
10
+ resolvedBy: String,
11
+ resolution: String,
12
+ statusHistory: [Object],
13
+ type: String,
14
+ status: {
15
+ type: String,
16
+ enum: ["Open", "Resolved", "Cancelled", "Needs Review"],
17
+ },
18
+ },
19
+ {
20
+ timestamps: true,
21
+ }
22
+ );
23
+
24
+ const QANotifications = mongoose.model(
25
+ "QANotifications",
26
+ qaNotificationsSchema
27
+ );
28
+
29
+ qaNotificationsSchema.index({ monitorId: 1 });
30
+ qaNotificationsSchema.index({ createdAt: 1 });
31
+
32
+ export { qaNotificationsSchema, QANotifications };
@@ -1,19 +1,19 @@
1
- import mongoose from 'mongoose';
2
-
3
- const referenceMonitorInfoSchema = mongoose.Schema({
4
- bboxStartLat: String,
5
- bboxStartLong: String,
6
- bboxEndLat: String,
7
- bboxEndLong: String,
8
- parameters: String,
9
- sponsor: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
10
- location: Object,
11
- isActive: { type: Boolean, default: true },
12
- context: [String]
13
- }, {
14
- timestamps: true
15
- });
16
-
17
- const ReferenceMonitorInfo = mongoose.model('ReferenceMonitorInfo', referenceMonitorInfoSchema);
18
-
1
+ import mongoose from 'mongoose';
2
+
3
+ const referenceMonitorInfoSchema = mongoose.Schema({
4
+ bboxStartLat: String,
5
+ bboxStartLong: String,
6
+ bboxEndLat: String,
7
+ bboxEndLong: String,
8
+ parameters: String,
9
+ sponsor: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
10
+ location: Object,
11
+ isActive: { type: Boolean, default: true },
12
+ context: [String]
13
+ }, {
14
+ timestamps: true
15
+ });
16
+
17
+ const ReferenceMonitorInfo = mongoose.model('ReferenceMonitorInfo', referenceMonitorInfoSchema);
18
+
19
19
  export {referenceMonitorInfoSchema, ReferenceMonitorInfo};