@justair/justair-library 3.3.3 → 3.3.4-alpha

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,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);
@@ -1,95 +1,125 @@
1
- import mongoose from "mongoose";
2
-
3
- const monitorsSchema = mongoose.Schema(
4
- {
5
- monitorCode: String,
6
- //keep up to date with diff manufacturers
7
- monitorSupplier: {
8
- type: String,
9
- enum: [
10
- "clarity",
11
- "aeroqual",
12
- "purple air",
13
- "reference monitor",
14
- "earthview",
15
- "sensit",
16
- "blue sky",
17
- "aq mesh",
18
- "quant aq",
19
- ],
20
- },
21
- monitorType: String,
22
- monitorIdFromSupplier: String,
23
- measurementUpdate: Date,
24
- monitorProperties: Object,
25
- isPrivate: { type: Boolean, default: false },
26
- monitorState: {
27
- type: String,
28
- enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
29
- },
30
- monitorStateHistory: [Object],
31
- monitorAlertStatus: {
32
- type: String,
33
- enum: [
34
- "Good",
35
- "Moderate",
36
- "Unhealthy for SG",
37
- "Unhealthy",
38
- "Very Unhealthy",
39
- "Hazardous",
40
- "Bad",
41
- ],
42
- default: "Good",
43
- },
44
- sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
45
- sponsorName: String,
46
- monitorLatitude: Number,
47
- monitorLongitude: Number,
48
- gpsLocation: {
49
- type: { type: String, enul: ["Point"], required: true },
50
- coordinates: { type: [Number], required: true },
51
- },
52
- location: Object, //copied over from associated org
53
- context: [String],
54
- colocationDate: Date,
55
- deploymentDate: Date,
56
- subscriptionDate: Date,
57
- parameters: [
58
- {
59
- type: String,
60
- enum: [
61
- "NO2",
62
- "SO2",
63
- "PM2.5",
64
- "PM10",
65
- "Temperature",
66
- "Humidity",
67
- "OZONE",
68
- "VOC",
69
- "CO",
70
- "NO",
71
- "PM1",
72
- "WS And Direction",
73
- ],
74
- },
75
- ],
76
- latestPM2_5: Number,
77
- latestAQI_PM2_5: Number,
78
- notes: [Object],
79
- calculatedAverages: [Object],
80
- images: [String],
81
- isActive: { type: Boolean, default: true },
82
- },
83
- {
84
- timestamps: true,
85
- }
86
- );
87
-
88
- monitorsSchema.index({ gpsLocation: "2dsphere" });
89
- monitorsSchema.index({ monitorSupplier: 1 });
90
- monitorsSchema.index({ monitorIdFromSupplier: 1 });
91
- monitorsSchema.index({ monitorState: 1 });
92
-
93
- const Monitors = mongoose.model("Monitors", monitorsSchema);
94
-
95
- export { monitorsSchema, Monitors };
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
+ const parameterThresholdsSchema = mongoose.Schema({
18
+ parameter: {
19
+ type: Map,
20
+ of: new mongoose.Schema({
21
+ invalid: {
22
+ upper: Number,
23
+ lower: Number,
24
+ },
25
+
26
+ warning: {
27
+ upper: Number,
28
+ lower: Number,
29
+ },
30
+ }),
31
+ },
32
+ });
33
+
34
+ /*Ensure the parameter value is one of our valid parameters */
35
+ parameterThresholdsSchema.path('parameter').validate(function(value) {
36
+ for (let key of value.keys()) {
37
+ if (!parametersEnum.includes(key)) {
38
+ return false;
39
+ }
40
+ }
41
+ return true;
42
+ }, 'Invalid parameter key');
43
+
44
+ // Monitors Schema
45
+ const monitorsSchema = mongoose.Schema(
46
+ {
47
+ monitorCode: String,
48
+ //keep up to date with diff manufacturers
49
+ monitorSupplier: {
50
+ type: String,
51
+ enum: [
52
+ "clarity",
53
+ "aeroqual",
54
+ "purple air",
55
+ "reference monitor",
56
+ "earthview",
57
+ "sensit",
58
+ "blue sky",
59
+ "aq mesh",
60
+ "quant aq",
61
+ ],
62
+ },
63
+ monitorType: String,
64
+ monitorIdFromSupplier: String,
65
+ measurementUpdate: Date,
66
+ monitorProperties: Object,
67
+ isPrivate: { type: Boolean, default: false },
68
+ monitorState: {
69
+ type: String,
70
+ enum: ["Collocation", "Deployed", "Maintenance", "Pending Deployment"],
71
+ },
72
+ monitorStateHistory: [Object],
73
+ monitorAlertStatus: {
74
+ type: String,
75
+ enum: [
76
+ "Good",
77
+ "Moderate",
78
+ "Unhealthy for SG",
79
+ "Unhealthy",
80
+ "Very Unhealthy",
81
+ "Hazardous",
82
+ "Bad",
83
+ ],
84
+ default: "Good",
85
+ },
86
+ sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
87
+ sponsorName: String,
88
+ monitorLatitude: Number,
89
+ monitorLongitude: Number,
90
+ gpsLocation: {
91
+ type: { type: String, enul: ["Point"], required: true },
92
+ coordinates: { type: [Number], required: true },
93
+ },
94
+ location: Object, //copied over from associated org
95
+ context: [String],
96
+ colocationDate: Date,
97
+ deploymentDate: Date,
98
+ subscriptionDate: Date,
99
+ parameters: [
100
+ {
101
+ type: String,
102
+ enum: parametersEnum,
103
+ },
104
+ ],
105
+ latestPM2_5: Number,
106
+ latestAQI_PM2_5: Number,
107
+ notes: [Object],
108
+ calculatedAverages: [Object],
109
+ images: [String],
110
+ isActive: { type: Boolean, default: true },
111
+ parameterThresholds: { type: Map, of: parameterThresholdsSchema },
112
+ },
113
+ {
114
+ timestamps: true,
115
+ }
116
+ );
117
+
118
+ monitorsSchema.index({ gpsLocation: "2dsphere" });
119
+ monitorsSchema.index({ monitorSupplier: 1 });
120
+ monitorsSchema.index({ monitorIdFromSupplier: 1 });
121
+ monitorsSchema.index({ monitorState: 1 });
122
+
123
+ const Monitors = mongoose.model("Monitors", monitorsSchema);
124
+
125
+ export { monitorsSchema, Monitors };
@@ -1,24 +1,24 @@
1
- import mongoose from "mongoose";
2
-
3
- const organizationsSchema = mongoose.Schema({
4
- name: String,
5
- website: String,
6
- location: Object,
7
- authorized: { type: Boolean, default: false },
8
- orgAPIKey: [Object],
9
- orgCode: String,
10
- orgDescription: String,
11
- orgLogo: String,
12
- customAlertLevels: [Object],
13
- connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
14
- communityMessages: [Object],
15
- weeklyReportData: [Object],
16
- isActive: { type: Boolean, default: true }
17
- },
18
- {
19
- timestamps: true
20
- });
21
-
22
- const Organizations = mongoose.model('Organizations', organizationsSchema);
23
-
1
+ import mongoose from "mongoose";
2
+
3
+ const organizationsSchema = mongoose.Schema({
4
+ name: String,
5
+ website: String,
6
+ location: Object,
7
+ authorized: { type: Boolean, default: false },
8
+ orgAPIKey: [Object],
9
+ orgCode: String,
10
+ orgDescription: String,
11
+ orgLogo: String,
12
+ customAlertLevels: [Object],
13
+ connectedMonitors: [{ type: mongoose.Types.ObjectId, ref: 'Monitors' }],
14
+ communityMessages: [Object],
15
+ weeklyReportData: [Object],
16
+ isActive: { type: Boolean, default: true }
17
+ },
18
+ {
19
+ timestamps: true
20
+ });
21
+
22
+ const Organizations = mongoose.model('Organizations', organizationsSchema);
23
+
24
24
  export {organizationsSchema, Organizations};
@@ -1,12 +1,12 @@
1
- import mongoose from "mongoose";
2
-
3
- const parametersSchema = mongoose.Schema({
4
- name: String
5
- },
6
- {
7
- timestamps: true
8
- });
9
-
10
- const Parameters = mongoose.model("Parameters", parametersSchema);
11
-
1
+ import mongoose from "mongoose";
2
+
3
+ const parametersSchema = mongoose.Schema({
4
+ name: String
5
+ },
6
+ {
7
+ timestamps: true
8
+ });
9
+
10
+ const Parameters = mongoose.model("Parameters", parametersSchema);
11
+
12
12
  export {parametersSchema, Parameters};