@justair/justair-library 4.1.0-delta → 4.2.0-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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justair/justair-library",
3
- "version": "4.1.0-delta",
3
+ "version": "4.2.0-alpha",
4
4
  "description": "JustAir Internal Library",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -13,7 +13,12 @@ import {
13
13
  monitorRequestsSchema,
14
14
  MonitorRequests,
15
15
  } from "./models/monitorRequests.js";
16
- import { monitorsSchema, Monitors, monitorAuditSchema, MonitorAudit } from "./models/monitors.js";
16
+ import {
17
+ monitorsSchema,
18
+ Monitors,
19
+ monitorAuditSchema,
20
+ MonitorAudit,
21
+ } from "./models/monitors.js";
17
22
  import { organizationsSchema, Organizations } from "./models/organizations.js";
18
23
  import {
19
24
  referenceMonitorInfoSchema,
@@ -41,6 +46,12 @@ import { announcementSchema, Announcements } from "./models/announcements.js";
41
46
  import { jobsSchema, Jobs } from "./models/jobs.js";
42
47
  import { apiKeySchema, ApiKey } from "./models/apiKey.js";
43
48
  import { UsageMetrics, usageMetricsSchema } from "./models/usageMetrics.js";
49
+ import {
50
+ AlertsAudit,
51
+ Alerts,
52
+ alertsSchema,
53
+ alertsAuditSchema,
54
+ } from "./models/alerts.js";
44
55
  import Database from "./config/db.js"; // Import the new Database class
45
56
  import CustomLogger from "./config/logger.js";
46
57
 
@@ -91,5 +102,9 @@ export {
91
102
  EventsAudit,
92
103
  eventsAuditSchema,
93
104
  MonitorAudit,
94
- monitorAuditSchema
105
+ monitorAuditSchema,
106
+ AlertsAudit,
107
+ Alerts,
108
+ alertsSchema,
109
+ alertsAuditSchema,
95
110
  };
@@ -0,0 +1,173 @@
1
+ import mongoose, { Schema } from "mongoose";
2
+
3
+ const recipientSchema = new Schema({
4
+ adminId: {
5
+ type: mongoose.Types.ObjectId,
6
+ ref: "Admin",
7
+ },
8
+ typeOfCommunication: String,
9
+ dateCommunicated: {
10
+ type: Date,
11
+ defult: Date.now,
12
+ },
13
+ });
14
+
15
+ const alertsSchema = mongoose.Schema(
16
+ {
17
+ type: {
18
+ type: String,
19
+ enum: ["Data Review"],
20
+ },
21
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
22
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
23
+ status: {
24
+ type: String,
25
+ enum: ["Active", "Ack", "Mute"],
26
+ },
27
+ actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
28
+ actionDate: { type: Date, default: Date.now },
29
+ recipents: [recipientSchema],
30
+ },
31
+ {
32
+ timestamps: true,
33
+ }
34
+ );
35
+
36
+ alertsSchema.index({ orgId: 1 });
37
+ alertsSchema.index({ monitorId: 1 });
38
+ alertsSchema.index({ type: 1 });
39
+
40
+ const alertsAuditSchema = mongoose.Schema(
41
+ {
42
+ timeUpdated: Date,
43
+ deletedAt: { type: Date, default: Date.now },
44
+ type: {
45
+ type: String,
46
+ enum: ["Data Review"],
47
+ },
48
+ orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
49
+ monitorId: { type: mongoose.Types.ObjectId, ref: "Monitors" },
50
+ status: {
51
+ type: String,
52
+ enum: ["Active", "Ack", "Mute"],
53
+ },
54
+ actionBy: { type: mongoose.Types.ObjectId, ref: "Admin" },
55
+ actionDate: { type: Date, default: Date.now },
56
+ recipents: [recipientSchema],
57
+ },
58
+ {
59
+ timestamps: true,
60
+ }
61
+ );
62
+
63
+ // Pre-hook to log single alert deletions
64
+ alertsSchema.pre("findOneAndDelete", async function () {
65
+ const docToDelete = await this.model.findOne(this.getFilter());
66
+ if (docToDelete) {
67
+ console.log("Logging findOneAndDelete to audit", docToDelete);
68
+ const auditLog = new AlertsAudit({
69
+ timeUpdated: docToDelete.updatedAt,
70
+ type: docToDelete.type,
71
+ orgId: docToDelete.orgId,
72
+ monitorId: docToDelete.monitorId,
73
+ status: docToDelete.status,
74
+ actionBy: docToDelete.actionBy,
75
+ actionDate: docToDelete.actionDate,
76
+ recipients: docToDelete.recipients,
77
+ deletedAt: new Date(),
78
+ });
79
+ await auditLog.save();
80
+ }
81
+ });
82
+
83
+ // Pre-hook to log multiple alert deletions
84
+ alertsSchema.pre("deleteMany", async function () {
85
+ console.log("deleteMany pre-hook triggered");
86
+ const docsToDelete = await this.model.find(this.getFilter()).lean();
87
+
88
+ if (docsToDelete.length) {
89
+ console.log(`Logging ${docsToDelete.length} documents to audit`);
90
+ const auditLogs = docsToDelete.map((doc) => ({
91
+ timeUpdated: doc.updatedAt,
92
+ type: doc.type,
93
+ orgId: doc.orgId,
94
+ monitorId: doc.monitorId,
95
+ status: doc.status,
96
+ actionBy: doc.actionBy,
97
+ actionDate: doc.actionDate,
98
+ recipients: doc.recipients,
99
+ deletedAt: new Date(),
100
+ }));
101
+
102
+ await AlertsAudit.insertMany(auditLogs);
103
+ }
104
+ });
105
+
106
+ // Pre-hook to log a single alert deletion (for deleteOne)
107
+ alertsSchema.pre("deleteOne", async function () {
108
+ console.log("deleteOne pre-hook triggered");
109
+ const docToDelete = await this.model.findOne(this.getFilter()).lean();
110
+
111
+ if (docToDelete) {
112
+ console.log("Logging deleteOne to audit", docToDelete);
113
+ const auditLog = new AlertsAudit({
114
+ timeUpdated: docToDelete.updatedAt,
115
+ type: docToDelete.type,
116
+ orgId: docToDelete.orgId,
117
+ monitorId: docToDelete.monitorId,
118
+ status: docToDelete.status,
119
+ actionBy: docToDelete.actionBy,
120
+ actionDate: docToDelete.actionDate,
121
+ recipients: docToDelete.recipients,
122
+ deletedAt: new Date(),
123
+ });
124
+ await auditLog.save();
125
+ }
126
+ });
127
+
128
+ // Pre-hook to log single alert updates
129
+ alertsSchema.pre("findOneAndUpdate", async function () {
130
+ const docToUpdate = await this.model.findOne(this.getFilter()).lean();
131
+ if (docToUpdate) {
132
+ console.log("Logging findOneAndUpdate to audit", docToUpdate);
133
+ const auditLog = new AlertsAudit({
134
+ timeUpdated: docToUpdate.updatedAt,
135
+ type: docToUpdate.type,
136
+ orgId: docToUpdate.orgId,
137
+ monitorId: docToUpdate.monitorId,
138
+ status: docToUpdate.status,
139
+ actionBy: docToUpdate.actionBy,
140
+ actionDate: docToUpdate.actionDate,
141
+ recipients: docToUpdate.recipients,
142
+ deletedAt: null, // No deletion happening
143
+ });
144
+ await auditLog.save();
145
+ }
146
+ });
147
+
148
+ // Pre-hook to log multiple alert updates
149
+ alertsSchema.pre("updateMany", async function () {
150
+ const docsToUpdate = await this.model.find(this.getFilter()).lean();
151
+ if (docsToUpdate.length) {
152
+ console.log(`Logging ${docsToUpdate.length} documents to audit`);
153
+ const auditLogs = docsToUpdate.map((doc) => ({
154
+ timeUpdated: doc.updatedAt,
155
+ type: doc.type,
156
+ orgId: doc.orgId,
157
+ monitorId: doc.monitorId,
158
+ status: doc.status,
159
+ actionBy: doc.actionBy,
160
+ actionDate: doc.actionDate,
161
+ recipients: doc.recipients,
162
+ deletedAt: null, // No deletion happening
163
+ }));
164
+
165
+ await AlertsAudit.insertMany(auditLogs);
166
+ }
167
+ });
168
+
169
+ const AlertsAudit = mongoose.model("Audit", alertsAuditSchema);
170
+
171
+ const Alerts = mongoose.model("Alerts", alertsSchema);
172
+
173
+ export { alertsSchema, Alerts, AlertsAudit, alertsAuditSchema };
@@ -1,5 +1,19 @@
1
1
  import mongoose, { Schema } from "mongoose";
2
2
 
3
+ const alertSchema = new Schema({
4
+ type: {
5
+ type: String,
6
+ enum: ["Data Review"],
7
+ },
8
+ metadata: Object,
9
+ frequency: Object,
10
+ communicationType: [String],
11
+ recipients: {
12
+ type: mongoose.Types.ObjectId,
13
+ ref: "Admin",
14
+ },
15
+ });
16
+
3
17
  const correctionSchema = new Schema(
4
18
  {
5
19
  correctionName: String,
@@ -35,6 +49,7 @@ const organizationsSchema = mongoose.Schema(
35
49
  weeklyReportData: [Object],
36
50
  isActive: { type: Boolean, default: true },
37
51
  correctionRules: [correctionSchema],
52
+ alertConfigurations: [alertSchema],
38
53
  },
39
54
  {
40
55
  timestamps: true,