Skip to content

Commit 9de0446

Browse files
authored
Merge pull request #109 from konecty/fix/inheritedFields-and-history
Fix: Inherited fields and history creation
2 parents f24a9f3 + 577c273 commit 9de0446

File tree

4 files changed

+25
-105
lines changed

4 files changed

+25
-105
lines changed

src/imports/konsistent/createHistory.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ export default async function createHistory(metaName, action, id, data, updatedB
3333

3434
const historyQuery = { _id: changeId };
3535

36+
const userDetailFields = ["_id"].concat(get(meta, "fields._user.detailFields", ["name", "active"]));
37+
3638
// Define base data to history
3739
const historyItem = {
3840
dataId: id,
3941
createdAt: updatedAt,
40-
createdBy: updatedBy,
42+
createdBy: get(updatedBy, userDetailFields),
4143
data: historyData,
4244
type: action,
4345
};

src/imports/konsistent/processIncomingChange.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import createHistory from './createHistory';
33
import processReverseLookups from './processReverseLookups';
44
import * as References from './updateReferences';
55

6+
import { DataDocument } from '@imports/types/data';
67
import omit from 'lodash/omit';
78
import { v4 as uuidV4 } from 'uuid';
89

@@ -13,25 +14,25 @@ const logTimeSpent = (startTime: [number, number], message: string) => {
1314
logger.debug(`${totalTime[0]}s ${totalTime[1] / 1000000}ms => ${message}`);
1415
};
1516

16-
export default async function processIncomingChange(metaName: string, incomingChange: object, action: Action, user: object) {
17+
export default async function processIncomingChange(metaName: string, incomingChange: DataDocument, action: Action, user: object) {
1718
try {
1819
const keysToIgnore = ['_updatedAt', '_createdAt', '_deletedAt', '_updatedBy', '_createdBy', '_deletedBy'];
19-
const dataId = uuidV4();
20+
const changeId = uuidV4();
2021

2122
let startTime = process.hrtime();
2223

2324
if (action === 'update') {
24-
await References.updateLookups(metaName, dataId, incomingChange);
25+
await References.updateLookups(metaName, incomingChange._id, incomingChange);
2526
logTimeSpent(startTime, `Updated lookup references for ${metaName}`);
2627
}
2728

28-
await processReverseLookups(metaName, dataId, incomingChange, action);
29+
await processReverseLookups(metaName, incomingChange._id, incomingChange, action);
2930
logTimeSpent(startTime, `Process'd reverse lookups for ${metaName}`);
3031

31-
await References.updateRelations(metaName, action, dataId, incomingChange);
32+
await References.updateRelations(metaName, action, incomingChange._id, incomingChange);
3233
logTimeSpent(startTime, `Updated relation references for ${metaName}`);
3334

34-
await createHistory(metaName, action, dataId, omit(incomingChange, keysToIgnore), user, new Date(), '');
35+
await createHistory(metaName, action, incomingChange._id, omit(incomingChange, keysToIgnore), user, new Date(), changeId);
3536
logTimeSpent(startTime, `Created history for ${metaName}`);
3637
} catch (err) {
3738
const error = err as Error;

src/imports/meta/copyDescriptionAndInheritedFields.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function copyDescriptionAndInheritedFields({ field, record, meta, a
3838
idsToUpdate,
3939
});
4040
if (validateResult.success === true) {
41-
Object.assign(value, { [inheritedField.fieldName]: validateResult.data });
41+
Object.assign(objectNewValues, { [inheritedField.fieldName]: validateResult.data });
4242
}
4343
return validateResult;
4444
} else {
@@ -55,7 +55,7 @@ export async function copyDescriptionAndInheritedFields({ field, record, meta, a
5555
idsToUpdate,
5656
});
5757
if (validateResult.success === true) {
58-
Object.assign(value, { [inheritedField.fieldName]: validateResult.data });
58+
Object.assign(objectNewValues, { [inheritedField.fieldName]: validateResult.data });
5959
}
6060
return validateResult;
6161
}

src/imports/meta/validateAndProcessValueFor.js

Lines changed: 13 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { copyDescriptionAndInheritedFields } from '../meta/copyDescriptionAndInh
2929
import { removeInheritedFields } from '../meta/removeInheritedFields';
3030
import { getNextCode } from './getNextCode';
3131

32+
import { errorReturn } from '@imports/utils/return';
3233
import { BCRYPT_SALT_ROUNDS } from '../consts';
3334

3435
const regexUtils = {
@@ -74,80 +75,38 @@ const ALLOWED_CURRENCIES = ['BRL'];
7475

7576
export async function validateAndProcessValueFor({ meta, fieldName, value, actionType, objectOriginalValues, objectNewValues, idsToUpdate }) {
7677
if (meta == null) {
77-
return {
78-
success: false,
79-
errors: [
80-
{
81-
message: `MetaObject.Meta does not exists`,
82-
},
83-
],
84-
};
78+
return errorReturn(`MetaObject.Meta does not exists`);
8579
}
8680

8781
const field = meta.fields[fieldName];
8882

8983
if (!field) {
90-
return {
91-
success: false,
92-
errors: [
93-
{
94-
message: `Field ${fieldName} does not exists on ${meta._id}`,
95-
},
96-
],
97-
};
84+
return errorReturn(`Field ${fieldName} does not exists on ${meta._id}`);
9885
}
9986

10087
// Validate required fields
10188

10289
if (field.isRequired === true && (value == null || (typeof value === 'string' && size(value) === 0))) {
103-
return {
104-
success: false,
105-
errors: [
106-
{
107-
message: `Field ${fieldName} is required`,
108-
},
109-
],
110-
};
90+
return errorReturn(`Field ${fieldName} is required`);
11191
}
11292

11393
// Validate List fields
11494
if (field.isList === true) {
11595
if (field.maxElements && field.maxElements > 0) {
11696
if (!isArray(value) || value.length > field.maxElements) {
117-
return {
118-
success: false,
119-
errors: [
120-
{
121-
message: `Value for field ${fieldName} must be array with the maximum of ${field.maxElements} item(s)`,
122-
},
123-
],
124-
};
97+
return errorReturn(`Value for field ${fieldName} must be array with the maximum of ${field.maxElements} item(s)`);
12598
}
12699
}
127100

128101
if (field.minElements && field.minElements > 0) {
129102
if (!isArray(value) || value.length < field.minElements) {
130-
return {
131-
success: false,
132-
errors: [
133-
{
134-
message: `Value for field ${fieldName} must be array with the minimum of ${field.minElements} item(s)`,
135-
},
136-
],
137-
};
103+
return errorReturn(`Value for field ${fieldName} must be array with the minimum of ${field.minElements} item(s)`);
138104
}
139105
}
140106

141107
if (field.isAllowDuplicates === false && isArray(value)) {
142108
if (value.some((itemA, indexA) => value.some((itemB, indexB) => indexA !== indexB && isEqual(itemA, itemB)))) {
143-
return {
144-
success: false,
145-
errors: [
146-
{
147-
message: `Value for field ${fieldName} must be array no duplicated values`,
148-
},
149-
],
150-
};
109+
return errorReturn(`Value for field ${fieldName} must be a list with no duplicated values`);
151110
}
152111
}
153112
}
@@ -157,14 +116,7 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
157116
if (isNumber(field.minSelected)) {
158117
if (field.minSelected === 1) {
159118
if (!value || (isArray(value) && value.length === 0)) {
160-
return {
161-
success: false,
162-
errors: [
163-
{
164-
message: `Value for field ${fieldName} must be an array with at least 1 item`,
165-
},
166-
],
167-
};
119+
return errorReturn(`Value for field ${fieldName} must be an array with at least 1 item`);
168120
}
169121
}
170122
}
@@ -197,26 +149,12 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
197149
const collection = MetaObject.Collections[meta.name];
198150

199151
if (collection == null) {
200-
return {
201-
success: false,
202-
errors: [
203-
{
204-
message: `Collection for ${meta.name} does not exists`,
205-
},
206-
],
207-
};
152+
return errorReturn(`Collection for ${meta.name} does not exists`);
208153
}
209154

210155
const count = await collection.countDocuments(query);
211156
if (count > 0) {
212-
return {
213-
success: false,
214-
errors: [
215-
{
216-
message: `Value for field ${fieldName} must be unique`,
217-
},
218-
],
219-
};
157+
return errorReturn(`Value for field ${fieldName} must be unique`);
220158
}
221159
}
222160

@@ -521,14 +459,7 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
521459

522460
if (isNumber(field.size) && field.size > 0) {
523461
if (value.length > field.size) {
524-
return {
525-
success: false,
526-
errors: [
527-
{
528-
message: `Value for field ${fieldName} must be less than ${field.size} characters`,
529-
},
530-
],
531-
};
462+
return errorReturn(`Value for field ${fieldName} must be less than ${field.size} characters`);
532463
}
533464
}
534465

@@ -1051,27 +982,13 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
1051982

1052983
const lookupCollection = MetaObject.Collections[field.document];
1053984
if (lookupCollection == null) {
1054-
return {
1055-
success: false,
1056-
errors: [
1057-
{
1058-
message: `Collection ${field.document} not found`,
1059-
},
1060-
],
1061-
};
985+
return errorReturn(`Collection ${field.document} not found`);
1062986
}
1063987

1064988
const record = await lookupCollection.findOne({ _id: value._id });
1065989

1066990
if (record == null) {
1067-
return {
1068-
success: false,
1069-
errors: [
1070-
{
1071-
message: `Record not found for field ${fieldName} with _id [${value._id}] on document [${field.document}]`,
1072-
},
1073-
],
1074-
};
991+
return errorReturn(`Record not found for field ${fieldName} with _id [${value._id}] on document [${field.document}]`);
1075992
}
1076993

1077994
const inheritedFieldsResult = await copyDescriptionAndInheritedFields({

0 commit comments

Comments
 (0)