-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-process-functions.ts
More file actions
48 lines (45 loc) · 1.72 KB
/
post-process-functions.ts
File metadata and controls
48 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { jsonCopy } from "./general";
export function postProcessRecord(record: any, attributes: any[]) {
const prepareRecord = jsonCopy(record);
if (!prepareRecord.hasOwnProperty('data')) {
if (prepareRecord.hasOwnProperty('fullRecordData')) {
prepareRecord.data = prepareRecord.fullRecordData;
}
else if (prepareRecord.hasOwnProperty('recordData')) {
prepareRecord.data = prepareRecord.recordData;
} else {
throw new Error("Cant find record data in record object");
}
}
attributes.forEach((attribute, index) => {
if (typeof prepareRecord.data[attribute.name] === 'boolean') {
prepareRecord.data[attribute.name] = prepareRecord.data[attribute.name].toString();
}
});
return prepareRecord;
}
export function postProcessAttributes(attributes: any[]) {
const prepareAttributes = jsonCopy(attributes);
if (attributes && attributes.length > 0) {
if (!attributes[0].hasOwnProperty('key')) {
prepareAttributes.forEach((attribute, index) => {
if (attribute.id !== null) {
attribute.key = attribute.id;
} else {
throw new Error("Cant find attribute id in attribute object");
}
});
}
}
return prepareAttributes;
}
export function postProcessUpdateAndSortRecords(prev: any[], newRecords: any[]) {
const merged = [...prev, ...newRecords];
const uniqueRecords = Array.from(
new Map(merged.map((item) => [item.data?.running_id, item])).values()
);
uniqueRecords.sort(
(a, b) => (a.data?.running_id || 0) - (b.data?.running_id || 0)
);
return uniqueRecords;
}