@dynamatix/gb-schemas 0.23.2 → 0.23.4
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/lookup.service.js +55 -0
- package/package.json +4 -3
- package/utils/encryption.middleware.js +49 -13
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import LookupGroupModel from "./shared/lookup-group.model.js";
|
|
3
|
+
import LookupModel from "./shared/lookup.model.js";
|
|
4
|
+
import { encryptObject } from "./utils/encryption.js";
|
|
5
|
+
|
|
6
|
+
export class LookupService {
|
|
7
|
+
static async getLookupID(groupName, lookupName) {
|
|
8
|
+
// console.log("--------------groupName, lookupName",groupName, lookupName);
|
|
9
|
+
try {
|
|
10
|
+
let lookupGroup = await LookupGroupModel.find().populate('lookups').exec();
|
|
11
|
+
if (!lookupGroup) {
|
|
12
|
+
console.log(`Lookup group ${groupName} not found, creating`)
|
|
13
|
+
lookupGroup = await LookupGroupModel.create({ name: groupName })
|
|
14
|
+
console.log(`Lookup group ${groupName} not found, created`)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let lookup = await lookupGroup.lookups?.find(lookup => lookup.name === lookupName);
|
|
18
|
+
if (!lookup) {
|
|
19
|
+
console.log(`Lookup ${lookupName} not found for ${groupName}, creating`);
|
|
20
|
+
lookup = await LookupModel.create({
|
|
21
|
+
lookupGroupId: lookupGroup._id,
|
|
22
|
+
name: lookupName,
|
|
23
|
+
text: lookupName
|
|
24
|
+
});
|
|
25
|
+
lookupGroup.lookups.push(lookup._id);
|
|
26
|
+
await lookupGroup.save();
|
|
27
|
+
console.log(`Lookup ${lookupName} not found for ${groupName}, created`)
|
|
28
|
+
}
|
|
29
|
+
return lookup._id;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error(error);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
const mongoConnectionString = 'mongodb+srv://gatehouse-uat:j6GX4cXBGFxDVXc1@gatehouse-uat.bvfhdhn.mongodb.net/gatehouse-uat-e?retryWrites=true&w=majority&appName=gatehouse-uat-e'
|
|
39
|
+
|
|
40
|
+
const connectDB = async () => {
|
|
41
|
+
try {
|
|
42
|
+
await mongoose.connect(mongoConnectionString, {
|
|
43
|
+
useNewUrlParser: true,
|
|
44
|
+
useUnifiedTopology: true
|
|
45
|
+
});
|
|
46
|
+
console.log('Connected to MongoDB');
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('MongoDB connection error:', error);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
await connectDB();
|
|
54
|
+
const result = await LookupService.getLookupID('ApplicationType','Private Limited');
|
|
55
|
+
console.log(result);
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamatix/gb-schemas",
|
|
3
|
-
"version": "0.23.
|
|
4
|
-
"description": "All the schemas for gatehouse bank back-end
|
|
5
|
-
"main": "
|
|
3
|
+
"version": "0.23.4",
|
|
4
|
+
"description": "All the schemas for gatehouse bank back-end",
|
|
5
|
+
"main": "lookup.service.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"./properties": "./properties/index.js",
|
|
28
28
|
"./users": "./users/index.js",
|
|
29
29
|
"./product-catalogues": "./product-catalogues/index.js",
|
|
30
|
+
"./underwriters": "./underwriters/index.js",
|
|
30
31
|
"./utils": "./utils/index.js"
|
|
31
32
|
}
|
|
32
33
|
}
|
|
@@ -62,23 +62,59 @@ export default function mongooseEncryption(schema, options) {
|
|
|
62
62
|
next();
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
const decryptDocument = (doc, collectionName) => {
|
|
66
|
+
if (!doc || doc.$locals.hasDecrypted) return doc; // ✅ Skip if already decrypted
|
|
67
|
+
|
|
68
|
+
// ✅ Convert document to object before decrypting
|
|
69
|
+
const decrypted = decryptObject(doc.toObject(), collectionName);
|
|
70
|
+
|
|
71
|
+
// ✅ Preserve and decrypt populated fields
|
|
72
|
+
Object.keys(doc.toObject()).forEach((key) => {
|
|
73
|
+
if (doc.populated && doc.populated(key)) {
|
|
74
|
+
if (Array.isArray(doc[key])) {
|
|
75
|
+
decrypted[key] = doc[key]
|
|
76
|
+
// ✅ Decrypt each populated document in an array
|
|
77
|
+
// decrypted[key] = doc[key].map((subDoc) =>
|
|
78
|
+
// subDoc.toObject ? decryptObject(subDoc.toObject(), "LookupModel") : subDoc
|
|
79
|
+
// );
|
|
80
|
+
} else {
|
|
81
|
+
// ✅ Decrypt a single populated document
|
|
82
|
+
decrypted[key] = doc[key]?.toObject
|
|
83
|
+
? decryptObject(doc[key].toObject(), "LookupModel")
|
|
84
|
+
: doc[key];
|
|
73
85
|
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// ✅ Set the flag to prevent duplicate decryption
|
|
90
|
+
doc.$locals.hasDecrypted = true;
|
|
91
|
+
|
|
92
|
+
return Object.assign(doc, decrypted); // ✅ Update the document safely
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// ✅ Middleware for find()
|
|
96
|
+
schema.post(['find'], function (docs, next) {
|
|
97
|
+
if (!docs || !Array.isArray(docs)) return next(); // ✅ Skip if no document found
|
|
98
|
+
|
|
99
|
+
docs.forEach((doc, index) => {
|
|
100
|
+
docs[index] = decryptDocument(doc, this.collection.name);
|
|
101
|
+
});
|
|
102
|
+
|
|
79
103
|
next();
|
|
80
104
|
});
|
|
81
105
|
|
|
106
|
+
// ✅ Middleware for findOne() and findById()
|
|
107
|
+
schema.post(['findOne', 'findById'], function (doc, next) {
|
|
108
|
+
if (!doc) return next(); // ✅ Skip if no document found
|
|
109
|
+
|
|
110
|
+
doc = decryptDocument(doc, this.collection.name);
|
|
111
|
+
|
|
112
|
+
next();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
82
118
|
// Decrypt after update operations (single or multiple documents)
|
|
83
119
|
schema.post("update", function (result) {
|
|
84
120
|
if (Array.isArray(result)) {
|