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