@justair/justair-library 3.3.4 → 3.3.5
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/README +85 -85
- package/package.json +27 -27
- package/src/config/db.js +42 -42
- package/src/config/logger.js +44 -44
- package/src/index.js +93 -95
- package/src/models/admin.js +22 -22
- package/src/models/announcements.js +31 -31
- package/src/models/apiKey.js +22 -22
- package/src/models/configurations.js +17 -17
- package/src/models/contexts.js +13 -13
- package/src/models/events.js +124 -124
- package/src/models/jobs.js +34 -36
- package/src/models/lightmonitors.js +28 -28
- package/src/models/measurements.js +130 -130
- package/src/models/monitorRequests.js +15 -15
- package/src/models/monitorSuppliers.js +11 -11
- package/src/models/monitorType.js +40 -0
- package/src/models/monitors.js +98 -215
- package/src/models/organizations.js +23 -23
- package/src/models/parameters.js +11 -11
- package/src/models/qanotifications.js +29 -32
- package/src/models/referenceMonitorInfo.js +18 -18
- package/src/models/tests/admin.test.js +42 -42
- package/src/models/tests/configurations.test.js +44 -44
- package/src/models/tests/measurements.test.js +46 -46
- package/src/models/tests/monitorRequests.test.js +46 -46
- package/src/models/tests/monitors.test.js +62 -62
- package/src/models/tests/organizations.test.js +51 -51
- package/src/models/tests/users.test.js +54 -54
- package/src/models/usageMetrics.js +28 -28
- package/src/models/users.js +30 -30
- package/tsconfig.json +10 -10
|
@@ -1,130 +1,130 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
// Audit Schema
|
|
4
|
-
const auditSchema = mongoose.Schema(
|
|
5
|
-
{
|
|
6
|
-
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
7
|
-
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
8
|
-
timeUpdated: Date,
|
|
9
|
-
deletedAt: { type: Date, default: Date.now },
|
|
10
|
-
measurements: Object,
|
|
11
|
-
monitorState: String,
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
timestamps: true,
|
|
15
|
-
}
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
const Audit = mongoose.model("Audit", auditSchema);
|
|
19
|
-
|
|
20
|
-
// Measurements Schema
|
|
21
|
-
const measurementsSchema = mongoose.Schema(
|
|
22
|
-
{
|
|
23
|
-
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
24
|
-
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
25
|
-
timeUpdated: Date,
|
|
26
|
-
measurements: Object,
|
|
27
|
-
monitorState: String,
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
timestamps: true,
|
|
31
|
-
}
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
measurementsSchema.index({ monitorId: 1 });
|
|
35
|
-
measurementsSchema.index({ timeUpdated: 1 });
|
|
36
|
-
|
|
37
|
-
// Pre-hook to log single document deletions
|
|
38
|
-
measurementsSchema.pre("findOneAndDelete", async function () {
|
|
39
|
-
const docToDelete = await this.model.findOne(this.getFilter());
|
|
40
|
-
if (docToDelete) {
|
|
41
|
-
console.log("Logging findOneAndDelete to audit", docToDelete);
|
|
42
|
-
const auditLog = new Audit({
|
|
43
|
-
monitorId: docToDelete.monitorId,
|
|
44
|
-
orgId: docToDelete.orgId,
|
|
45
|
-
timeUpdated: docToDelete.timeUpdated,
|
|
46
|
-
measurements: docToDelete.measurements,
|
|
47
|
-
monitorState: docToDelete.monitorState,
|
|
48
|
-
deletedAt: new Date(),
|
|
49
|
-
});
|
|
50
|
-
await auditLog.save();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
// Pre-hook to log multiple document deletions
|
|
55
|
-
measurementsSchema.pre("deleteMany", async function () {
|
|
56
|
-
console.log("deleteMany pre-hook triggered");
|
|
57
|
-
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
58
|
-
|
|
59
|
-
if (docsToDelete.length) {
|
|
60
|
-
console.log(`Logging ${docsToDelete.length} documents to audit`);
|
|
61
|
-
const auditLogs = docsToDelete.map((doc) => ({
|
|
62
|
-
monitorId: doc.monitorId,
|
|
63
|
-
orgId: doc.orgId,
|
|
64
|
-
timeUpdated: doc.timeUpdated,
|
|
65
|
-
measurements: doc.measurements,
|
|
66
|
-
monitorState: doc.monitorState,
|
|
67
|
-
deletedAt: new Date(),
|
|
68
|
-
}));
|
|
69
|
-
|
|
70
|
-
await Audit.insertMany(auditLogs);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Pre-hook to log a single document deletion (for deleteOne)
|
|
75
|
-
measurementsSchema.pre("deleteOne", async function () {
|
|
76
|
-
console.log("deleteOne pre-hook triggered");
|
|
77
|
-
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
78
|
-
|
|
79
|
-
if (docToDelete) {
|
|
80
|
-
console.log("Logging deleteOne to audit", docToDelete);
|
|
81
|
-
const auditLog = new Audit({
|
|
82
|
-
monitorId: docToDelete.monitorId,
|
|
83
|
-
orgId: docToDelete.orgId,
|
|
84
|
-
timeUpdated: docToDelete.timeUpdated,
|
|
85
|
-
measurements: docToDelete.measurements,
|
|
86
|
-
monitorState: docToDelete.monitorState,
|
|
87
|
-
deletedAt: new Date(),
|
|
88
|
-
});
|
|
89
|
-
await auditLog.save();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// Pre-hook to log single document updates
|
|
94
|
-
measurementsSchema.pre("findOneAndUpdate", async function () {
|
|
95
|
-
const docToUpdate = await this.model.findOne(this.getFilter()).lean();
|
|
96
|
-
if (docToUpdate) {
|
|
97
|
-
console.log("Logging findOneAndUpdate to audit", docToUpdate);
|
|
98
|
-
const auditLog = new Audit({
|
|
99
|
-
monitorId: docToUpdate.monitorId,
|
|
100
|
-
orgId: docToUpdate.orgId,
|
|
101
|
-
timeUpdated: docToUpdate.timeUpdated,
|
|
102
|
-
measurements: docToUpdate.measurements,
|
|
103
|
-
monitorState: docToUpdate.monitorState,
|
|
104
|
-
deletedAt: null, // No deletion happening, so set it to null or undefined
|
|
105
|
-
});
|
|
106
|
-
await auditLog.save();
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// Pre-hook to log multiple document updates
|
|
111
|
-
measurementsSchema.pre("updateMany", async function () {
|
|
112
|
-
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
113
|
-
if (docsToUpdate.length) {
|
|
114
|
-
console.log(`Logging ${docsToUpdate.length} documents to audit`);
|
|
115
|
-
const auditLogs = docsToUpdate.map((doc) => ({
|
|
116
|
-
monitorId: doc.monitorId,
|
|
117
|
-
orgId: doc.orgId,
|
|
118
|
-
timeUpdated: doc.timeUpdated,
|
|
119
|
-
measurements: doc.measurements,
|
|
120
|
-
monitorState: doc.monitorState,
|
|
121
|
-
deletedAt: null, // No deletion happening
|
|
122
|
-
}));
|
|
123
|
-
|
|
124
|
-
await Audit.insertMany(auditLogs);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
129
|
-
|
|
130
|
-
export { measurementsSchema, Measurements, Audit, auditSchema };
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
// Audit Schema
|
|
4
|
+
const auditSchema = mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
7
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
8
|
+
timeUpdated: Date,
|
|
9
|
+
deletedAt: { type: Date, default: Date.now },
|
|
10
|
+
measurements: Object,
|
|
11
|
+
monitorState: String,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
timestamps: true,
|
|
15
|
+
}
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const Audit = mongoose.model("Audit", auditSchema);
|
|
19
|
+
|
|
20
|
+
// Measurements Schema
|
|
21
|
+
const measurementsSchema = mongoose.Schema(
|
|
22
|
+
{
|
|
23
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
24
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
25
|
+
timeUpdated: Date,
|
|
26
|
+
measurements: Object,
|
|
27
|
+
monitorState: String,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
timestamps: true,
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
measurementsSchema.index({ monitorId: 1 });
|
|
35
|
+
measurementsSchema.index({ timeUpdated: 1 });
|
|
36
|
+
|
|
37
|
+
// Pre-hook to log single document deletions
|
|
38
|
+
measurementsSchema.pre("findOneAndDelete", async function () {
|
|
39
|
+
const docToDelete = await this.model.findOne(this.getFilter());
|
|
40
|
+
if (docToDelete) {
|
|
41
|
+
console.log("Logging findOneAndDelete to audit", docToDelete);
|
|
42
|
+
const auditLog = new Audit({
|
|
43
|
+
monitorId: docToDelete.monitorId,
|
|
44
|
+
orgId: docToDelete.orgId,
|
|
45
|
+
timeUpdated: docToDelete.timeUpdated,
|
|
46
|
+
measurements: docToDelete.measurements,
|
|
47
|
+
monitorState: docToDelete.monitorState,
|
|
48
|
+
deletedAt: new Date(),
|
|
49
|
+
});
|
|
50
|
+
await auditLog.save();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Pre-hook to log multiple document deletions
|
|
55
|
+
measurementsSchema.pre("deleteMany", async function () {
|
|
56
|
+
console.log("deleteMany pre-hook triggered");
|
|
57
|
+
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
58
|
+
|
|
59
|
+
if (docsToDelete.length) {
|
|
60
|
+
console.log(`Logging ${docsToDelete.length} documents to audit`);
|
|
61
|
+
const auditLogs = docsToDelete.map((doc) => ({
|
|
62
|
+
monitorId: doc.monitorId,
|
|
63
|
+
orgId: doc.orgId,
|
|
64
|
+
timeUpdated: doc.timeUpdated,
|
|
65
|
+
measurements: doc.measurements,
|
|
66
|
+
monitorState: doc.monitorState,
|
|
67
|
+
deletedAt: new Date(),
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
await Audit.insertMany(auditLogs);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Pre-hook to log a single document deletion (for deleteOne)
|
|
75
|
+
measurementsSchema.pre("deleteOne", async function () {
|
|
76
|
+
console.log("deleteOne pre-hook triggered");
|
|
77
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
78
|
+
|
|
79
|
+
if (docToDelete) {
|
|
80
|
+
console.log("Logging deleteOne to audit", docToDelete);
|
|
81
|
+
const auditLog = new Audit({
|
|
82
|
+
monitorId: docToDelete.monitorId,
|
|
83
|
+
orgId: docToDelete.orgId,
|
|
84
|
+
timeUpdated: docToDelete.timeUpdated,
|
|
85
|
+
measurements: docToDelete.measurements,
|
|
86
|
+
monitorState: docToDelete.monitorState,
|
|
87
|
+
deletedAt: new Date(),
|
|
88
|
+
});
|
|
89
|
+
await auditLog.save();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Pre-hook to log single document updates
|
|
94
|
+
measurementsSchema.pre("findOneAndUpdate", async function () {
|
|
95
|
+
const docToUpdate = await this.model.findOne(this.getFilter()).lean();
|
|
96
|
+
if (docToUpdate) {
|
|
97
|
+
console.log("Logging findOneAndUpdate to audit", docToUpdate);
|
|
98
|
+
const auditLog = new Audit({
|
|
99
|
+
monitorId: docToUpdate.monitorId,
|
|
100
|
+
orgId: docToUpdate.orgId,
|
|
101
|
+
timeUpdated: docToUpdate.timeUpdated,
|
|
102
|
+
measurements: docToUpdate.measurements,
|
|
103
|
+
monitorState: docToUpdate.monitorState,
|
|
104
|
+
deletedAt: null, // No deletion happening, so set it to null or undefined
|
|
105
|
+
});
|
|
106
|
+
await auditLog.save();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Pre-hook to log multiple document updates
|
|
111
|
+
measurementsSchema.pre("updateMany", async function () {
|
|
112
|
+
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
113
|
+
if (docsToUpdate.length) {
|
|
114
|
+
console.log(`Logging ${docsToUpdate.length} documents to audit`);
|
|
115
|
+
const auditLogs = docsToUpdate.map((doc) => ({
|
|
116
|
+
monitorId: doc.monitorId,
|
|
117
|
+
orgId: doc.orgId,
|
|
118
|
+
timeUpdated: doc.timeUpdated,
|
|
119
|
+
measurements: doc.measurements,
|
|
120
|
+
monitorState: doc.monitorState,
|
|
121
|
+
deletedAt: null, // No deletion happening
|
|
122
|
+
}));
|
|
123
|
+
|
|
124
|
+
await Audit.insertMany(auditLogs);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
129
|
+
|
|
130
|
+
export { measurementsSchema, Measurements, Audit, auditSchema };
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
|
-
|
|
3
|
-
const monitorRequestsSchema = mongoose.Schema({
|
|
4
|
-
monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
|
|
5
|
-
monitorOwnerOrgName: String,
|
|
6
|
-
monitorSharerOrg: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
|
|
7
|
-
monitorSharerOrgName: String,
|
|
8
|
-
monitorList: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
|
|
9
|
-
status: { type: String, enum: ['Requested', 'Approved', 'Denied', 'Cancel', 'Deleted'] }
|
|
10
|
-
}, {
|
|
11
|
-
timestamps: true
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const MonitorRequests = mongoose.model('MonitorRequests', monitorRequestsSchema);
|
|
15
|
-
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
|
|
3
|
+
const monitorRequestsSchema = mongoose.Schema({
|
|
4
|
+
monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
|
|
5
|
+
monitorOwnerOrgName: String,
|
|
6
|
+
monitorSharerOrg: { type: mongoose.Types.ObjectId, ref: 'Organizations' },
|
|
7
|
+
monitorSharerOrgName: String,
|
|
8
|
+
monitorList: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
|
|
9
|
+
status: { type: String, enum: ['Requested', 'Approved', 'Denied', 'Cancel', 'Deleted'] }
|
|
10
|
+
}, {
|
|
11
|
+
timestamps: true
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const MonitorRequests = mongoose.model('MonitorRequests', monitorRequestsSchema);
|
|
15
|
+
|
|
16
16
|
export {monitorRequestsSchema, MonitorRequests};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
const monitorSuppliersSchema = mongoose.Schema({
|
|
4
|
-
name : String
|
|
5
|
-
},
|
|
6
|
-
{
|
|
7
|
-
timestamps: true
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
const MonitorSuppliers = mongoose.model("MonitorSuppliers", monitorSuppliersSchema);
|
|
11
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const monitorSuppliersSchema = mongoose.Schema({
|
|
4
|
+
name : String
|
|
5
|
+
},
|
|
6
|
+
{
|
|
7
|
+
timestamps: true
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const MonitorSuppliers = mongoose.model("MonitorSuppliers", monitorSuppliersSchema);
|
|
11
|
+
|
|
12
12
|
export {monitorSuppliersSchema, MonitorSuppliers};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const parametersEnum = [
|
|
4
|
+
"NO2",
|
|
5
|
+
"SO2",
|
|
6
|
+
"PM2.5",
|
|
7
|
+
"PM10",
|
|
8
|
+
"Temperature",
|
|
9
|
+
"Humidity",
|
|
10
|
+
"OZONE",
|
|
11
|
+
"VOC",
|
|
12
|
+
"CO",
|
|
13
|
+
"NO",
|
|
14
|
+
"PM1",
|
|
15
|
+
"WS And Direction",
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
const monitorTypeSchema = mongoose.Schema(
|
|
19
|
+
{
|
|
20
|
+
model: {
|
|
21
|
+
type: String,
|
|
22
|
+
required: true,
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
supplier: {
|
|
26
|
+
type: mongoose.Types.ObjectId,
|
|
27
|
+
required: true,
|
|
28
|
+
ref: "MonitorSuppliers",
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
parameters: {
|
|
32
|
+
type: String,
|
|
33
|
+
enum: parametersEnum,
|
|
34
|
+
required: true,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{ timestamps: true }
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const monitorType = mongoose.model("MonitorType", monitorTypeSchema);
|
package/src/models/monitors.js
CHANGED
|
@@ -1,215 +1,98 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
],
|
|
65
|
-
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
latestPM2_5: Number,
|
|
100
|
-
latestAQI_PM2_5: Number,
|
|
101
|
-
notes: [Object],
|
|
102
|
-
calculatedAverages: [Object],
|
|
103
|
-
images: [String],
|
|
104
|
-
isActive: { type: Boolean, default: true },
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
timestamps: true,
|
|
108
|
-
}
|
|
109
|
-
);
|
|
110
|
-
|
|
111
|
-
monitorsSchema.index({ gpsLocation: "2dsphere" });
|
|
112
|
-
monitorsSchema.index({ monitorSupplier: 1 });
|
|
113
|
-
monitorsSchema.index({ monitorIdFromSupplier: 1 });
|
|
114
|
-
monitorsSchema.index({ monitorState: 1 });
|
|
115
|
-
|
|
116
|
-
// Pre-hook to log single document deletions
|
|
117
|
-
monitorsSchema.pre("findOneAndDelete", async function () {
|
|
118
|
-
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
119
|
-
if (docToDelete) {
|
|
120
|
-
console.log("Logging findOneAndDelete to monitor audit", docToDelete);
|
|
121
|
-
const auditLog = new MonitorAudit({
|
|
122
|
-
monitorId: docToDelete._id,
|
|
123
|
-
orgId: docToDelete.sponsor,
|
|
124
|
-
timeUpdated: docToDelete.updatedAt,
|
|
125
|
-
monitorProperties: docToDelete.monitorProperties,
|
|
126
|
-
monitorState: docToDelete.monitorState,
|
|
127
|
-
monitorLocation: {
|
|
128
|
-
type: "Point",
|
|
129
|
-
coordinates: [
|
|
130
|
-
docToDelete.monitorLongitude,
|
|
131
|
-
docToDelete.monitorLatitude,
|
|
132
|
-
],
|
|
133
|
-
},
|
|
134
|
-
deletedAt: new Date(),
|
|
135
|
-
});
|
|
136
|
-
await auditLog.save();
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// Pre-hook to log multiple document deletions
|
|
141
|
-
monitorsSchema.pre("deleteMany", async function () {
|
|
142
|
-
console.log("deleteMany pre-hook triggered for monitors");
|
|
143
|
-
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
144
|
-
|
|
145
|
-
if (docsToDelete.length) {
|
|
146
|
-
console.log(`Logging ${docsToDelete.length} monitor documents to audit`);
|
|
147
|
-
const auditLogs = docsToDelete.map((doc) => ({
|
|
148
|
-
monitorId: doc._id,
|
|
149
|
-
orgId: doc.sponsor,
|
|
150
|
-
timeUpdated: doc.updatedAt,
|
|
151
|
-
monitorProperties: doc.monitorProperties,
|
|
152
|
-
monitorState: doc.monitorState,
|
|
153
|
-
monitorLocation: {
|
|
154
|
-
type: "Point",
|
|
155
|
-
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
156
|
-
},
|
|
157
|
-
deletedAt: new Date(),
|
|
158
|
-
}));
|
|
159
|
-
|
|
160
|
-
await MonitorAudit.insertMany(auditLogs);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// Pre-hook to log a single document deletion (for deleteOne)
|
|
165
|
-
monitorsSchema.pre("deleteOne", async function () {
|
|
166
|
-
console.log("deleteOne pre-hook triggered for monitors");
|
|
167
|
-
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
168
|
-
|
|
169
|
-
if (docToDelete) {
|
|
170
|
-
console.log("Logging deleteOne to monitor audit", docToDelete);
|
|
171
|
-
const auditLog = new MonitorAudit({
|
|
172
|
-
monitorId: docToDelete._id,
|
|
173
|
-
orgId: docToDelete.sponsor,
|
|
174
|
-
timeUpdated: docToDelete.updatedAt,
|
|
175
|
-
monitorProperties: docToDelete.monitorProperties,
|
|
176
|
-
monitorState: docToDelete.monitorState,
|
|
177
|
-
monitorLocation: {
|
|
178
|
-
type: "Point",
|
|
179
|
-
coordinates: [
|
|
180
|
-
docToDelete.monitorLongitude,
|
|
181
|
-
docToDelete.monitorLatitude,
|
|
182
|
-
],
|
|
183
|
-
},
|
|
184
|
-
deletedAt: new Date(),
|
|
185
|
-
});
|
|
186
|
-
await auditLog.save();
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// Pre-hook to log multiple document updates
|
|
191
|
-
monitorsSchema.pre("updateMany", async function () {
|
|
192
|
-
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
193
|
-
if (docsToUpdate.length) {
|
|
194
|
-
console.log(`Logging ${docsToUpdate.length} monitor documents to audit`);
|
|
195
|
-
const auditLogs = docsToUpdate.map((doc) => ({
|
|
196
|
-
monitorId: doc._id,
|
|
197
|
-
orgId: doc.sponsor,
|
|
198
|
-
timeUpdated: doc.updatedAt,
|
|
199
|
-
monitorProperties: doc.monitorProperties,
|
|
200
|
-
monitorState: doc.monitorState,
|
|
201
|
-
monitorLocation: {
|
|
202
|
-
type: "Point",
|
|
203
|
-
coordinates: [doc.monitorLongitude, doc.monitorLatitude],
|
|
204
|
-
},
|
|
205
|
-
deletedAt: null, // Not a deletion, so this field is null
|
|
206
|
-
}));
|
|
207
|
-
|
|
208
|
-
await MonitorAudit.insertMany(auditLogs);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
// Create the Monitors model
|
|
213
|
-
const Monitors = mongoose.model("Monitors", monitorsSchema);
|
|
214
|
-
|
|
215
|
-
export { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit };
|
|
1
|
+
import mongoose from "mongoose";import { emitWarning } from "process";
|
|
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
|
+
// Monitors Schema
|
|
18
|
+
const monitorsSchema = mongoose.Schema(
|
|
19
|
+
{
|
|
20
|
+
monitorCode: String,
|
|
21
|
+
//keep up to date with diff manufacturers
|
|
22
|
+
monitorSupplier: {
|
|
23
|
+
type: String,
|
|
24
|
+
enum: [
|
|
25
|
+
"clarity",
|
|
26
|
+
"aeroqual",
|
|
27
|
+
"purple air",
|
|
28
|
+
"reference monitor",
|
|
29
|
+
"earthview",
|
|
30
|
+
"sensit",
|
|
31
|
+
"blue sky",
|
|
32
|
+
"aq mesh",
|
|
33
|
+
"quant aq",
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
monitorType: String,
|
|
37
|
+
monitorIdFromSupplier: String,
|
|
38
|
+
measurementUpdate: Date,
|
|
39
|
+
monitorProperties: Object,
|
|
40
|
+
isPrivate: { type: Boolean, default: false },
|
|
41
|
+
monitorState: {
|
|
42
|
+
type: String,
|
|
43
|
+
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
44
|
+
},
|
|
45
|
+
monitorStateHistory: [Object],
|
|
46
|
+
monitorAlertStatus: {
|
|
47
|
+
type: String,
|
|
48
|
+
enum: [
|
|
49
|
+
"Good",
|
|
50
|
+
"Moderate",
|
|
51
|
+
"Unhealthy for SG",
|
|
52
|
+
"Unhealthy",
|
|
53
|
+
"Very Unhealthy",
|
|
54
|
+
"Hazardous",
|
|
55
|
+
"Bad",
|
|
56
|
+
],
|
|
57
|
+
default: "Good",
|
|
58
|
+
},
|
|
59
|
+
sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
60
|
+
sponsorName: String,
|
|
61
|
+
monitorLatitude: Number,
|
|
62
|
+
monitorLongitude: Number,
|
|
63
|
+
gpsLocation: {
|
|
64
|
+
type: { type: String, enul: ["Point"], required: true },
|
|
65
|
+
coordinates: { type: [Number], required: true },
|
|
66
|
+
},
|
|
67
|
+
location: Object, //copied over from associated org
|
|
68
|
+
context: [String],
|
|
69
|
+
colocationDate: Date,
|
|
70
|
+
deploymentDate: Date,
|
|
71
|
+
subscriptionDate: Date,
|
|
72
|
+
parameters: [
|
|
73
|
+
{
|
|
74
|
+
type: String,
|
|
75
|
+
enum: parametersEnum,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
latestPM2_5: Number,
|
|
79
|
+
latestAQI_PM2_5: Number,
|
|
80
|
+
notes: [Object],
|
|
81
|
+
calculatedAverages: [Object],
|
|
82
|
+
images: [String],
|
|
83
|
+
isActive: { type: Boolean, default: true },
|
|
84
|
+
parameterThresholds: Object
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
timestamps: true,
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
monitorsSchema.index({ gpsLocation: "2dsphere" });
|
|
92
|
+
monitorsSchema.index({ monitorSupplier: 1 });
|
|
93
|
+
monitorsSchema.index({ monitorIdFromSupplier: 1 });
|
|
94
|
+
monitorsSchema.index({ monitorState: 1 });
|
|
95
|
+
|
|
96
|
+
const Monitors = mongoose.model("Monitors", monitorsSchema);
|
|
97
|
+
|
|
98
|
+
export { monitorsSchema, Monitors };
|