@justair/justair-library 3.3.2 → 3.3.3
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 -86
- 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 -30
- package/src/models/jobs.js +34 -34
- package/src/models/lightmonitors.js +28 -28
- package/src/models/measurements.js +130 -95
- package/src/models/monitorRequests.js +15 -15
- package/src/models/monitorSuppliers.js +11 -11
- package/src/models/monitors.js +95 -95
- package/src/models/organizations.js +23 -23
- package/src/models/parameters.js +11 -11
- package/src/models/qanotifications.js +29 -29
- 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,31 +1,31 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
const locationSchema = mongoose.Schema(
|
|
4
|
-
{
|
|
5
|
-
city: { type: String, default: null },
|
|
6
|
-
county: { type: String, default: null },
|
|
7
|
-
state: { type: String, required: [true, "state is required"] },
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
timestamps: false,
|
|
11
|
-
_id: false,
|
|
12
|
-
}
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const announcementSchema = mongoose.Schema(
|
|
16
|
-
{
|
|
17
|
-
category: { type: String, required: [true, "category is required"]},
|
|
18
|
-
message: { type: String, required: [true, "message is required"] },
|
|
19
|
-
link: String,
|
|
20
|
-
location: { type: locationSchema, required: [true, "location is required"] },
|
|
21
|
-
isActive: { type: Boolean, default: false },
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
timestamps: true,
|
|
25
|
-
}
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
const Announcements = mongoose.model("Announcements", announcementSchema);
|
|
29
|
-
|
|
30
|
-
export { announcementSchema, Announcements };
|
|
31
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const locationSchema = mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
city: { type: String, default: null },
|
|
6
|
+
county: { type: String, default: null },
|
|
7
|
+
state: { type: String, required: [true, "state is required"] },
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
timestamps: false,
|
|
11
|
+
_id: false,
|
|
12
|
+
}
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const announcementSchema = mongoose.Schema(
|
|
16
|
+
{
|
|
17
|
+
category: { type: String, required: [true, "category is required"]},
|
|
18
|
+
message: { type: String, required: [true, "message is required"] },
|
|
19
|
+
link: String,
|
|
20
|
+
location: { type: locationSchema, required: [true, "location is required"] },
|
|
21
|
+
isActive: { type: Boolean, default: false },
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
timestamps: true,
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const Announcements = mongoose.model("Announcements", announcementSchema);
|
|
29
|
+
|
|
30
|
+
export { announcementSchema, Announcements };
|
|
31
|
+
|
package/src/models/apiKey.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
const apiKeySchema = mongoose.Schema(
|
|
3
|
-
{
|
|
4
|
-
adminId: { type: mongoose.Schema.Types.ObjectId, ref: "Admin" },
|
|
5
|
-
clientKey: { type: String, unique: true },
|
|
6
|
-
permissions: {
|
|
7
|
-
type: Object,
|
|
8
|
-
of: [{ type: mongoose.Schema.Types.ObjectId }],
|
|
9
|
-
default: new Map(),
|
|
10
|
-
},
|
|
11
|
-
accessToken: String,
|
|
12
|
-
refreshToken: String,
|
|
13
|
-
lastUsedAt: Date,
|
|
14
|
-
isActive: Boolean,
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
timestamps: true,
|
|
18
|
-
}
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
const ApiKey = mongoose.model("ApiKey", apiKeySchema);
|
|
22
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
const apiKeySchema = mongoose.Schema(
|
|
3
|
+
{
|
|
4
|
+
adminId: { type: mongoose.Schema.Types.ObjectId, ref: "Admin" },
|
|
5
|
+
clientKey: { type: String, unique: true },
|
|
6
|
+
permissions: {
|
|
7
|
+
type: Object,
|
|
8
|
+
of: [{ type: mongoose.Schema.Types.ObjectId }],
|
|
9
|
+
default: new Map(),
|
|
10
|
+
},
|
|
11
|
+
accessToken: String,
|
|
12
|
+
refreshToken: String,
|
|
13
|
+
lastUsedAt: Date,
|
|
14
|
+
isActive: Boolean,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
timestamps: true,
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
const ApiKey = mongoose.model("ApiKey", apiKeySchema);
|
|
22
|
+
|
|
23
23
|
export { apiKeySchema, ApiKey };
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
|
-
|
|
3
|
-
const configurationsSchema = mongoose.Schema({
|
|
4
|
-
type: { type: String, enum: ['flag'] },
|
|
5
|
-
name: String,
|
|
6
|
-
properties: Object,
|
|
7
|
-
isActive: { type: Boolean, default: false },
|
|
8
|
-
firstRun: {type: Boolean, default: false}
|
|
9
|
-
}, {
|
|
10
|
-
timestamps: true
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
configurationsSchema.index({name: 1});
|
|
14
|
-
|
|
15
|
-
const Configurations = mongoose.model('Configurations', configurationsSchema);
|
|
16
|
-
|
|
17
|
-
export {configurationsSchema, Configurations};
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
|
|
3
|
+
const configurationsSchema = mongoose.Schema({
|
|
4
|
+
type: { type: String, enum: ['flag'] },
|
|
5
|
+
name: String,
|
|
6
|
+
properties: Object,
|
|
7
|
+
isActive: { type: Boolean, default: false },
|
|
8
|
+
firstRun: {type: Boolean, default: false}
|
|
9
|
+
}, {
|
|
10
|
+
timestamps: true
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
configurationsSchema.index({name: 1});
|
|
14
|
+
|
|
15
|
+
const Configurations = mongoose.model('Configurations', configurationsSchema);
|
|
16
|
+
|
|
17
|
+
export {configurationsSchema, Configurations};
|
package/src/models/contexts.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
const contextsSchema = mongoose.Schema(
|
|
4
|
-
{
|
|
5
|
-
name: String
|
|
6
|
-
},
|
|
7
|
-
{
|
|
8
|
-
timestamps: true
|
|
9
|
-
}
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
const Contexts = mongoose.model("Contexts", contextsSchema);
|
|
13
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const contextsSchema = mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
name: String
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
timestamps: true
|
|
9
|
+
}
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const Contexts = mongoose.model("Contexts", contextsSchema);
|
|
13
|
+
|
|
14
14
|
export {contextsSchema, Contexts};
|
package/src/models/events.js
CHANGED
|
@@ -1,30 +1,124 @@
|
|
|
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
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
// Events Audit Schema
|
|
4
|
+
const eventsAuditSchema = mongoose.Schema(
|
|
5
|
+
{
|
|
6
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
7
|
+
parameter: {
|
|
8
|
+
type: String,
|
|
9
|
+
enum: ["AQI", "PID"],
|
|
10
|
+
},
|
|
11
|
+
values: Object,
|
|
12
|
+
alertLevels: Object,
|
|
13
|
+
currentMonitorState: {
|
|
14
|
+
type: String,
|
|
15
|
+
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
16
|
+
},
|
|
17
|
+
pollutantsFlags: {
|
|
18
|
+
type: Object,
|
|
19
|
+
default: {},
|
|
20
|
+
},
|
|
21
|
+
deletedAt: { type: Date, default: null },
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
timestamps: true,
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const EventsAudit = mongoose.model("EventsAudit", eventsAuditSchema);
|
|
29
|
+
|
|
30
|
+
// Events Schema
|
|
31
|
+
const eventsSchema = mongoose.Schema(
|
|
32
|
+
{
|
|
33
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
34
|
+
parameter: {
|
|
35
|
+
type: String,
|
|
36
|
+
enum: ["AQI", "PID"],
|
|
37
|
+
},
|
|
38
|
+
values: Object,
|
|
39
|
+
alertLevels: Object,
|
|
40
|
+
currentMonitorState: {
|
|
41
|
+
type: String,
|
|
42
|
+
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
43
|
+
},
|
|
44
|
+
pollutantsFlags: {
|
|
45
|
+
type: Object,
|
|
46
|
+
default: {},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
timestamps: true,
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
eventsSchema.index({ monitorId: 1 });
|
|
55
|
+
eventsSchema.index({ parameter: 1 });
|
|
56
|
+
|
|
57
|
+
// Pre-hook to log single document deletions
|
|
58
|
+
eventsSchema.pre("findOneAndDelete", async function () {
|
|
59
|
+
const docToDelete = await this.model.findOne(this.getFilter());
|
|
60
|
+
if (docToDelete) {
|
|
61
|
+
console.log("Logging findOneAndDelete to events audit", docToDelete);
|
|
62
|
+
const auditLog = new EventsAudit({
|
|
63
|
+
...docToDelete.toObject(),
|
|
64
|
+
deletedAt: new Date(),
|
|
65
|
+
});
|
|
66
|
+
await auditLog.save();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Pre-hook to log multiple document deletions
|
|
71
|
+
eventsSchema.pre("deleteMany", async function () {
|
|
72
|
+
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
73
|
+
if (docsToDelete.length) {
|
|
74
|
+
console.log(`Logging ${docsToDelete.length} documents to events audit`);
|
|
75
|
+
const auditLogs = docsToDelete.map((doc) => ({
|
|
76
|
+
...doc,
|
|
77
|
+
deletedAt: new Date(),
|
|
78
|
+
}));
|
|
79
|
+
await EventsAudit.insertMany(auditLogs);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Pre-hook to log a single document deletion (for deleteOne)
|
|
84
|
+
eventsSchema.pre("deleteOne", async function () {
|
|
85
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
86
|
+
if (docToDelete) {
|
|
87
|
+
console.log("Logging deleteOne to events audit", docToDelete);
|
|
88
|
+
const auditLog = new EventsAudit({
|
|
89
|
+
...docToDelete,
|
|
90
|
+
deletedAt: new Date(),
|
|
91
|
+
});
|
|
92
|
+
await auditLog.save();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// Pre-hook to log single document updates
|
|
97
|
+
eventsSchema.pre("findOneAndUpdate", async function () {
|
|
98
|
+
const docToUpdate = await this.model.findOne(this.getFilter()).lean();
|
|
99
|
+
if (docToUpdate) {
|
|
100
|
+
console.log("Logging findOneAndUpdate to events audit", docToUpdate);
|
|
101
|
+
const auditLog = new EventsAudit({
|
|
102
|
+
...docToUpdate,
|
|
103
|
+
deletedAt: null, // Not deleted
|
|
104
|
+
});
|
|
105
|
+
await auditLog.save();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Pre-hook to log multiple document updates
|
|
110
|
+
eventsSchema.pre("updateMany", async function () {
|
|
111
|
+
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
112
|
+
if (docsToUpdate.length) {
|
|
113
|
+
console.log(`Logging ${docsToUpdate.length} documents to events audit`);
|
|
114
|
+
const auditLogs = docsToUpdate.map((doc) => ({
|
|
115
|
+
...doc,
|
|
116
|
+
deletedAt: null, // Not deleted
|
|
117
|
+
}));
|
|
118
|
+
await EventsAudit.insertMany(auditLogs);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const Events = mongoose.model("Events", eventsSchema);
|
|
123
|
+
|
|
124
|
+
export { eventsSchema, Events, EventsAudit, eventsAuditSchema };
|
package/src/models/jobs.js
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
const jobsSchema = mongoose.Schema(
|
|
4
|
-
{
|
|
5
|
-
requestorId: { type: mongoose.Types.ObjectId, ref: "Admin" },
|
|
6
|
-
requestorEmail: String,
|
|
7
|
-
category: {
|
|
8
|
-
type: String,
|
|
9
|
-
enum: [
|
|
10
|
-
"Truck Detection",
|
|
11
|
-
"Backfill Monitor Data",
|
|
12
|
-
"AQI Network Summary",
|
|
13
|
-
"Multi Monitor Data Pull",
|
|
14
|
-
"Multi Monitor Average AQI Data Pull",
|
|
15
|
-
],
|
|
16
|
-
},
|
|
17
|
-
resourceURL: String,
|
|
18
|
-
publicResourceURL: String,
|
|
19
|
-
resourceMetaData: Object,
|
|
20
|
-
resultData: { type: mongoose.Schema.Types.Mixed, default: {} },
|
|
21
|
-
jobStatus: {
|
|
22
|
-
type: String,
|
|
23
|
-
enum: ["New", "In-Progress", "Complete", "Cancelled", "Failed"],
|
|
24
|
-
},
|
|
25
|
-
jobStatusHistory: [Object],
|
|
26
|
-
sharedWith: [{ type: mongoose.Types.ObjectId, ref: "Admin" }],
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
timestamps: true,
|
|
30
|
-
}
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
const Jobs = mongoose.model("Jobs", jobsSchema);
|
|
34
|
-
export { jobsSchema, Jobs };
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const jobsSchema = mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
requestorId: { type: mongoose.Types.ObjectId, ref: "Admin" },
|
|
6
|
+
requestorEmail: String,
|
|
7
|
+
category: {
|
|
8
|
+
type: String,
|
|
9
|
+
enum: [
|
|
10
|
+
"Truck Detection",
|
|
11
|
+
"Backfill Monitor Data",
|
|
12
|
+
"AQI Network Summary",
|
|
13
|
+
"Multi Monitor Data Pull",
|
|
14
|
+
"Multi Monitor Average AQI Data Pull",
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
resourceURL: String,
|
|
18
|
+
publicResourceURL: String,
|
|
19
|
+
resourceMetaData: Object,
|
|
20
|
+
resultData: { type: mongoose.Schema.Types.Mixed, default: {} },
|
|
21
|
+
jobStatus: {
|
|
22
|
+
type: String,
|
|
23
|
+
enum: ["New", "In-Progress", "Complete", "Cancelled", "Failed"],
|
|
24
|
+
},
|
|
25
|
+
jobStatusHistory: [Object],
|
|
26
|
+
sharedWith: [{ type: mongoose.Types.ObjectId, ref: "Admin" }],
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
timestamps: true,
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const Jobs = mongoose.model("Jobs", jobsSchema);
|
|
34
|
+
export { jobsSchema, Jobs };
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
|
|
3
|
-
const lightMonitorSchema = mongoose.Schema(
|
|
4
|
-
{
|
|
5
|
-
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
6
|
-
currentColor: { type: String, default: "16000" },
|
|
7
|
-
isActive: { type: Boolean, default: false },
|
|
8
|
-
configuration: {
|
|
9
|
-
hueUsername: { type: String },
|
|
10
|
-
clientId: { type: String, required: true },
|
|
11
|
-
clientSecret: { type: String, required: true },
|
|
12
|
-
lightId: { type: String, required: true },
|
|
13
|
-
accessTokenInfo: Object,
|
|
14
|
-
refreshTokenInfo: Object,
|
|
15
|
-
hueCode: { type: String, required: true },
|
|
16
|
-
hueCodeHistory: [Object],
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
timestamps: true,
|
|
21
|
-
}
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
lightMonitorSchema.index({ monitorId: 1 });
|
|
25
|
-
|
|
26
|
-
const LightMonitors = mongoose.model("LightMonitor", lightMonitorSchema);
|
|
27
|
-
|
|
28
|
-
export { lightMonitorSchema, LightMonitors };
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const lightMonitorSchema = mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
6
|
+
currentColor: { type: String, default: "16000" },
|
|
7
|
+
isActive: { type: Boolean, default: false },
|
|
8
|
+
configuration: {
|
|
9
|
+
hueUsername: { type: String },
|
|
10
|
+
clientId: { type: String, required: true },
|
|
11
|
+
clientSecret: { type: String, required: true },
|
|
12
|
+
lightId: { type: String, required: true },
|
|
13
|
+
accessTokenInfo: Object,
|
|
14
|
+
refreshTokenInfo: Object,
|
|
15
|
+
hueCode: { type: String, required: true },
|
|
16
|
+
hueCodeHistory: [Object],
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
timestamps: true,
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
lightMonitorSchema.index({ monitorId: 1 });
|
|
25
|
+
|
|
26
|
+
const LightMonitors = mongoose.model("LightMonitor", lightMonitorSchema);
|
|
27
|
+
|
|
28
|
+
export { lightMonitorSchema, LightMonitors };
|
|
@@ -1,95 +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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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 };
|