@justair/justair-library 3.3.1 → 3.3.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.
- 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 +86 -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 +30 -26
- package/src/models/jobs.js +34 -34
- package/src/models/lightmonitors.js +28 -28
- package/src/models/measurements.js +95 -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,26 +1,30 @@
|
|
|
1
|
-
import mongoose from "mongoose";
|
|
2
|
-
//TODO: Capture historical data, every hour record AQI values for monitors
|
|
3
|
-
const eventsSchema = mongoose.Schema(
|
|
4
|
-
{
|
|
5
|
-
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
6
|
-
parameter: {
|
|
7
|
-
type: String,
|
|
8
|
-
enum: ["AQI", "PID"],
|
|
9
|
-
},
|
|
10
|
-
values: Object,
|
|
11
|
-
alertLevels: Object,
|
|
12
|
-
currentMonitorState: {
|
|
13
|
-
type: String,
|
|
14
|
-
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
//TODO: Capture historical data, every hour record AQI values for monitors
|
|
3
|
+
const eventsSchema = mongoose.Schema(
|
|
4
|
+
{
|
|
5
|
+
monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
|
|
6
|
+
parameter: {
|
|
7
|
+
type: String,
|
|
8
|
+
enum: ["AQI", "PID"],
|
|
9
|
+
},
|
|
10
|
+
values: Object,
|
|
11
|
+
alertLevels: Object,
|
|
12
|
+
currentMonitorState: {
|
|
13
|
+
type: String,
|
|
14
|
+
enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
|
|
15
|
+
},
|
|
16
|
+
pollutantsFlags: {
|
|
17
|
+
type: Object,
|
|
18
|
+
default: {},
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
timestamps: true,
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
eventsSchema.index({ monitorId: 1 });
|
|
27
|
+
eventsSchema.index({ parameter: 1 });
|
|
28
|
+
|
|
29
|
+
const Events = mongoose.model("Events", eventsSchema);
|
|
30
|
+
export { eventsSchema, Events };
|
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,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
|
-
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
94
|
-
|
|
95
|
-
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
|
+
const Measurements = mongoose.model("Measurements", measurementsSchema);
|
|
94
|
+
|
|
95
|
+
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};
|