@justair/justair-library 3.2.1 → 3.3.0
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 +1 -1
- package/src/index.js +2 -1
- package/src/models/measurements.js +86 -11
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
configurationsSchema,
|
|
4
4
|
Configurations,
|
|
5
5
|
} from "./models/configurations.js";
|
|
6
|
-
import { measurementsSchema, Measurements } from "./models/measurements.js";
|
|
6
|
+
import { measurementsSchema, Measurements, Audit } from "./models/measurements.js";
|
|
7
7
|
import {
|
|
8
8
|
monitorRequestsSchema,
|
|
9
9
|
MonitorRequests,
|
|
@@ -76,4 +76,5 @@ export {
|
|
|
76
76
|
ApiKey,
|
|
77
77
|
UsageMetrics,
|
|
78
78
|
usageMetricsSchema,
|
|
79
|
+
Audit
|
|
79
80
|
};
|
|
@@ -1,18 +1,93 @@
|
|
|
1
|
-
import mongoose from
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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" },
|
|
6
8
|
timeUpdated: Date,
|
|
9
|
+
deletedAt: { type: Date, default: Date.now }, // Timestamp of deletion
|
|
7
10
|
measurements: Object,
|
|
8
|
-
monitorState: String
|
|
9
|
-
},
|
|
10
|
-
|
|
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
|
+
// Post hook to log single document deletions
|
|
38
|
+
measurementsSchema.post("findOneAndDelete", async function (doc) {
|
|
39
|
+
if (doc) {
|
|
40
|
+
const auditLog = new Audit({
|
|
41
|
+
monitorId: doc.monitorId,
|
|
42
|
+
orgId: doc.orgId,
|
|
43
|
+
timeUpdated: doc.timeUpdated,
|
|
44
|
+
measurements: doc.measurements,
|
|
45
|
+
monitorState: doc.monitorState,
|
|
46
|
+
});
|
|
47
|
+
await auditLog.save();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Post hook to log multiple document deletions
|
|
52
|
+
measurementsSchema.post("deleteMany", async function (result, next) {
|
|
53
|
+
// Access the query object to know what was deleted
|
|
54
|
+
const deletedDocs = await this.model.find(this.getQuery());
|
|
55
|
+
|
|
56
|
+
if (deletedDocs.length) {
|
|
57
|
+
const auditLogs = deletedDocs.map((doc) => ({
|
|
58
|
+
monitorId: doc.monitorId,
|
|
59
|
+
orgId: doc.orgId,
|
|
60
|
+
timeUpdated: doc.timeUpdated,
|
|
61
|
+
measurements: doc.measurements,
|
|
62
|
+
monitorState: doc.monitorState,
|
|
63
|
+
deletedAt: Date.now(),
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
await Audit.insertMany(auditLogs);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
next();
|
|
11
70
|
});
|
|
12
71
|
|
|
13
|
-
|
|
14
|
-
measurementsSchema.
|
|
72
|
+
// Post hook to log a single document deletion (for deleteOne)
|
|
73
|
+
measurementsSchema.post("deleteOne", async function (result, next) {
|
|
74
|
+
const deletedDoc = await this.model.findOne(this.getQuery());
|
|
75
|
+
|
|
76
|
+
if (deletedDoc) {
|
|
77
|
+
const auditLog = new Audit({
|
|
78
|
+
monitorId: deletedDoc.monitorId,
|
|
79
|
+
orgId: deletedDoc.orgId,
|
|
80
|
+
timeUpdated: deletedDoc.timeUpdated,
|
|
81
|
+
measurements: deletedDoc.measurements,
|
|
82
|
+
monitorState: deletedDoc.monitorState,
|
|
83
|
+
deletedAt: Date.now(),
|
|
84
|
+
});
|
|
85
|
+
await auditLog.save();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
next();
|
|
89
|
+
});
|
|
15
90
|
|
|
16
|
-
const Measurements = mongoose.model(
|
|
91
|
+
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
17
92
|
|
|
18
|
-
export
|
|
93
|
+
export { measurementsSchema, Measurements, Audit };
|