@dynamatix/cat-shared 0.0.75 → 0.0.77

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
- This is the shared library for Catura Services.
2
- # Purpose
3
-
4
- The purposes of this repository are to include:
5
- - All the models of Catura
6
- - Shared services in Catura
1
+ This is the shared library for Catura Services.
2
+ # Purpose
3
+
4
+ The purposes of this repository are to include:
5
+ - All the models of Catura
6
+ - Shared services in Catura
7
7
  - Shared middlewares in Catura
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from './models/index.js';
2
- export * from './middlewares/index.js';
1
+ export * from './models/index.js';
2
+ export * from './middlewares/index.js';
3
3
  export * from './services/index.js';
@@ -1,137 +1,137 @@
1
- import AuditConfigModel from '../models/audit-config.model.js';
2
- import AuditLog from '../models/audit.model.js';
3
- import ValueReferenceMap from '../models/value-reference-map.model.js';
4
- import { getContext } from '../services/request-context.service.js';
5
- import mongoose from 'mongoose';
6
-
7
- let onAuditLogCreated = null;
8
-
9
- // Generic resolver: fetch display values using ValueReferenceMap
10
- async function resolveAuditValues(field, oldValue, newValue) {
11
- const map = await ValueReferenceMap.findOne({ field });
12
- if (!map || (!oldValue && !newValue)) return { oldValue, newValue };
13
-
14
- const Model = mongoose.models[map.model];
15
- if (!Model) return { oldValue, newValue };
16
-
17
- const [oldDoc, newDoc] = await Promise.all([
18
- oldValue ? Model.findById(oldValue).lean() : null,
19
- newValue ? Model.findById(newValue).lean() : null
20
- ]);
21
-
22
- return {
23
- oldValue: oldDoc?.[map.displayField] || oldValue,
24
- newValue: newDoc?.[map.displayField] || newValue
25
- };
26
- }
27
-
28
- async function resolveExternalData(descriptionResolutorForExternalData, recordId) {
29
- const map = await ValueReferenceMap.findOne({ field: descriptionResolutorForExternalData });
30
- const Model = mongoose.models[map.model];
31
- if (!Model) return "";
32
- const doc = await Model.findById(recordId).lean()
33
- return doc[map.displayField];
34
- }
35
-
36
- function applyAuditMiddleware(schema, collectionName) {
37
- // Handle creation audit
38
- schema.post('save', async function (doc) {
39
- const auditConfig = await AuditConfigModel.findOne({ collectionName });
40
- if (!auditConfig?.trackCreation) return;
41
-
42
- const context = getContext();
43
- const userId = context?.userId || 'anonymous';
44
- const contextId = context?.contextId;
45
-
46
- const log = {
47
- name: `${collectionName} created`,
48
- recordId: contextId || doc._id,
49
- oldValue: null,
50
- newValue: 'Document created',
51
- createdBy: userId,
52
- contextId
53
- };
54
- if (auditConfig.descriptionResolutorForExternalData) {
55
- log.externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, doc[descriptionResolutorForExternalData])
56
- log.externalData.contextId = contextId;
57
- }
58
- await AuditLog.create(log);
59
- await updateContextAuditCount(contextId, 1);
60
-
61
- if (onAuditLogCreated) {
62
- await onAuditLogCreated([log], contextId);
63
- }
64
- });
65
-
66
- // Capture the original doc before update
67
- schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
68
- this._originalDoc = await this.model.findOne(this.getQuery()).lean();
69
- next();
70
- });
71
-
72
- // Handle update audits
73
- schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
74
- if (!result) return;
75
-
76
- const auditConfig = await AuditConfigModel.findOne({ collectionName });
77
- if (!auditConfig?.fields?.length) return;
78
-
79
- const update = this.getUpdate();
80
- const logs = [];
81
- const context = getContext();
82
- const userId = context?.userId || 'anonymous';
83
- const contextId = context?.contextId;
84
-
85
- for (const field of auditConfig.fields) {
86
- const hasChanged =
87
- update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field);
88
-
89
- if (hasChanged) {
90
- const newValue = update?.$set?.[field] ?? update?.[field];
91
- const oldValue = this._originalDoc ? this._originalDoc[field] : '';
92
-
93
- if (oldValue !== newValue) {
94
- // Resolve human-readable values
95
- const { oldValue: resolvedOld, newValue: resolvedNew } =
96
- await resolveAuditValues(field, oldValue, newValue);
97
-
98
- let externalData = {};
99
- if (auditConfig.descriptionResolutorForExternalData) {
100
- externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, result[auditConfig.descriptionResolutorForExternalData])
101
- externalData.contextId = contextId || result._id;
102
- }
103
- logs.push({
104
- name: `${collectionName}.${field}`,
105
- recordId: contextId || result._id,
106
- oldValue: resolvedOld,
107
- newValue: resolvedNew,
108
- createdBy: userId,
109
- externalData
110
- });
111
- }
112
- }
113
- }
114
-
115
- if (logs.length) {
116
- await AuditLog.insertMany(logs);
117
- await updateContextAuditCount(contextId, logs.length);
118
- if (onAuditLogCreated) {
119
- await onAuditLogCreated(logs, contextId);
120
- }
121
- }
122
- });
123
- }
124
-
125
- async function updateContextAuditCount(contextId, count) {
126
- if(!contextId) return;
127
- const model = mongoose.models['Application'];
128
- const context = await model.findById(contextId);
129
- context.newAuditRecordsCount = (context.newAuditRecordsCount || 0) + (count ? count : 1);
130
- await context.save();
131
- }
132
-
133
- export function registerAuditHook(callback) {
134
- onAuditLogCreated = callback;
135
- }
136
-
137
- export default applyAuditMiddleware;
1
+ import AuditConfigModel from '../models/audit-config.model.js';
2
+ import AuditLog from '../models/audit.model.js';
3
+ import ValueReferenceMap from '../models/value-reference-map.model.js';
4
+ import { getContext } from '../services/request-context.service.js';
5
+ import mongoose from 'mongoose';
6
+
7
+ let onAuditLogCreated = null;
8
+
9
+ // Generic resolver: fetch display values using ValueReferenceMap
10
+ async function resolveAuditValues(field, oldValue, newValue) {
11
+ const map = await ValueReferenceMap.findOne({ field });
12
+ if (!map || (!oldValue && !newValue)) return { oldValue, newValue };
13
+
14
+ const Model = mongoose.models[map.model];
15
+ if (!Model) return { oldValue, newValue };
16
+
17
+ const [oldDoc, newDoc] = await Promise.all([
18
+ oldValue ? Model.findById(oldValue).lean() : null,
19
+ newValue ? Model.findById(newValue).lean() : null
20
+ ]);
21
+
22
+ return {
23
+ oldValue: oldDoc?.[map.displayField] || oldValue,
24
+ newValue: newDoc?.[map.displayField] || newValue
25
+ };
26
+ }
27
+
28
+ async function resolveExternalData(descriptionResolutorForExternalData, recordId) {
29
+ const map = await ValueReferenceMap.findOne({ field: descriptionResolutorForExternalData });
30
+ const Model = mongoose.models[map.model];
31
+ if (!Model) return "";
32
+ const doc = await Model.findById(recordId).lean()
33
+ return doc[map.displayField];
34
+ }
35
+
36
+ function applyAuditMiddleware(schema, collectionName) {
37
+ // Handle creation audit
38
+ schema.post('save', async function (doc) {
39
+ const auditConfig = await AuditConfigModel.findOne({ collectionName });
40
+ if (!auditConfig?.trackCreation) return;
41
+
42
+ const context = getContext();
43
+ const userId = context?.userId || 'anonymous';
44
+ const contextId = context?.contextId;
45
+
46
+ const log = {
47
+ name: `${collectionName} created`,
48
+ recordId: contextId || doc._id,
49
+ oldValue: null,
50
+ newValue: 'Document created',
51
+ createdBy: userId,
52
+ contextId
53
+ };
54
+ if (auditConfig.descriptionResolutorForExternalData) {
55
+ log.externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, doc[descriptionResolutorForExternalData])
56
+ log.externalData.contextId = contextId;
57
+ }
58
+ await AuditLog.create(log);
59
+ await updateContextAuditCount(contextId, 1);
60
+
61
+ if (onAuditLogCreated) {
62
+ await onAuditLogCreated([log], contextId);
63
+ }
64
+ });
65
+
66
+ // Capture the original doc before update
67
+ schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
68
+ this._originalDoc = await this.model.findOne(this.getQuery()).lean();
69
+ next();
70
+ });
71
+
72
+ // Handle update audits
73
+ schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
74
+ if (!result) return;
75
+
76
+ const auditConfig = await AuditConfigModel.findOne({ collectionName });
77
+ if (!auditConfig?.fields?.length) return;
78
+
79
+ const update = this.getUpdate();
80
+ const logs = [];
81
+ const context = getContext();
82
+ const userId = context?.userId || 'anonymous';
83
+ const contextId = context?.contextId;
84
+
85
+ for (const field of auditConfig.fields) {
86
+ const hasChanged =
87
+ update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field);
88
+
89
+ if (hasChanged) {
90
+ const newValue = update?.$set?.[field] ?? update?.[field];
91
+ const oldValue = this._originalDoc ? this._originalDoc[field] : '';
92
+
93
+ if (oldValue !== newValue) {
94
+ // Resolve human-readable values
95
+ const { oldValue: resolvedOld, newValue: resolvedNew } =
96
+ await resolveAuditValues(field, oldValue, newValue);
97
+
98
+ let externalData = {};
99
+ if (auditConfig.descriptionResolutorForExternalData) {
100
+ externalData.description = await resolveExternalData(auditConfig.descriptionResolutorForExternalData, result[auditConfig.descriptionResolutorForExternalData])
101
+ externalData.contextId = contextId || result._id;
102
+ }
103
+ logs.push({
104
+ name: `${collectionName}.${field}`,
105
+ recordId: contextId || result._id,
106
+ oldValue: resolvedOld,
107
+ newValue: resolvedNew,
108
+ createdBy: userId,
109
+ externalData
110
+ });
111
+ }
112
+ }
113
+ }
114
+
115
+ if (logs.length) {
116
+ await AuditLog.insertMany(logs);
117
+ await updateContextAuditCount(contextId, logs.length);
118
+ if (onAuditLogCreated) {
119
+ await onAuditLogCreated(logs, contextId);
120
+ }
121
+ }
122
+ });
123
+ }
124
+
125
+ async function updateContextAuditCount(contextId, count) {
126
+ if(!contextId) return;
127
+ const model = mongoose.models['Application'];
128
+ const context = await model.findById(contextId);
129
+ context.newAuditRecordsCount = Number(context.newAuditRecordsCount || 0) + Number(count ? count : 1);
130
+ await context.save();
131
+ }
132
+
133
+ export function registerAuditHook(callback) {
134
+ onAuditLogCreated = callback;
135
+ }
136
+
137
+ export default applyAuditMiddleware;
@@ -1,2 +1,2 @@
1
- export {default as applyAuditMiddleware} from './audit.middleware.js';
2
- export { registerAuditHook } from './audit.middleware.js';
1
+ export {default as applyAuditMiddleware} from './audit.middleware.js';
2
+ export { registerAuditHook } from './audit.middleware.js';
@@ -1,13 +1,13 @@
1
- import mongoose from 'mongoose';
2
-
3
- const auditConfigSchema = new mongoose.Schema({
4
- collectionName: String,
5
- fields: [String],
6
- trackCreation: { type: Boolean, default: false },
7
- descriptionResolutorForExternalData: { type: String, default: null },
8
- });
9
-
10
- const AuditConfigModel = mongoose.models.AuditConfig || mongoose.model('AuditConfig', auditConfigSchema);
11
-
12
-
13
- export default AuditConfigModel;
1
+ import mongoose from 'mongoose';
2
+
3
+ const auditConfigSchema = new mongoose.Schema({
4
+ collectionName: String,
5
+ fields: [String],
6
+ trackCreation: { type: Boolean, default: false },
7
+ descriptionResolutorForExternalData: { type: String, default: null },
8
+ });
9
+
10
+ const AuditConfigModel = mongoose.models.AuditConfig || mongoose.model('AuditConfig', auditConfigSchema);
11
+
12
+
13
+ export default AuditConfigModel;
@@ -1,16 +1,16 @@
1
- import mongoose from 'mongoose';
2
-
3
- const auditSchema = new mongoose.Schema({
4
- name: String,
5
- recordId: mongoose.Schema.Types.ObjectId,
6
- contextId: mongoose.Schema.Types.ObjectId,
7
- oldValue: mongoose.Schema.Types.Mixed,
8
- newValue: mongoose.Schema.Types.Mixed,
9
- timestamp: { type: Date, default: Date.now },
10
- createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
11
- externalData: mongoose.Schema.Types.Mixed
12
- });
13
-
14
- const AuditLog = mongoose.models.AuditLog || mongoose.model('AuditLog', auditSchema);
15
-
16
- export default AuditLog;
1
+ import mongoose from 'mongoose';
2
+
3
+ const auditSchema = new mongoose.Schema({
4
+ name: String,
5
+ recordId: mongoose.Schema.Types.ObjectId,
6
+ contextId: mongoose.Schema.Types.ObjectId,
7
+ oldValue: mongoose.Schema.Types.Mixed,
8
+ newValue: mongoose.Schema.Types.Mixed,
9
+ timestamp: { type: Date, default: Date.now },
10
+ createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
11
+ externalData: mongoose.Schema.Types.Mixed
12
+ });
13
+
14
+ const AuditLog = mongoose.models.AuditLog || mongoose.model('AuditLog', auditSchema);
15
+
16
+ export default AuditLog;
@@ -1,257 +1,265 @@
1
- import mongoose from 'mongoose';
2
-
3
- const optionSchema = new mongoose.Schema({
4
- label: { type: mongoose.Schema.Types.Mixed },
5
- value: { type: mongoose.Schema.Types.Mixed },
6
- text: { type: mongoose.Schema.Types.Mixed }
7
-
8
- }, { _id: false });
9
-
10
- const formfieldSchema = new mongoose.Schema({
11
- isFutureDateBlocked: {
12
- type: Boolean,
13
- },
14
- addToPayload: {
15
- type: Boolean,
16
- default: false
17
- },
18
- fieldName: {
19
- type: String,
20
- required: true,
21
- trim: true
22
- },
23
- isHidden: {
24
- type: Boolean,
25
- default: false
26
- },
27
- isEditable: {
28
- type: Boolean,
29
- default: true
30
- },
31
- size: {
32
- type: Number,
33
- default: 6,
34
- enum: [3, 4, 6, 8, 10, 12],
35
- },
36
- layout: {
37
- type: String,
38
- enum: ['horizontal', 'vertical'],
39
- },
40
- label: {
41
- type: String,
42
- required: true,
43
- trim: true
44
- },
45
- dataType: {
46
- type: String,
47
- required: true,
48
- enum: ['text-area', 'string', 'number', 'boolean-check', 'boolean-radio', 'percentage', 'date', 'array', 'object', 'select', 'radio', 'drop-down', 'sort-code', 'post-code', 'pound', 'lookup', 'multi-select', 'email', 'phone', 'internationalPhone'] // Added common form field types
49
- },
50
- isRequired: {
51
- type: Boolean,
52
- default: false
53
- },
54
- helpText: {
55
- type: String,
56
- default: null
57
- },
58
- isForceUpdate: {
59
- type: Boolean,
60
- default: false
61
- },
62
- requiredErrorMessage: {
63
- type: String,
64
- default: 'Field is required'
65
- },
66
- defaultValue: {
67
- type: mongoose.Schema.Types.Mixed
68
- },
69
- validationRules: [{
70
- type: mongoose.Schema.Types.Mixed,
71
- default: {}
72
- }],
73
- visibilityCondition: { // (LendingType=='BTL' || ApplicationType=='Company')
74
- type: mongoose.Schema.Types.Mixed,
75
- default: null
76
- },
77
- options: [optionSchema],
78
- section: {
79
- type: String,
80
- default: null
81
- },
82
- // Ex: sort-code, post-code, pound, drop-down, lookup
83
- uiHint: {
84
- type: String
85
- },
86
- list: {
87
- name: { // Lookup.BusinessType, DocumentType(collection)
88
- type: String
89
- },
90
- label: { // name
91
- type: String
92
- },
93
- value: { // _id
94
- type: String
95
- },
96
- listFilterQuery: { // MongoDB query ex: applicationId:applicationId
97
- type: mongoose.Schema.Types.Mixed,
98
- default: null
99
- },
100
- dynamicSource: {
101
- sourceMapping: [{
102
- sourceField: {
103
- type: String,
104
- default: null
105
- },
106
- sourceValue: { type: mongoose.Schema.Types.Mixed },
107
- listName: { type: String }, // The collection name to use when source field has this value
108
- fixedValue: { type: mongoose.Schema.Types.Mixed } // It's not req , and it can be a number like 0 , or a string etc.
109
- }]
110
- }
111
- },
112
- isSyncEnabled: { // Fixed typo (was isSyncEnabed)
113
- type: Boolean,
114
- default: true
115
- },
116
- dataElement: { //<collectionName>.<fieldName>
117
- type: String,
118
- default: null
119
- },
120
- syncTargetDataElement: { //<collectionName>.<fieldName> of Apprivo
121
- type: String,
122
- default: null
123
- },
124
- syncMapping: mongoose.Schema.Types.Mixed, // Ex: {true:'Checked', false: ''}
125
- order: {
126
- type: Number,
127
- default: 0
128
- },
129
- maxLength: {
130
- type: Number,
131
- default: null,
132
- min: 0
133
- },
134
- maxValue: { // this is not required used only when given else ignore ex- 100% max
135
- type: mongoose.Schema.Types.Mixed
136
- },
137
- minValue: { // this is not required used only when given else ignore ex- 0% min
138
- type: Number
139
- },
140
- isReadOnly: {
141
- type: Boolean,
142
- default: false
143
- },
144
- collectionName: { // name of collection for field if field doesnt belong to main collection
145
- type: String,
146
- default: null
147
- },
148
- foreignReferenceField: { // Ex: when field is from forieng collection then foreignReferenceField could be applicationId(which refers to the application)
149
- type: String,
150
- default: null
151
- },
152
- shouldfilterOptionsAfterAdd: {
153
- type : Boolean,
154
- default : false
155
- },
156
- shouldfilterOptionsAfterDelete: {
157
- type : Boolean,
158
- default : false
159
- }
160
- }, { timestamps: true, _id: false });
161
-
162
- const formConfigurationSchema = new mongoose.Schema({
163
- formName: {
164
- type: String,
165
- required: true,
166
- trim: true,
167
- unique: true // Consider making form names unique
168
- },
169
- collectionName: {
170
- type: String,
171
- required: true
172
- },
173
- sectionLayout: {
174
- type: String,
175
- trim: true
176
- },
177
- parentKey: String,
178
- populations: [Object],
179
- visibilityCondition: {
180
- type: mongoose.Schema.Types.Mixed,
181
- default: null
182
- },
183
- sections: [{
184
- sectionName: {
185
- type: String,
186
- trim: true
187
- },
188
- sectionLabel: {
189
- type: String,
190
- trim: true
191
- },
192
- isArray: {
193
- type: Boolean,
194
- default: false
195
- },
196
- isTable: {
197
- type: Boolean,
198
- default: false
199
- },
200
- dataFromSectionName: {
201
- type: Boolean,
202
- default: false
203
- },
204
- fields: [formfieldSchema],
205
- order: {
206
- type: Number,
207
- default: 0
208
- },
209
- visibilityCondition: { // Added section-level visibility
210
- type: mongoose.Schema.Types.Mixed,
211
- default: null
212
- },
213
- sections: [{
214
- sectionName: {
215
- type: String,
216
- trim: true
217
- },
218
- sectionLabel: {
219
- type: String,
220
- trim: true
221
- },
222
- fields: [formfieldSchema],
223
- order: {
224
- type: Number,
225
- default: 0
226
- },
227
- isTable: {
228
- type: Boolean,
229
- default: false
230
- },
231
- visibilityCondition: { // Added section-level visibility
232
- type: mongoose.Schema.Types.Mixed,
233
- default: null
234
- }
235
- }]
236
- }],
237
- isActive: { // Consider adding status flag
238
- type: Boolean,
239
- default: true
240
- },
241
- isDeleteApprivoSync: {
242
- type: Boolean,
243
- default: true
244
- },
245
- isCreateApprivoSync: {
246
- type: Boolean,
247
- default: true
248
- },
249
- version: { // Consider adding versioning
250
- type: Number,
251
- default: 1
252
- }
253
- }, { timestamps: true });
254
-
255
- const FormConfigurationModel = mongoose.models.FormConfiguration || mongoose.model('FormConfiguration', formConfigurationSchema);
256
-
257
- export default FormConfigurationModel;
1
+ import mongoose from 'mongoose';
2
+
3
+ const optionSchema = new mongoose.Schema({
4
+ label: { type: mongoose.Schema.Types.Mixed },
5
+ value: { type: mongoose.Schema.Types.Mixed },
6
+ text: { type: mongoose.Schema.Types.Mixed }
7
+
8
+ }, { _id: false });
9
+
10
+ const formfieldSchema = new mongoose.Schema({
11
+ isFutureDateBlocked: {
12
+ type: Boolean,
13
+ },
14
+ addToPayload: {
15
+ type: Boolean,
16
+ default: false
17
+ },
18
+ fieldName: {
19
+ type: String,
20
+ required: true,
21
+ trim: true
22
+ },
23
+ isHidden: {
24
+ type: Boolean,
25
+ default: false
26
+ },
27
+ isEditable: {
28
+ type: Boolean,
29
+ default: true
30
+ },
31
+ size: {
32
+ type: Number,
33
+ default: 6,
34
+ enum: [3, 4, 6, 8, 10, 12],
35
+ },
36
+ layout: {
37
+ type: String,
38
+ enum: ['horizontal', 'vertical'],
39
+ },
40
+ label: {
41
+ type: String,
42
+ required: true,
43
+ trim: true
44
+ },
45
+ dataType: {
46
+ type: String,
47
+ required: true,
48
+ enum: ['text-area', 'string', 'number', 'boolean-check', 'boolean-radio', 'percentage', 'date', 'array', 'object', 'select', 'radio', 'drop-down', 'sort-code', 'post-code', 'pound', 'lookup', 'multi-select', 'email', 'phone', 'internationalPhone'] // Added common form field types
49
+ },
50
+ isRequired: {
51
+ type: Boolean,
52
+ default: false
53
+ },
54
+ helpText: {
55
+ type: String,
56
+ default: null
57
+ },
58
+ isForceUpdate: {
59
+ type: Boolean,
60
+ default: false
61
+ },
62
+ requiredErrorMessage: {
63
+ type: String,
64
+ default: 'Field is required'
65
+ },
66
+ defaultValue: {
67
+ type: mongoose.Schema.Types.Mixed
68
+ },
69
+ validationRules: [{
70
+ type: mongoose.Schema.Types.Mixed,
71
+ default: {}
72
+ }],
73
+ visibilityCondition: { // (LendingType=='BTL' || ApplicationType=='Company')
74
+ type: mongoose.Schema.Types.Mixed,
75
+ default: null
76
+ },
77
+ options: [optionSchema],
78
+ section: {
79
+ type: String,
80
+ default: null
81
+ },
82
+ keyIconClass: {
83
+ type: String,
84
+ default: null
85
+ },
86
+ keyIconStyle: {
87
+ type: Object,
88
+ default: null
89
+ },
90
+ // Ex: sort-code, post-code, pound, drop-down, lookup
91
+ uiHint: {
92
+ type: String
93
+ },
94
+ list: {
95
+ name: { // Lookup.BusinessType, DocumentType(collection)
96
+ type: String
97
+ },
98
+ label: { // name
99
+ type: String
100
+ },
101
+ value: { // _id
102
+ type: String
103
+ },
104
+ listFilterQuery: { // MongoDB query ex: applicationId:applicationId
105
+ type: mongoose.Schema.Types.Mixed,
106
+ default: null
107
+ },
108
+ dynamicSource: {
109
+ sourceMapping: [{
110
+ sourceField: {
111
+ type: String,
112
+ default: null
113
+ },
114
+ sourceValue: { type: mongoose.Schema.Types.Mixed },
115
+ listName: { type: String }, // The collection name to use when source field has this value
116
+ fixedValue: { type: mongoose.Schema.Types.Mixed } // It's not req , and it can be a number like 0 , or a string etc.
117
+ }]
118
+ }
119
+ },
120
+ isSyncEnabled: { // Fixed typo (was isSyncEnabed)
121
+ type: Boolean,
122
+ default: true
123
+ },
124
+ dataElement: { //<collectionName>.<fieldName>
125
+ type: String,
126
+ default: null
127
+ },
128
+ syncTargetDataElement: { //<collectionName>.<fieldName> of Apprivo
129
+ type: String,
130
+ default: null
131
+ },
132
+ syncMapping: mongoose.Schema.Types.Mixed, // Ex: {true:'Checked', false: ''}
133
+ order: {
134
+ type: Number,
135
+ default: 0
136
+ },
137
+ maxLength: {
138
+ type: Number,
139
+ default: null,
140
+ min: 0
141
+ },
142
+ maxValue: { // this is not required used only when given else ignore ex- 100% max
143
+ type: mongoose.Schema.Types.Mixed
144
+ },
145
+ minValue: { // this is not required used only when given else ignore ex- 0% min
146
+ type: Number
147
+ },
148
+ isReadOnly: {
149
+ type: Boolean,
150
+ default: false
151
+ },
152
+ collectionName: { // name of collection for field if field doesnt belong to main collection
153
+ type: String,
154
+ default: null
155
+ },
156
+ foreignReferenceField: { // Ex: when field is from forieng collection then foreignReferenceField could be applicationId(which refers to the application)
157
+ type: String,
158
+ default: null
159
+ },
160
+ shouldfilterOptionsAfterAdd: {
161
+ type : Boolean,
162
+ default : false
163
+ },
164
+ shouldfilterOptionsAfterDelete: {
165
+ type : Boolean,
166
+ default : false
167
+ }
168
+ }, { timestamps: true, _id: false });
169
+
170
+ const formConfigurationSchema = new mongoose.Schema({
171
+ formName: {
172
+ type: String,
173
+ required: true,
174
+ trim: true,
175
+ unique: true // Consider making form names unique
176
+ },
177
+ collectionName: {
178
+ type: String,
179
+ required: true
180
+ },
181
+ sectionLayout: {
182
+ type: String,
183
+ trim: true
184
+ },
185
+ parentKey: String,
186
+ populations: [Object],
187
+ visibilityCondition: {
188
+ type: mongoose.Schema.Types.Mixed,
189
+ default: null
190
+ },
191
+ sections: [{
192
+ sectionName: {
193
+ type: String,
194
+ trim: true
195
+ },
196
+ sectionLabel: {
197
+ type: String,
198
+ trim: true
199
+ },
200
+ isArray: {
201
+ type: Boolean,
202
+ default: false
203
+ },
204
+ isTable: {
205
+ type: Boolean,
206
+ default: false
207
+ },
208
+ dataFromSectionName: {
209
+ type: Boolean,
210
+ default: false
211
+ },
212
+ fields: [formfieldSchema],
213
+ order: {
214
+ type: Number,
215
+ default: 0
216
+ },
217
+ visibilityCondition: { // Added section-level visibility
218
+ type: mongoose.Schema.Types.Mixed,
219
+ default: null
220
+ },
221
+ sections: [{
222
+ sectionName: {
223
+ type: String,
224
+ trim: true
225
+ },
226
+ sectionLabel: {
227
+ type: String,
228
+ trim: true
229
+ },
230
+ fields: [formfieldSchema],
231
+ order: {
232
+ type: Number,
233
+ default: 0
234
+ },
235
+ isTable: {
236
+ type: Boolean,
237
+ default: false
238
+ },
239
+ visibilityCondition: { // Added section-level visibility
240
+ type: mongoose.Schema.Types.Mixed,
241
+ default: null
242
+ }
243
+ }]
244
+ }],
245
+ isActive: { // Consider adding status flag
246
+ type: Boolean,
247
+ default: true
248
+ },
249
+ isDeleteApprivoSync: {
250
+ type: Boolean,
251
+ default: true
252
+ },
253
+ isCreateApprivoSync: {
254
+ type: Boolean,
255
+ default: true
256
+ },
257
+ version: { // Consider adding versioning
258
+ type: Number,
259
+ default: 1
260
+ }
261
+ }, { timestamps: true });
262
+
263
+ const FormConfigurationModel = mongoose.models.FormConfiguration || mongoose.model('FormConfiguration', formConfigurationSchema);
264
+
265
+ export default FormConfigurationModel;
package/models/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { default as AuditConfigModel } from './audit-config.model.js';
2
- export { default as AuditModel } from './audit.model.js';
3
- export { default as ValueReferenceMapModel } from './value-reference-map.model.js';
1
+ export { default as AuditConfigModel } from './audit-config.model.js';
2
+ export { default as AuditModel } from './audit.model.js';
3
+ export { default as ValueReferenceMapModel } from './value-reference-map.model.js';
4
4
  export { default as FormConfigurationModel } from './form-configuration.model.js';
