@dynamatix/cat-shared 0.0.98 → 0.0.100
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.
|
@@ -3,6 +3,7 @@ import AuditLog from '../models/audit.model.js';
|
|
|
3
3
|
import ValueReferenceMap from '../models/value-reference-map.model.js';
|
|
4
4
|
import { getContext } from '../services/request-context.service.js';
|
|
5
5
|
import mongoose from 'mongoose';
|
|
6
|
+
import FormConfigurationModel from '../models/form-configuration.model.js';
|
|
6
7
|
|
|
7
8
|
let onAuditLogCreated = null;
|
|
8
9
|
|
|
@@ -96,12 +97,33 @@ async function resolveDeletionDescription(doc, auditConfig) {
|
|
|
96
97
|
return await resolveExpressionDescription(doc, expression, 'direct');
|
|
97
98
|
}
|
|
98
99
|
|
|
100
|
+
// Utility to build a fieldName -> dataType map from multiple formConfig.sections
|
|
101
|
+
function buildFieldTypeMapFromConfigs(formConfigs) {
|
|
102
|
+
const map = {};
|
|
103
|
+
function extractFields(sections) {
|
|
104
|
+
for (const section of sections || []) {
|
|
105
|
+
if (section.fields) {
|
|
106
|
+
for (const f of section.fields) map[f.fieldName] = f.dataType;
|
|
107
|
+
}
|
|
108
|
+
if (section.sections) extractFields(section.sections);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
for (const config of formConfigs) {
|
|
112
|
+
extractFields(config.sections);
|
|
113
|
+
}
|
|
114
|
+
return map;
|
|
115
|
+
}
|
|
116
|
+
|
|
99
117
|
function applyAuditMiddleware(schema, collectionName) {
|
|
100
118
|
// Handle creation audit
|
|
101
119
|
schema.post('save', async function (doc) {
|
|
102
120
|
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
103
121
|
if (!auditConfig?.trackCreation) return;
|
|
104
122
|
|
|
123
|
+
// Fetch all form configs and build fieldTypeMap once
|
|
124
|
+
const formConfigs = await FormConfigurationModel.find({ collectionName });
|
|
125
|
+
const fieldTypeMap = buildFieldTypeMapFromConfigs(formConfigs);
|
|
126
|
+
|
|
105
127
|
const context = getContext();
|
|
106
128
|
const userId = context?.userId || 'anonymous';
|
|
107
129
|
const contextId = context?.contextId;
|
|
@@ -123,12 +145,18 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
123
145
|
}
|
|
124
146
|
}
|
|
125
147
|
const fieldDescription = await resolveDescription(field, doc);
|
|
148
|
+
// --- Add pound prefix if needed ---
|
|
149
|
+
let displayNewValue = lookupName || newValue;
|
|
150
|
+
const dataType = fieldTypeMap[field];
|
|
151
|
+
if (dataType === 'pound' && displayNewValue != null && displayNewValue !== '') {
|
|
152
|
+
displayNewValue = `£${displayNewValue}`;
|
|
153
|
+
}
|
|
126
154
|
logs.push({
|
|
127
155
|
name: fieldDescription,
|
|
128
156
|
entity: entityDescription,
|
|
129
157
|
recordId: contextId || doc._id,
|
|
130
158
|
oldValue: null,
|
|
131
|
-
newValue:
|
|
159
|
+
newValue: displayNewValue,
|
|
132
160
|
createdBy: userId,
|
|
133
161
|
externalData: {
|
|
134
162
|
description: entityDescription,
|
|
@@ -165,6 +193,10 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
165
193
|
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
166
194
|
if (!auditConfig?.fields?.length) return;
|
|
167
195
|
|
|
196
|
+
// Fetch all form configs and build fieldTypeMap once
|
|
197
|
+
const formConfigs = await FormConfigurationModel.find({ collectionName });
|
|
198
|
+
const fieldTypeMap = buildFieldTypeMapFromConfigs(formConfigs);
|
|
199
|
+
|
|
168
200
|
const update = this.getUpdate();
|
|
169
201
|
let logs = [];
|
|
170
202
|
const context = getContext();
|
|
@@ -174,7 +206,6 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
174
206
|
const entityDescription = await resolveEntityDescription(result, auditConfig);
|
|
175
207
|
|
|
176
208
|
for (const field of auditConfig.fields) {
|
|
177
|
-
|
|
178
209
|
const hasChanged =
|
|
179
210
|
update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field);
|
|
180
211
|
|
|
@@ -199,16 +230,22 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
199
230
|
}
|
|
200
231
|
|
|
201
232
|
// Convert null/undefined to empty string for comparison
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
233
|
+
let displayOldValue = lookupOldName ?? oldValue ?? '';
|
|
234
|
+
let displayNewValue = lookupNewName ?? newValue ?? '';
|
|
235
|
+
// --- Add pound prefix if needed ---
|
|
236
|
+
const dataType = fieldTypeMap[field];
|
|
237
|
+
if (dataType === 'pound') {
|
|
238
|
+
if (displayOldValue != null && displayOldValue !== '') displayOldValue = `£${displayOldValue}`;
|
|
239
|
+
if (displayNewValue != null && displayNewValue !== '') displayNewValue = `£${displayNewValue}`;
|
|
240
|
+
}
|
|
241
|
+
if (displayOldValue !== displayNewValue) {
|
|
205
242
|
const fieldDescription = await resolveDescription(field, result);
|
|
206
243
|
logs.push({
|
|
207
244
|
name: fieldDescription,
|
|
208
245
|
entity: entityDescription,
|
|
209
246
|
recordId: contextId || result._id,
|
|
210
|
-
oldValue:
|
|
211
|
-
newValue:
|
|
247
|
+
oldValue: displayOldValue,
|
|
248
|
+
newValue: displayNewValue,
|
|
212
249
|
createdBy: userId,
|
|
213
250
|
externalData: {
|
|
214
251
|
description: entityDescription,
|