@justair/justair-library 3.4.1 → 3.4.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.
@@ -1,132 +1,133 @@
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
- normalizedMeasurements: Object,
13
- },
14
- {
15
- timestamps: true,
16
- }
17
- );
18
-
19
- const Audit = mongoose.model("Audit", auditSchema);
20
-
21
- // Measurements Schema
22
- const measurementsSchema = mongoose.Schema(
23
- {
24
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
25
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
26
- timeUpdated: Date,
27
- measurements: Object,
28
- monitorState: String,
29
- normalizedMeasurements: Object,
30
- },
31
- {
32
- timestamps: true,
33
- }
34
- );
35
-
36
- measurementsSchema.index({ monitorId: 1 });
37
- measurementsSchema.index({ timeUpdated: 1 });
38
-
39
- // Pre-hook to log single document deletions
40
- measurementsSchema.pre("findOneAndDelete", async function () {
41
- const docToDelete = await this.model.findOne(this.getFilter());
42
- if (docToDelete) {
43
- console.log("Logging findOneAndDelete to audit", docToDelete);
44
- const auditLog = new Audit({
45
- monitorId: docToDelete.monitorId,
46
- orgId: docToDelete.orgId,
47
- timeUpdated: docToDelete.timeUpdated,
48
- measurements: docToDelete.measurements,
49
- monitorState: docToDelete.monitorState,
50
- deletedAt: new Date(),
51
- });
52
- await auditLog.save();
53
- }
54
- });
55
-
56
- // Pre-hook to log multiple document deletions
57
- measurementsSchema.pre("deleteMany", async function () {
58
- console.log("deleteMany pre-hook triggered");
59
- const docsToDelete = await this.model.find(this.getFilter()).lean();
60
-
61
- if (docsToDelete.length) {
62
- console.log(`Logging ${docsToDelete.length} documents to audit`);
63
- const auditLogs = docsToDelete.map((doc) => ({
64
- monitorId: doc.monitorId,
65
- orgId: doc.orgId,
66
- timeUpdated: doc.timeUpdated,
67
- measurements: doc.measurements,
68
- monitorState: doc.monitorState,
69
- deletedAt: new Date(),
70
- }));
71
-
72
- await Audit.insertMany(auditLogs);
73
- }
74
- });
75
-
76
- // Pre-hook to log a single document deletion (for deleteOne)
77
- measurementsSchema.pre("deleteOne", async function () {
78
- console.log("deleteOne pre-hook triggered");
79
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
80
-
81
- if (docToDelete) {
82
- console.log("Logging deleteOne to audit", docToDelete);
83
- const auditLog = new Audit({
84
- monitorId: docToDelete.monitorId,
85
- orgId: docToDelete.orgId,
86
- timeUpdated: docToDelete.timeUpdated,
87
- measurements: docToDelete.measurements,
88
- monitorState: docToDelete.monitorState,
89
- deletedAt: new Date(),
90
- });
91
- await auditLog.save();
92
- }
93
- });
94
-
95
- // Pre-hook to log single document updates
96
- measurementsSchema.pre("findOneAndUpdate", async function () {
97
- const docToUpdate = await this.model.findOne(this.getFilter()).lean();
98
- if (docToUpdate) {
99
- console.log("Logging findOneAndUpdate to audit", docToUpdate);
100
- const auditLog = new Audit({
101
- monitorId: docToUpdate.monitorId,
102
- orgId: docToUpdate.orgId,
103
- timeUpdated: docToUpdate.timeUpdated,
104
- measurements: docToUpdate.measurements,
105
- monitorState: docToUpdate.monitorState,
106
- deletedAt: null, // No deletion happening, so set it to null or undefined
107
- });
108
- await auditLog.save();
109
- }
110
- });
111
-
112
- // Pre-hook to log multiple document updates
113
- measurementsSchema.pre("updateMany", async function () {
114
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
115
- if (docsToUpdate.length) {
116
- console.log(`Logging ${docsToUpdate.length} documents to audit`);
117
- const auditLogs = docsToUpdate.map((doc) => ({
118
- monitorId: doc.monitorId,
119
- orgId: doc.orgId,
120
- timeUpdated: doc.timeUpdated,
121
- measurements: doc.measurements,
122
- monitorState: doc.monitorState,
123
- deletedAt: null, // No deletion happening
124
- }));
125
-
126
- await Audit.insertMany(auditLogs);
127
- }
128
- });
129
-
130
- const Measurements = mongoose.model("Measurements", measurementsSchema);
131
-
132
- 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
+ normalizedMeasurements: Object,
13
+ },
14
+ {
15
+ timestamps: true,
16
+ }
17
+ );
18
+
19
+ const Audit = mongoose.model("Audit", auditSchema);
20
+
21
+ // Measurements Schema
22
+ const measurementsSchema = mongoose.Schema(
23
+ {
24
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
25
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
26
+ timeUpdated: Date,
27
+ measurements: Object,
28
+ monitorState: String,
29
+ normalizedMeasurements: Object,
30
+ flags: Object
31
+ },
32
+ {
33
+ timestamps: true,
34
+ }
35
+ );
36
+
37
+ measurementsSchema.index({ monitorId: 1 });
38
+ measurementsSchema.index({ timeUpdated: 1 });
39
+
40
+ // Pre-hook to log single document deletions
41
+ measurementsSchema.pre("findOneAndDelete", async function () {
42
+ const docToDelete = await this.model.findOne(this.getFilter());
43
+ if (docToDelete) {
44
+ console.log("Logging findOneAndDelete to audit", docToDelete);
45
+ const auditLog = new Audit({
46
+ monitorId: docToDelete.monitorId,
47
+ orgId: docToDelete.orgId,
48
+ timeUpdated: docToDelete.timeUpdated,
49
+ measurements: docToDelete.measurements,
50
+ monitorState: docToDelete.monitorState,
51
+ deletedAt: new Date(),
52
+ });
53
+ await auditLog.save();
54
+ }
55
+ });
56
+
57
+ // Pre-hook to log multiple document deletions
58
+ measurementsSchema.pre("deleteMany", async function () {
59
+ console.log("deleteMany pre-hook triggered");
60
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
61
+
62
+ if (docsToDelete.length) {
63
+ console.log(`Logging ${docsToDelete.length} documents to audit`);
64
+ const auditLogs = docsToDelete.map((doc) => ({
65
+ monitorId: doc.monitorId,
66
+ orgId: doc.orgId,
67
+ timeUpdated: doc.timeUpdated,
68
+ measurements: doc.measurements,
69
+ monitorState: doc.monitorState,
70
+ deletedAt: new Date(),
71
+ }));
72
+
73
+ await Audit.insertMany(auditLogs);
74
+ }
75
+ });
76
+
77
+ // Pre-hook to log a single document deletion (for deleteOne)
78
+ measurementsSchema.pre("deleteOne", async function () {
79
+ console.log("deleteOne pre-hook triggered");
80
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
81
+
82
+ if (docToDelete) {
83
+ console.log("Logging deleteOne to audit", docToDelete);
84
+ const auditLog = new Audit({
85
+ monitorId: docToDelete.monitorId,
86
+ orgId: docToDelete.orgId,
87
+ timeUpdated: docToDelete.timeUpdated,
88
+ measurements: docToDelete.measurements,
89
+ monitorState: docToDelete.monitorState,
90
+ deletedAt: new Date(),
91
+ });
92
+ await auditLog.save();
93
+ }
94
+ });
95
+
96
+ // Pre-hook to log single document updates
97
+ measurementsSchema.pre("findOneAndUpdate", async function () {
98
+ const docToUpdate = await this.model.findOne(this.getFilter()).lean();
99
+ if (docToUpdate) {
100
+ console.log("Logging findOneAndUpdate to audit", docToUpdate);
101
+ const auditLog = new Audit({
102
+ monitorId: docToUpdate.monitorId,
103
+ orgId: docToUpdate.orgId,
104
+ timeUpdated: docToUpdate.timeUpdated,
105
+ measurements: docToUpdate.measurements,
106
+ monitorState: docToUpdate.monitorState,
107
+ deletedAt: null, // No deletion happening, so set it to null or undefined
108
+ });
109
+ await auditLog.save();
110
+ }
111
+ });
112
+
113
+ // Pre-hook to log multiple document updates
114
+ measurementsSchema.pre("updateMany", async function () {
115
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
116
+ if (docsToUpdate.length) {
117
+ console.log(`Logging ${docsToUpdate.length} documents to audit`);
118
+ const auditLogs = docsToUpdate.map((doc) => ({
119
+ monitorId: doc.monitorId,
120
+ orgId: doc.orgId,
121
+ timeUpdated: doc.timeUpdated,
122
+ measurements: doc.measurements,
123
+ monitorState: doc.monitorState,
124
+ deletedAt: null, // No deletion happening
125
+ }));
126
+
127
+ await Audit.insertMany(auditLogs);
128
+ }
129
+ });
130
+
131
+ const Measurements = mongoose.model("Measurements", measurementsSchema);
132
+
133
+ export { measurementsSchema, Measurements, Audit, auditSchema };
@@ -1,25 +1,25 @@
1
- import mongoose from "mongoose";
2
-
3
- const monitorRequestsSchema = mongoose.Schema(
4
- {
5
- monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
6
- monitorOwnerOrgName: String,
7
- monitorReceiverOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
8
- monitorReceiverOrgName: String,
9
- monitorList: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
10
- status: {
11
- type: String,
12
- enum: ["Requested", "Approved", "Denied", "Cancel", "Deleted"],
13
- },
14
- },
15
- {
16
- timestamps: true,
17
- }
18
- );
19
-
20
- const MonitorRequests = mongoose.model(
21
- "MonitorRequests",
22
- monitorRequestsSchema
23
- );
24
-
25
- export { monitorRequestsSchema, MonitorRequests };
1
+ import mongoose from "mongoose";
2
+
3
+ const monitorRequestsSchema = mongoose.Schema(
4
+ {
5
+ monitorOwnerOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
6
+ monitorOwnerOrgName: String,
7
+ monitorReceiverOrg: { type: mongoose.Types.ObjectId, ref: "Organizations" },
8
+ monitorReceiverOrgName: String,
9
+ monitorList: [{ type: mongoose.Types.ObjectId, ref: "Monitors" }],
10
+ status: {
11
+ type: String,
12
+ enum: ["Requested", "Approved", "Denied", "Cancel", "Deleted"],
13
+ },
14
+ },
15
+ {
16
+ timestamps: true,
17
+ }
18
+ );
19
+
20
+ const MonitorRequests = mongoose.model(
21
+ "MonitorRequests",
22
+ monitorRequestsSchema
23
+ );
24
+
25
+ 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);