@@ -1,12 +1,12 @@
1
- import mongoose from 'mongoose';
2
-
3
- const valueReferenceMapSchema = new mongoose.Schema({
4
- field: String, // e.g. 'documentTypeId', 'createdBy', 'status'
5
- model: String, // e.g. 'DocumentType', 'User', 'ApplicationStatus'
6
- displayField: String, // e.g. 'name', 'fullName', 'label'
7
- });
8
-
9
- valueReferenceMapSchema.index({ field: 1 }, { unique: true });
10
-
11
- const ValueReferenceMapModel = mongoose.models.ValueReferenceMap || mongoose.model('ValueReferenceMap', valueReferenceMapSchema);
12
- export default ValueReferenceMapModel;
1
+ import mongoose from 'mongoose';
2
+
3
+ const valueReferenceMapSchema = new mongoose.Schema({
4
+ field: String, // e.g. 'documentTypeId', 'createdBy', 'status'
5
+ model: String, // e.g. 'DocumentType', 'User', 'ApplicationStatus'
6
+ displayField: String, // e.g. 'name', 'fullName', 'label'
7
+ });
8
+
9
+ valueReferenceMapSchema.index({ field: 1 }, { unique: true });
10
+
11
+ const ValueReferenceMapModel = mongoose.models.ValueReferenceMap || mongoose.model('ValueReferenceMap', valueReferenceMapSchema);
12
+ export default ValueReferenceMapModel;
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
- {
2
- "name": "@dynamatix/cat-shared",
3
- "version": "0.0.75",
4
- "main": "index.js",
5
- "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
7
- },
8
- "author": "",
9
- "license": "ISC",
10
- "type": "module",
11
- "description": "",
12
- "exports": {
13
- "./models": "./models/index.js",
14
- "./middlewares": "./middlewares/index.js",
15
- "./services": "./services/index.js"
16
- },
17
- "dependencies": {
18
- "dotenv": "^16.4.7",
19
- "mongoose": "^8.13.1"
20
- }
21
- }
1
+ {
2
+ "name": "@dynamatix/cat-shared",
3
+ "version": "0.0.77",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "type": "module",
11
+ "description": "",
12
+ "exports": {
13
+ "./models": "./models/index.js",
14
+ "./middlewares": "./middlewares/index.js",
15
+ "./services": "./services/index.js"
16
+ },
17
+ "dependencies": {
18
+ "dotenv": "^16.4.7",
19
+ "mongoose": "^8.13.1"
20
+ }
21
+ }
@@ -1,66 +1,66 @@
1
- import mongoose, { model } from 'mongoose';
2
- import dotenv from 'dotenv';
3
- import ValueReferenceMapModel from '../models/value-reference-map.model.js';
4
-
5
- dotenv.config();
6
-
7
- const MONGO_URI = 'mongodb+srv://qa-user:vw4sIy3lZpyTPoX0@gatehouse-testing.e08r2.mongodb.net/gatehouse-qa?retryWrites=true&w=majority';
8
-
9
- // Grouped definition (DRY)
10
- const groupedMappings = [
11
- {
12
- model: 'DocumentType',
13
- displayField: 'name',
14
- fields: ['documentTypeId']
15
- },
16
- {
17
- model: 'User',
18
- displayField: 'fullName',
19
- fields: ['createdBy', 'updatedBy', 'ownerId','documentOwnerId']
20
- },
21
- {
22
- model: 'Applicant',
23
- displayField: 'firstName',
24
- fields: ['documentOwnerId', 'applicantId']
25
- }
26
- ];
27
-
28
- // Flatten the mappings
29
- const referenceMappings = groupedMappings.flatMap(group =>
30
- group.fields.map(field => ({
31
- field,
32
- model: group.model,
33
- displayField: group.displayField
34
- }))
35
- );
36
-
37
- async function seed() {
38
- try {
39
- console.log(MONGO_URI);
40
- await mongoose.connect(MONGO_URI);
41
- console.log('āœ… Connected to MongoDB');
42
-
43
- for (const mapping of referenceMappings) {
44
- const existing = await ValueReferenceMapModel.findOne({ field: mapping.field });
45
- if (existing) {
46
- await ValueReferenceMapModel.updateOne(
47
- { field: mapping.field },
48
- { $set: mapping }
49
- );
50
- console.log(`šŸ”„ Updated mapping for '${mapping.field}'`);
51
- } else {
52
- await ValueReferenceMapModel.create(mapping);
53
- console.log(`āž• Inserted mapping for '${mapping.field}'`);
54
- }
55
- }
56
-
57
- console.log('\n🌱 Seeding complete āœ…');
58
- } catch (err) {
59
- console.error('āŒ Seeding failed', err);
60
- } finally {
61
- await mongoose.disconnect();
62
- process.exit();
63
- }
64
- }
65
-
66
- seed();
1
+ import mongoose, { model } from 'mongoose';
2
+ import dotenv from 'dotenv';
3
+ import ValueReferenceMapModel from '../models/value-reference-map.model.js';
4
+
5
+ dotenv.config();
6
+
7
+ const MONGO_URI = 'mongodb+srv://qa-user:vw4sIy3lZpyTPoX0@gatehouse-testing.e08r2.mongodb.net/gatehouse-qa?retryWrites=true&w=majority';
8
+
9
+ // Grouped definition (DRY)
10
+ const groupedMappings = [
11
+ {
12
+ model: 'DocumentType',
13
+ displayField: 'name',
14
+ fields: ['documentTypeId']
15
+ },
16
+ {
17
+ model: 'User',
18
+ displayField: 'fullName',
19
+ fields: ['createdBy', 'updatedBy', 'ownerId','documentOwnerId']
20
+ },
21
+ {
22
+ model: 'Applicant',
23
+ displayField: 'firstName',
24
+ fields: ['documentOwnerId', 'applicantId']
25
+ }
26
+ ];
27
+
28
+ // Flatten the mappings
29
+ const referenceMappings = groupedMappings.flatMap(group =>
30
+ group.fields.map(field => ({
31
+ field,
32
+ model: group.model,
33
+ displayField: group.displayField
34
+ }))
35
+ );
36
+
37
+ async function seed() {
38
+ try {
39
+ console.log(MONGO_URI);
40
+ await mongoose.connect(MONGO_URI);
41
+ console.log('āœ… Connected to MongoDB');
42
+
43
+ for (const mapping of referenceMappings) {
44
+ const existing = await ValueReferenceMapModel.findOne({ field: mapping.field });
45
+ if (existing) {
46
+ await ValueReferenceMapModel.updateOne(
47
+ { field: mapping.field },
48
+ { $set: mapping }
49
+ );
50
+ console.log(`šŸ”„ Updated mapping for '${mapping.field}'`);
51
+ } else {
52
+ await ValueReferenceMapModel.create(mapping);
53
+ console.log(`āž• Inserted mapping for '${mapping.field}'`);
54
+ }
55
+ }
56
+
57
+ console.log('\n🌱 Seeding complete āœ…');
58
+ } catch (err) {
59
+ console.error('āŒ Seeding failed', err);
60
+ } finally {
61
+ await mongoose.disconnect();
62
+ process.exit();
63
+ }
64
+ }
65
+
66
+ seed();
@@ -1,2 +1,2 @@
1
-
2
-
1
+
2
+
package/services/index.js CHANGED
@@ -1 +1 @@
1
- export { getContext, setContext } from './request-context.service.js';
1
+ export { getContext, setContext } from './request-context.service.js';
@@ -1,8 +1,8 @@
1
- // utils/request-context.js
2
- import { AsyncLocalStorage } from 'node:async_hooks';
3
-
4
- const asyncLocalStorage = new AsyncLocalStorage();
5
-
6
- export const setContext = (data) => asyncLocalStorage.enterWith(data);
7
-
8
- export const getContext = () => asyncLocalStorage.getStore();
1
+ // utils/request-context.js
2
+ import { AsyncLocalStorage } from 'node:async_hooks';
3
+
4
+ const asyncLocalStorage = new AsyncLocalStorage();
5
+
6
+ export const setContext = (data) => asyncLocalStorage.enterWith(data);
7
+
8
+ export const getContext = () => asyncLocalStorage.getStore();