@dynamatix/cat-shared 0.0.129 → 0.0.132
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.
|
@@ -7,6 +7,54 @@ import FormConfigurationModel from '../models/form-configuration.model.js';
|
|
|
7
7
|
|
|
8
8
|
let onAuditLogCreated = null;
|
|
9
9
|
|
|
10
|
+
// Utility function to safely access nested properties
|
|
11
|
+
function getNestedProperty(obj, path) {
|
|
12
|
+
if (!path || !obj) return undefined;
|
|
13
|
+
|
|
14
|
+
// Handle simple property access
|
|
15
|
+
if (!path.includes('.')) {
|
|
16
|
+
return obj[path];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// First check if the flattened key exists (MongoDB style: "additionalData.statusLid")
|
|
20
|
+
if (obj.hasOwnProperty(path)) {
|
|
21
|
+
return obj[path];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Then try nested object structure
|
|
25
|
+
return path.split('.').reduce((current, key) => {
|
|
26
|
+
return current && current[key] !== undefined ? current[key] : undefined;
|
|
27
|
+
}, obj);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Utility function to check if nested property exists in object
|
|
31
|
+
function hasNestedProperty(obj, path) {
|
|
32
|
+
if (!path || !obj) return false;
|
|
33
|
+
|
|
34
|
+
// Handle simple property access
|
|
35
|
+
if (!path.includes('.')) {
|
|
36
|
+
return obj.hasOwnProperty(path);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// First check if the flattened key exists (MongoDB style: "additionalData.statusLid")
|
|
40
|
+
if (obj.hasOwnProperty(path)) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Then check nested object structure
|
|
45
|
+
const keys = path.split('.');
|
|
46
|
+
let current = obj;
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
49
|
+
if (!current || !current.hasOwnProperty(keys[i])) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
current = current[keys[i]];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return current && current.hasOwnProperty(keys[keys.length - 1]);
|
|
56
|
+
}
|
|
57
|
+
|
|
10
58
|
// Optionally, define custom resolvers here
|
|
11
59
|
const customResolvers = {
|
|
12
60
|
// Example:
|
|
@@ -21,7 +69,7 @@ async function resolveExpressionDescription(doc, expression, resolverType) {
|
|
|
21
69
|
let result = expression;
|
|
22
70
|
for (const match of matches) {
|
|
23
71
|
const fieldName = match[1];
|
|
24
|
-
let value = doc
|
|
72
|
+
let value = getNestedProperty(doc, fieldName);
|
|
25
73
|
if (resolverType === 'lookup' && value && mongoose.models['Lookup']) {
|
|
26
74
|
const lookupDoc = await mongoose.models['Lookup'].findById(value).lean();
|
|
27
75
|
value = lookupDoc?.name || value;
|
|
@@ -45,18 +93,18 @@ async function resolveDescription(field, doc) {
|
|
|
45
93
|
console.log("config.descriptionResolverType", config.descriptionResolverType);
|
|
46
94
|
switch (config.descriptionResolverType) {
|
|
47
95
|
case 'lookup': {
|
|
48
|
-
const lookupId = doc
|
|
96
|
+
const lookupId = getNestedProperty(doc, config.descriptionField);
|
|
49
97
|
if (!lookupId) return '';
|
|
50
98
|
const lookupDoc = await mongoose.models['Lookup'].findById(lookupId).lean();
|
|
51
99
|
return lookupDoc?.name || '';
|
|
52
100
|
}
|
|
53
101
|
case 'direct':
|
|
54
|
-
return doc
|
|
102
|
+
return getNestedProperty(doc, config.descriptionField) || '';
|
|
55
103
|
case 'displayFieldReturn': // For case we just want to show display field value directly
|
|
56
104
|
return config.displayField || '';
|
|
57
105
|
case 'composite':
|
|
58
106
|
return (config.descriptionFields || [])
|
|
59
|
-
.map(f => doc
|
|
107
|
+
.map(f => getNestedProperty(doc, f))
|
|
60
108
|
.filter(Boolean)
|
|
61
109
|
.join(' ');
|
|
62
110
|
case 'custom':
|
|
@@ -73,7 +121,7 @@ async function resolveDescription(field, doc) {
|
|
|
73
121
|
async function resolveEntityDescription(doc, auditConfig) {
|
|
74
122
|
if (!auditConfig.descriptionResolutorForExternalData) return '';
|
|
75
123
|
const field = auditConfig.descriptionResolutorForExternalData;
|
|
76
|
-
const value = doc
|
|
124
|
+
const value = getNestedProperty(doc, field);
|
|
77
125
|
// Try to resolve via ValueReferenceMap
|
|
78
126
|
console.log("field-", field);
|
|
79
127
|
const map = await ValueReferenceMap.findOne({ field });
|
|
@@ -144,7 +192,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
144
192
|
if (auditConfig.fields?.length) {
|
|
145
193
|
for (const field of auditConfig.fields) {
|
|
146
194
|
let lookupName;
|
|
147
|
-
const newValue = doc
|
|
195
|
+
const newValue = getNestedProperty(doc, field);
|
|
148
196
|
if (field.endsWith('Lid')) {
|
|
149
197
|
const newlookupDoc = await mongoose.models['Lookup'].findById(newValue).lean();
|
|
150
198
|
if (newlookupDoc) {
|
|
@@ -217,11 +265,12 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
217
265
|
for (const field of auditConfig.fields) {
|
|
218
266
|
console.log("field", field);
|
|
219
267
|
const hasChanged =
|
|
220
|
-
update?.$set
|
|
268
|
+
(update?.$set && hasNestedProperty(update.$set, field)) ||
|
|
269
|
+
(update && hasNestedProperty(update, field));
|
|
221
270
|
console.log("hasChanged", hasChanged);
|
|
222
271
|
if (hasChanged) {
|
|
223
|
-
const newValue = update?.$set
|
|
224
|
-
const oldValue = this._originalDoc ? this._originalDoc
|
|
272
|
+
const newValue = getNestedProperty(update?.$set, field) ?? getNestedProperty(update, field);
|
|
273
|
+
const oldValue = this._originalDoc ? getNestedProperty(this._originalDoc, field) : '';
|
|
225
274
|
let lookupOldName;
|
|
226
275
|
let lookupNewName;
|
|
227
276
|
if (field.endsWith('Lid')) {
|