@justair/justair-library 4.8.19 → 4.8.21
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 +1 -1
- package/src/models/sampleSites.js +252 -0
package/package.json
CHANGED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
const sampleParametersEnum = [
|
|
3
|
+
"C6H6", // Benzene
|
|
4
|
+
"PB", // Lead
|
|
5
|
+
"AS", // Arsenic
|
|
6
|
+
"CD", // Cadmium
|
|
7
|
+
"CR", // Chromium
|
|
8
|
+
"NI", // Nickel
|
|
9
|
+
"BA", // Barium
|
|
10
|
+
"FE", // Iron
|
|
11
|
+
"CU", // Copper
|
|
12
|
+
"ZN", // Zinc
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const noteSchema = mongoose.Schema({
|
|
16
|
+
note: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true,
|
|
19
|
+
},
|
|
20
|
+
type: {
|
|
21
|
+
type: String,
|
|
22
|
+
required: true,
|
|
23
|
+
},
|
|
24
|
+
adminId: {
|
|
25
|
+
type: mongoose.Types.ObjectId,
|
|
26
|
+
ref: "Admin",
|
|
27
|
+
required: true,
|
|
28
|
+
},
|
|
29
|
+
adminName: {
|
|
30
|
+
type: String,
|
|
31
|
+
},
|
|
32
|
+
date: {
|
|
33
|
+
type: Date,
|
|
34
|
+
default: Date.now,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Sample Site Audit Schema
|
|
39
|
+
const sampleSiteAuditSchema = mongoose.Schema(
|
|
40
|
+
{
|
|
41
|
+
sampleSiteId: { type: mongoose.Types.ObjectId, ref: "SampleSites" },
|
|
42
|
+
orgId: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
43
|
+
timeUpdated: Date,
|
|
44
|
+
deletedAt: { type: Date, default: Date.now }, // Only populated on delete
|
|
45
|
+
sampleSiteLocation: {
|
|
46
|
+
// Sample Site GPS location (lat, lon)
|
|
47
|
+
type: { type: String, enum: ["Point"], required: true },
|
|
48
|
+
coordinates: { type: [Number], required: true },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
timestamps: true,
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// Create the SampleSiteAudit model
|
|
57
|
+
const SampleSiteAudit = mongoose.model("SampleSiteAudit", sampleSiteAuditSchema);
|
|
58
|
+
|
|
59
|
+
// Sample Sites Schema
|
|
60
|
+
const sampleSitesSchema = mongoose.Schema(
|
|
61
|
+
{
|
|
62
|
+
name: { type: String },
|
|
63
|
+
managedBy: { type: String },
|
|
64
|
+
sponsoredBy: { type: String },
|
|
65
|
+
lastSamplePeriodStartDate: { type: Date },
|
|
66
|
+
lastSamplePeriodEndDate: { type: Date },
|
|
67
|
+
isPrivate: { type: Boolean, default: false, required: true },
|
|
68
|
+
sponsor: { type: mongoose.Types.ObjectId, ref: "Organizations" },
|
|
69
|
+
sponsorName: { type: String },
|
|
70
|
+
/*
|
|
71
|
+
* Location object: unified structure for all address and geospatial data.
|
|
72
|
+
* - monitorLatitude and monitorLongitude are deprecated and no longer used.
|
|
73
|
+
* - The location object (including location.gps) is now built in config/google.js.
|
|
74
|
+
* - location.gps is a GeoJSON Point used for all geospatial queries (replaces gpsLocation).
|
|
75
|
+
*/
|
|
76
|
+
location: {
|
|
77
|
+
city: { type: String },
|
|
78
|
+
state: { type: String },
|
|
79
|
+
county: { type: String },
|
|
80
|
+
street: { type: String },
|
|
81
|
+
zip_code: { type: String },
|
|
82
|
+
neighborhood: { type: String },
|
|
83
|
+
gps: {
|
|
84
|
+
type: {
|
|
85
|
+
type: String,
|
|
86
|
+
enum: ["Point"],
|
|
87
|
+
required: true,
|
|
88
|
+
},
|
|
89
|
+
coordinates: {
|
|
90
|
+
type: [Number],
|
|
91
|
+
required: true,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
context: { type: [String] },
|
|
96
|
+
parameters: [
|
|
97
|
+
{
|
|
98
|
+
type: String,
|
|
99
|
+
enum: sampleParametersEnum,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
notes: { type: [noteSchema] },
|
|
103
|
+
image: { type: String },
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
timestamps: true,
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Geographic queries - already exists
|
|
111
|
+
sampleSitesSchema.index({ "location.gps": "2dsphere" }); // use location.gps for geospatial queries
|
|
112
|
+
|
|
113
|
+
// Sponsor-based queries
|
|
114
|
+
sampleSitesSchema.index({ sponsor: 1, isPrivate: 1 });
|
|
115
|
+
|
|
116
|
+
// Location-based filtering
|
|
117
|
+
sampleSitesSchema.index({ "location.city": 1, "location.state": 1 });
|
|
118
|
+
sampleSitesSchema.index({ "location.neighborhood": 1 });
|
|
119
|
+
sampleSitesSchema.index({ "location.county": 1 });
|
|
120
|
+
|
|
121
|
+
// Query parameter filtering
|
|
122
|
+
sampleSitesSchema.index({ context: 1 });
|
|
123
|
+
sampleSitesSchema.index({ parameters: 1 });
|
|
124
|
+
|
|
125
|
+
// Pre-hook to log single document deletions
|
|
126
|
+
sampleSitesSchema.pre("findOneAndDelete", async function () {
|
|
127
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
128
|
+
if (docToDelete) {
|
|
129
|
+
console.log("Logging findOneAndDelete to sample site audit", docToDelete);
|
|
130
|
+
let coordinates = null;
|
|
131
|
+
if (docToDelete.location && docToDelete.location.gps && Array.isArray(docToDelete.location.gps.coordinates)) {
|
|
132
|
+
coordinates = docToDelete.location.gps.coordinates;
|
|
133
|
+
} else if (docToDelete.gpsLocation && Array.isArray(docToDelete.gpsLocation.coordinates)) {
|
|
134
|
+
coordinates = docToDelete.gpsLocation.coordinates;
|
|
135
|
+
}
|
|
136
|
+
if (coordinates) {
|
|
137
|
+
const auditLog = new SampleSiteAudit({
|
|
138
|
+
sampleSiteId: docToDelete._id,
|
|
139
|
+
orgId: docToDelete.sponsor,
|
|
140
|
+
timeUpdated: docToDelete.updatedAt,
|
|
141
|
+
sampleSiteLocation: {
|
|
142
|
+
type: "Point",
|
|
143
|
+
coordinates,
|
|
144
|
+
},
|
|
145
|
+
deletedAt: new Date(),
|
|
146
|
+
});
|
|
147
|
+
await auditLog.save();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Pre-hook to log multiple document deletions
|
|
153
|
+
sampleSitesSchema.pre("deleteMany", async function () {
|
|
154
|
+
console.log("deleteMany pre-hook triggered for sample sites");
|
|
155
|
+
const docsToDelete = await this.model.find(this.getFilter()).lean();
|
|
156
|
+
|
|
157
|
+
if (docsToDelete.length) {
|
|
158
|
+
console.log(`Logging ${docsToDelete.length} sample site documents to audit`);
|
|
159
|
+
const auditLogs = docsToDelete.map((doc) => {
|
|
160
|
+
let coordinates = null;
|
|
161
|
+
if (doc.location && doc.location.gps && Array.isArray(doc.location.gps.coordinates)) {
|
|
162
|
+
coordinates = doc.location.gps.coordinates;
|
|
163
|
+
} else if (doc.gpsLocation && Array.isArray(doc.gpsLocation.coordinates)) {
|
|
164
|
+
coordinates = doc.gpsLocation.coordinates;
|
|
165
|
+
}
|
|
166
|
+
if (coordinates) {
|
|
167
|
+
return {
|
|
168
|
+
sampleSiteId: doc._id,
|
|
169
|
+
orgId: doc.sponsor,
|
|
170
|
+
timeUpdated: doc.updatedAt,
|
|
171
|
+
sampleSiteLocation: {
|
|
172
|
+
type: "Point",
|
|
173
|
+
coordinates,
|
|
174
|
+
},
|
|
175
|
+
deletedAt: new Date(),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}).filter(Boolean);
|
|
180
|
+
|
|
181
|
+
if (auditLogs.length) {
|
|
182
|
+
await SampleSiteAudit.insertMany(auditLogs);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Pre-hook to log a single document deletion (for deleteOne)
|
|
188
|
+
sampleSitesSchema.pre("deleteOne", async function () {
|
|
189
|
+
console.log("deleteOne pre-hook triggered for sample sites");
|
|
190
|
+
const docToDelete = await this.model.findOne(this.getFilter()).lean();
|
|
191
|
+
|
|
192
|
+
if (docToDelete) {
|
|
193
|
+
console.log("Logging deleteOne to sample site audit", docToDelete);
|
|
194
|
+
let coordinates = null;
|
|
195
|
+
if (docToDelete.location && docToDelete.location.gps && Array.isArray(docToDelete.location.gps.coordinates)) {
|
|
196
|
+
coordinates = docToDelete.location.gps.coordinates;
|
|
197
|
+
} else if (docToDelete.gpsLocation && Array.isArray(docToDelete.gpsLocation.coordinates)) {
|
|
198
|
+
coordinates = docToDelete.gpsLocation.coordinates;
|
|
199
|
+
}
|
|
200
|
+
if (coordinates) {
|
|
201
|
+
const auditLog = new SampleSiteAudit({
|
|
202
|
+
sampleSiteId: docToDelete._id,
|
|
203
|
+
orgId: docToDelete.sponsor,
|
|
204
|
+
timeUpdated: docToDelete.updatedAt,
|
|
205
|
+
sampleSiteLocation: {
|
|
206
|
+
type: "Point",
|
|
207
|
+
coordinates,
|
|
208
|
+
},
|
|
209
|
+
deletedAt: new Date(),
|
|
210
|
+
});
|
|
211
|
+
await auditLog.save();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Pre-hook to log multiple document updates
|
|
217
|
+
sampleSitesSchema.pre("updateMany", async function () {
|
|
218
|
+
const docsToUpdate = await this.model.find(this.getFilter()).lean();
|
|
219
|
+
if (docsToUpdate.length) {
|
|
220
|
+
console.log(`Logging ${docsToUpdate.length} sample site documents to audit`);
|
|
221
|
+
const auditLogs = docsToUpdate.map((doc) => {
|
|
222
|
+
let coordinates = null;
|
|
223
|
+
if (doc.location && doc.location.gps && Array.isArray(doc.location.gps.coordinates)) {
|
|
224
|
+
coordinates = doc.location.gps.coordinates;
|
|
225
|
+
} else if (doc.gpsLocation && Array.isArray(doc.gpsLocation.coordinates)) {
|
|
226
|
+
coordinates = doc.gpsLocation.coordinates;
|
|
227
|
+
}
|
|
228
|
+
if (coordinates) {
|
|
229
|
+
return {
|
|
230
|
+
sampleSiteId: doc._id,
|
|
231
|
+
orgId: doc.sponsor,
|
|
232
|
+
timeUpdated: doc.updatedAt,
|
|
233
|
+
sampleSiteLocation: {
|
|
234
|
+
type: "Point",
|
|
235
|
+
coordinates,
|
|
236
|
+
},
|
|
237
|
+
deletedAt: null, // Not a deletion, so this field is null
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}).filter(Boolean);
|
|
242
|
+
|
|
243
|
+
if (auditLogs.length) {
|
|
244
|
+
await SampleSiteAudit.insertMany(auditLogs);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Create the SampleSites model
|
|
250
|
+
const SampleSites = mongoose.model("SampleSites", sampleSitesSchema);
|
|
251
|
+
|
|
252
|
+
export { sampleSitesSchema, SampleSites, sampleSiteAuditSchema, SampleSiteAudit, sampleParametersEnum };
|