@dynamatix/cat-shared 0.0.129 → 0.0.130

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,44 @@ 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
+ // Handle nested property access
20
+ return path.split('.').reduce((current, key) => {
21
+ return current && current[key] !== undefined ? current[key] : undefined;
22
+ }, obj);
23
+ }
24
+
25
+ // Utility function to check if nested property exists in object
26
+ function hasNestedProperty(obj, path) {
27
+ if (!path || !obj) return false;
28
+
29
+ // Handle simple property access
30
+ if (!path.includes('.')) {
31
+ return obj.hasOwnProperty(path);
32
+ }
33
+
34
+ // Handle nested property access
35
+ const keys = path.split('.');
36
+ let current = obj;
37
+
38
+ for (let i = 0; i < keys.length - 1; i++) {
39
+ if (!current || !current.hasOwnProperty(keys[i])) {
40
+ return false;
41
+ }
42
+ current = current[keys[i]];
43
+ }
44
+
45
+ return current && current.hasOwnProperty(keys[keys.length - 1]);
46
+ }
47
+
10
48
  // Optionally, define custom resolvers here
11
49
  const customResolvers = {
12
50
  // Example:
@@ -21,7 +59,7 @@ async function resolveExpressionDescription(doc, expression, resolverType) {
21
59
  let result = expression;
22
60
  for (const match of matches) {
23
61
  const fieldName = match[1];
24
- let value = doc[fieldName];
62
+ let value = getNestedProperty(doc, fieldName);
25
63
  if (resolverType === 'lookup' && value && mongoose.models['Lookup']) {
26
64
  const lookupDoc = await mongoose.models['Lookup'].findById(value).lean();
27
65
  value = lookupDoc?.name || value;
@@ -45,18 +83,18 @@ async function resolveDescription(field, doc) {
45
83
  console.log("config.descriptionResolverType", config.descriptionResolverType);
46
84
  switch (config.descriptionResolverType) {
47
85
  case 'lookup': {
48
- const lookupId = doc[config.descriptionField];
86
+ const lookupId = getNestedProperty(doc, config.descriptionField);
49
87
  if (!lookupId) return '';
50
88
  const lookupDoc = await mongoose.models['Lookup'].findById(lookupId).lean();
51
89
  return lookupDoc?.name || '';
52
90
  }
53
91
  case 'direct':
54
- return doc[config.descriptionField] || '';
92
+ return getNestedProperty(doc, config.descriptionField) || '';
55
93
  case 'displayFieldReturn': // For case we just want to show display field value directly
56
94
  return config.displayField || '';
57
95
  case 'composite':
58
96
  return (config.descriptionFields || [])
59
- .map(f => doc[f])
97
+ .map(f => getNestedProperty(doc, f))
60
98
  .filter(Boolean)
61
99
  .join(' ');
62
100
  case 'custom':
@@ -73,7 +111,7 @@ async function resolveDescription(field, doc) {
73
111
  async function resolveEntityDescription(doc, auditConfig) {
74
112
  if (!auditConfig.descriptionResolutorForExternalData) return '';
75
113
  const field = auditConfig.descriptionResolutorForExternalData;
76
- const value = doc[field];
114
+ const value = getNestedProperty(doc, field);
77
115
  // Try to resolve via ValueReferenceMap
78
116
  console.log("field-", field);
79
117
  const map = await ValueReferenceMap.findOne({ field });
@@ -144,7 +182,7 @@ function applyAuditMiddleware(schema, collectionName) {
144
182
  if (auditConfig.fields?.length) {
145
183
  for (const field of auditConfig.fields) {
146
184
  let lookupName;
147
- const newValue = doc[field];
185
+ const newValue = getNestedProperty(doc, field);
148
186
  if (field.endsWith('Lid')) {
149
187
  const newlookupDoc = await mongoose.models['Lookup'].findById(newValue).lean();
150
188
  if (newlookupDoc) {
@@ -217,11 +255,12 @@ function applyAuditMiddleware(schema, collectionName) {
217
255
  for (const field of auditConfig.fields) {
218
256
  console.log("field", field);
219
257
  const hasChanged =
220
- update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field);
258
+ (update?.$set && hasNestedProperty(update.$set, field)) ||
259
+ (update && hasNestedProperty(update, field));
221
260
  console.log("hasChanged", hasChanged);
222
261
  if (hasChanged) {
223
- const newValue = update?.$set?.[field] ?? update?.[field];
224
- const oldValue = this._originalDoc ? this._originalDoc[field] : '';
262
+ const newValue = getNestedProperty(update?.$set, field) ?? getNestedProperty(update, field);
263
+ const oldValue = this._originalDoc ? getNestedProperty(this._originalDoc, field) : '';
225
264
  let lookupOldName;
226
265
  let lookupNewName;
227
266
  if (field.endsWith('Lid')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.129",
3
+ "version": "0.0.130",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"