@justair/justair-library 4.7.21 → 4.7.22

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,221 +1,221 @@
1
- import mongoose, { Schema } from "mongoose";
2
-
3
- const recipientSchema = new Schema(
4
- {
5
- adminId: {
6
- type: mongoose.Types.ObjectId,
7
- ref: "Admin",
8
- },
9
- typeOfCommunication: String,
10
- dateCommunicated: {
11
- type: Date,
12
- defult: Date.now,
13
- },
14
- },
15
- { _id: false }
16
- );
17
-
18
- const alertsSchema = mongoose.Schema(
19
- {
20
- type: {
21
- type: String,
22
- enum: ["Data Review"],
23
- },
24
- alertConfigurationId: mongoose.Types.ObjectId,
25
- flagType: String,
26
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
27
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
28
- status: {
29
- type: String,
30
- enum: ["Active", "Ack", "Mute"],
31
- },
32
- activeUntil: { type: Date },
33
- actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
34
- actionDate: { type: Date, default: Date.now },
35
- recipients: [recipientSchema],
36
- metaData: Object,
37
- flaggedDataCompleteness: [Object],
38
- flaggedMeasurementIds: [
39
- {
40
- type: mongoose.Types.ObjectId,
41
- ref: "Measurements",
42
- },
43
- ],
44
- },
45
- {
46
- timestamps: true,
47
- }
48
- );
49
-
50
- // Organization-based queries with timestamp ordering
51
- alertsSchema.index({ orgId: 1, updatedAt: -1 });
52
- // Monitor-based queries for lookups with timestamp ordering
53
- alertsSchema.index({ monitorId: 1, updatedAt: -1 });
54
- // Status-based filtering with organization and timestamp
55
- alertsSchema.index({ orgId: 1, status: 1, updatedAt: -1 });
56
- // Alert configuration matching - already optimal
57
- alertsSchema.index({ alertConfigurationId: 1 });
58
-
59
- // Keep individual indexes for backward compatibility
60
- alertsSchema.index({ orgId: 1 });
61
- alertsSchema.index({ monitorId: 1 });
62
- alertsSchema.index({ type: 1 });
63
-
64
- const alertsAuditSchema = mongoose.Schema(
65
- {
66
- timeUpdated: Date,
67
- deletedAt: { type: Date, default: Date.now },
68
- type: {
69
- type: String,
70
- enum: ["Data Review"],
71
- },
72
- alertConfigurationId: mongoose.Types.ObjectId,
73
- orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
74
- monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
75
- status: {
76
- type: String,
77
- enum: ["Active", "Ack", "Mute"],
78
- },
79
- activeUntil: {type: Date},
80
- actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
81
- actionDate: { type: Date, default: Date.now },
82
- recipients: [recipientSchema],
83
- metaData: Object,
84
- flaggedMeasurementIds: [
85
- {
86
- type: mongoose.Types.ObjectId,
87
- ref: "Measurements",
88
- },
89
- ],
90
- },
91
- {
92
- timestamps: true,
93
- }
94
- );
95
-
96
- const AlertsAudit = mongoose.model("AlertsAudit", alertsAuditSchema);
97
-
98
- // Fix the typo in the hook name - 'sindOneAndDelete' to 'findOneAndDelete'
99
- alertsSchema.pre("findOneAndDelete", async function () {
100
- const docToDelete = await this.model.findOne(this.getFilter());
101
- if (docToDelete) {
102
- console.log("Logging findOneAndDelete to audit", docToDelete);
103
- const auditLog = new AlertsAudit({
104
- timeUpdated: docToDelete.updatedAt,
105
- type: docToDelete.type,
106
- orgId: docToDelete.orgId,
107
- alertConfigurationId: docToDelete.alertConfigurationId,
108
- monitorId: docToDelete.monitorId,
109
- status: docToDelete.status,
110
- activeUntil: docToDelete.activeUntil,
111
- actionBy: docToDelete.actionBy,
112
- actionDate: docToDelete.actionDate,
113
- recipients: docToDelete.recipients,
114
- flaggedMeasurementIds: docToDelete.flaggedMeasurementIds,
115
- deletedAt: new Date(),
116
- });
117
- await auditLog.save();
118
- }
119
- });
120
-
121
- // Pre-hook to log multiple alert deletions
122
- alertsSchema.pre("deleteMany", async function () {
123
- console.log("deleteMany pre-hook triggered");
124
- const docsToDelete = await this.model.find(this.getFilter()).lean();
125
-
126
- if (docsToDelete.length) {
127
- console.log(`Logging ${docsToDelete.length} documents to audit`);
128
- const auditLogs = docsToDelete.map((doc) => ({
129
- timeUpdated: doc.updatedAt,
130
- type: doc.type,
131
- alertConfigurationId: doc.alertConfigurationId,
132
- orgId: doc.orgId,
133
- monitorId: doc.monitorId,
134
- status: doc.status,
135
- activeUntil: doc.activeUntil,
136
- actionBy: doc.actionBy,
137
- actionDate: doc.actionDate,
138
- recipients: doc.recipients,
139
- flaggedMeasurementIds: doc.flaggedMeasurementIds,
140
- deletedAt: new Date(),
141
- }));
142
-
143
- await AlertsAudit.insertMany(auditLogs);
144
- }
145
- });
146
-
147
- // Pre-hook to log a single alert deletion (for deleteOne)
148
- alertsSchema.pre("deleteOne", async function () {
149
- console.log("deleteOne pre-hook triggered");
150
- const docToDelete = await this.model.findOne(this.getFilter()).lean();
151
-
152
- if (docToDelete) {
153
- console.log("Logging deleteOne to audit", docToDelete);
154
- const auditLog = new AlertsAudit({
155
- timeUpdated: docToDelete.updatedAt,
156
- type: docToDelete.type,
157
- alertConfigurationId: docToDelete.alertConfigurationId,
158
- orgId: docToDelete.orgId,
159
- monitorId: docToDelete.monitorId,
160
- status: docToDelete.status,
161
- activeUntil: docToDelete.activeUntil,
162
- actionBy: docToDelete.actionBy,
163
- actionDate: docToDelete.actionDate,
164
- recipients: docToDelete.recipients,
165
- flaggedMeasurementIds: docToDelete.flaggedMeasurementIds,
166
- deletedAt: new Date(),
167
- });
168
- await auditLog.save();
169
- }
170
- });
171
-
172
- // Pre-hook to log single alert updates
173
- alertsSchema.pre("findOneAndUpdate", async function () {
174
- const docToUpdate = await this.model.findOne(this.getFilter()).lean();
175
- if (docToUpdate) {
176
- console.log("Logging findOneAndUpdate to audit", docToUpdate);
177
- const auditLog = new AlertsAudit({
178
- timeUpdated: docToUpdate.updatedAt,
179
- type: docToUpdate.type,
180
- alertConfigurationId: docToUpdate.alertConfigurationId,
181
- orgId: docToUpdate.orgId,
182
- monitorId: docToUpdate.monitorId,
183
- status: docToUpdate.status,
184
- activeUntil: docToUpdate.activeUntil,
185
- actionBy: docToUpdate.actionBy,
186
- actionDate: docToUpdate.actionDate,
187
- recipients: docToUpdate.recipients,
188
- flaggedMeasurementIds: docToUpdate.flaggedMeasurementIds,
189
- deletedAt: null, // No deletion happening
190
- });
191
- await auditLog.save();
192
- }
193
- });
194
-
195
- // Pre-hook to log multiple alert updates
196
- alertsSchema.pre("updateMany", async function () {
197
- const docsToUpdate = await this.model.find(this.getFilter()).lean();
198
- if (docsToUpdate.length) {
199
- console.log(`Logging ${docsToUpdate.length} documents to audit`);
200
- const auditLogs = docsToUpdate.map((doc) => ({
201
- timeUpdated: doc.updatedAt,
202
- type: doc.type,
203
- alertConfigurationId: doc.alertConfigurationId,
204
- orgId: doc.orgId,
205
- monitorId: doc.monitorId,
206
- status: doc.status,
207
- activeUntil: doc.activeUntil,
208
- actionBy: doc.actionBy,
209
- actionDate: doc.actionDate,
210
- recipients: doc.recipients,
211
- flaggedMeasurementIds: doc.flaggedMeasurementIds,
212
- deletedAt: null, // No deletion happening
213
- }));
214
-
215
- await AlertsAudit.insertMany(auditLogs);
216
- }
217
- });
218
-
219
- const Alerts = mongoose.model("Alerts", alertsSchema);
220
-
221
- export { alertsSchema, Alerts, AlertsAudit, alertsAuditSchema };
1
+ import mongoose, { Schema } from "mongoose";
2
+
3
+ const recipientSchema = new Schema(
4
+ {
5
+ adminId: {
6
+ type: mongoose.Types.ObjectId,
7
+ ref: "Admin",
8
+ },
9
+ typeOfCommunication: String,
10
+ dateCommunicated: {
11
+ type: Date,
12
+ defult: Date.now,
13
+ },
14
+ },
15
+ { _id: false }
16
+ );
17
+
18
+ const alertsSchema = mongoose.Schema(
19
+ {
20
+ type: {
21
+ type: String,
22
+ enum: ["Data Review"],
23
+ },
24
+ alertConfigurationId: mongoose.Types.ObjectId,
25
+ flagType: String,
26
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
27
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
28
+ status: {
29
+ type: String,
30
+ enum: ["Active", "Ack", "Mute"],
31
+ },
32
+ activeUntil: { type: Date },
33
+ actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
34
+ actionDate: { type: Date, default: Date.now },
35
+ recipients: [recipientSchema],
36
+ metaData: Object,
37
+ flaggedDataCompleteness: [Object],
38
+ flaggedMeasurementIds: [
39
+ {
40
+ type: mongoose.Types.ObjectId,
41
+ ref: "Measurements",
42
+ },
43
+ ],
44
+ },
45
+ {
46
+ timestamps: true,
47
+ }
48
+ );
49
+
50
+ // Organization-based queries with timestamp ordering
51
+ alertsSchema.index({ orgId: 1, updatedAt: -1 });
52
+ // Monitor-based queries for lookups with timestamp ordering
53
+ alertsSchema.index({ monitorId: 1, updatedAt: -1 });
54
+ // Status-based filtering with organization and timestamp
55
+ alertsSchema.index({ orgId: 1, status: 1, updatedAt: -1 });
56
+ // Alert configuration matching - already optimal
57
+ alertsSchema.index({ alertConfigurationId: 1 });
58
+
59
+ // Keep individual indexes for backward compatibility
60
+ alertsSchema.index({ orgId: 1 });
61
+ alertsSchema.index({ monitorId: 1 });
62
+ alertsSchema.index({ type: 1 });
63
+
64
+ const alertsAuditSchema = mongoose.Schema(
65
+ {
66
+ timeUpdated: Date,
67
+ deletedAt: { type: Date, default: Date.now },
68
+ type: {
69
+ type: String,
70
+ enum: ["Data Review"],
71
+ },
72
+ alertConfigurationId: mongoose.Types.ObjectId,
73
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
74
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
75
+ status: {
76
+ type: String,
77
+ enum: ["Active", "Ack", "Mute"],
78
+ },
79
+ activeUntil: {type: Date},
80
+ actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
81
+ actionDate: { type: Date, default: Date.now },
82
+ recipients: [recipientSchema],
83
+ metaData: Object,
84
+ flaggedMeasurementIds: [
85
+ {
86
+ type: mongoose.Types.ObjectId,
87
+ ref: "Measurements",
88
+ },
89
+ ],
90
+ },
91
+ {
92
+ timestamps: true,
93
+ }
94
+ );
95
+
96
+ const AlertsAudit = mongoose.model("AlertsAudit", alertsAuditSchema);
97
+
98
+ // Fix the typo in the hook name - 'sindOneAndDelete' to 'findOneAndDelete'
99
+ alertsSchema.pre("findOneAndDelete", async function () {
100
+ const docToDelete = await this.model.findOne(this.getFilter());
101
+ if (docToDelete) {
102
+ console.log("Logging findOneAndDelete to audit", docToDelete);
103
+ const auditLog = new AlertsAudit({
104
+ timeUpdated: docToDelete.updatedAt,
105
+ type: docToDelete.type,
106
+ orgId: docToDelete.orgId,
107
+ alertConfigurationId: docToDelete.alertConfigurationId,
108
+ monitorId: docToDelete.monitorId,
109
+ status: docToDelete.status,
110
+ activeUntil: docToDelete.activeUntil,
111
+ actionBy: docToDelete.actionBy,
112
+ actionDate: docToDelete.actionDate,
113
+ recipients: docToDelete.recipients,
114
+ flaggedMeasurementIds: docToDelete.flaggedMeasurementIds,
115
+ deletedAt: new Date(),
116
+ });
117
+ await auditLog.save();
118
+ }
119
+ });
120
+
121
+ // Pre-hook to log multiple alert deletions
122
+ alertsSchema.pre("deleteMany", async function () {
123
+ console.log("deleteMany pre-hook triggered");
124
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
125
+
126
+ if (docsToDelete.length) {
127
+ console.log(`Logging ${docsToDelete.length} documents to audit`);
128
+ const auditLogs = docsToDelete.map((doc) => ({
129
+ timeUpdated: doc.updatedAt,
130
+ type: doc.type,
131
+ alertConfigurationId: doc.alertConfigurationId,
132
+ orgId: doc.orgId,
133
+ monitorId: doc.monitorId,
134
+ status: doc.status,
135
+ activeUntil: doc.activeUntil,
136
+ actionBy: doc.actionBy,
137
+ actionDate: doc.actionDate,
138
+ recipients: doc.recipients,
139
+ flaggedMeasurementIds: doc.flaggedMeasurementIds,
140
+ deletedAt: new Date(),
141
+ }));
142
+
143
+ await AlertsAudit.insertMany(auditLogs);
144
+ }
145
+ });
146
+
147
+ // Pre-hook to log a single alert deletion (for deleteOne)
148
+ alertsSchema.pre("deleteOne", async function () {
149
+ console.log("deleteOne pre-hook triggered");
150
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
151
+
152
+ if (docToDelete) {
153
+ console.log("Logging deleteOne to audit", docToDelete);
154
+ const auditLog = new AlertsAudit({
155
+ timeUpdated: docToDelete.updatedAt,
156
+ type: docToDelete.type,
157
+ alertConfigurationId: docToDelete.alertConfigurationId,
158
+ orgId: docToDelete.orgId,
159
+ monitorId: docToDelete.monitorId,
160
+ status: docToDelete.status,
161
+ activeUntil: docToDelete.activeUntil,
162
+ actionBy: docToDelete.actionBy,
163
+ actionDate: docToDelete.actionDate,
164
+ recipients: docToDelete.recipients,
165
+ flaggedMeasurementIds: docToDelete.flaggedMeasurementIds,
166
+ deletedAt: new Date(),
167
+ });
168
+ await auditLog.save();
169
+ }
170
+ });
171
+
172
+ // Pre-hook to log single alert updates
173
+ alertsSchema.pre("findOneAndUpdate", async function () {
174
+ const docToUpdate = await this.model.findOne(this.getFilter()).lean();
175
+ if (docToUpdate) {
176
+ console.log("Logging findOneAndUpdate to audit", docToUpdate);
177
+ const auditLog = new AlertsAudit({
178
+ timeUpdated: docToUpdate.updatedAt,
179
+ type: docToUpdate.type,
180
+ alertConfigurationId: docToUpdate.alertConfigurationId,
181
+ orgId: docToUpdate.orgId,
182
+ monitorId: docToUpdate.monitorId,
183
+ status: docToUpdate.status,
184
+ activeUntil: docToUpdate.activeUntil,
185
+ actionBy: docToUpdate.actionBy,
186
+ actionDate: docToUpdate.actionDate,
187
+ recipients: docToUpdate.recipients,
188
+ flaggedMeasurementIds: docToUpdate.flaggedMeasurementIds,
189
+ deletedAt: null, // No deletion happening
190
+ });
191
+ await auditLog.save();
192
+ }
193
+ });
194
+
195
+ // Pre-hook to log multiple alert updates
196
+ alertsSchema.pre("updateMany", async function () {
197
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
198
+ if (docsToUpdate.length) {
199
+ console.log(`Logging ${docsToUpdate.length} documents to audit`);
200
+ const auditLogs = docsToUpdate.map((doc) => ({
201
+ timeUpdated: doc.updatedAt,
202
+ type: doc.type,
203
+ alertConfigurationId: doc.alertConfigurationId,
204
+ orgId: doc.orgId,
205
+ monitorId: doc.monitorId,
206
+ status: doc.status,
207
+ activeUntil: doc.activeUntil,
208
+ actionBy: doc.actionBy,
209
+ actionDate: doc.actionDate,
210
+ recipients: doc.recipients,
211
+ flaggedMeasurementIds: doc.flaggedMeasurementIds,
212
+ deletedAt: null, // No deletion happening
213
+ }));
214
+
215
+ await AlertsAudit.insertMany(auditLogs);
216
+ }
217
+ });
218
+
219
+ const Alerts = mongoose.model("Alerts", alertsSchema);
220
+
221
+ export { alertsSchema, Alerts, AlertsAudit, alertsAuditSchema };
@@ -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
+
@@ -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};
@@ -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};