2019-07-15 08:12:52 +02:00
|
|
|
import { orderBy } from 'lodash';
|
|
|
|
import {
|
|
|
|
reduce, find, includes, flatten, union,
|
|
|
|
filter, each, map,
|
|
|
|
} from 'lodash/fp';
|
|
|
|
import {
|
|
|
|
joinKey, splitKey, isNonEmptyString,
|
|
|
|
isNothing, $, isSomething,
|
|
|
|
} from '../common';
|
|
|
|
import {
|
|
|
|
getFlattenedHierarchy, getNode, getRecordNodeId,
|
2019-12-22 08:12:21 +01:00
|
|
|
getExactNodeForKey, recordNodeIdIsAllowed,
|
2019-07-15 08:12:52 +02:00
|
|
|
isRecord, isGlobalIndex,
|
|
|
|
} from '../templateApi/hierarchy';
|
|
|
|
import { indexTypes } from '../templateApi/indexes';
|
2019-12-22 08:12:21 +01:00
|
|
|
import { getIndexDir } from "../indexApi/getIndexDir";
|
|
|
|
import { getRecordInfo} from "../recordApi/recordInfo";
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
export const getRelevantAncestorIndexes = (hierarchy, record) => {
|
2019-07-15 08:12:52 +02:00
|
|
|
const key = record.key;
|
|
|
|
const keyParts = splitKey(key);
|
|
|
|
const nodeId = getRecordNodeId(key);
|
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
const flatHierarchy = orderBy(getFlattenedHierarchy(hierarchy),
|
2019-07-15 08:12:52 +02:00
|
|
|
[node => node.pathRegx().length],
|
|
|
|
['desc']);
|
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
const makeindexNodeAndDir_ForAncestorIndex = (indexNode, parentRecordDir) => makeIndexNodeAndDir(indexNode, joinKey(parentRecordDir, indexNode.name));
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
const traverseAncestorIndexesInPath = () => reduce((acc, part) => {
|
|
|
|
const currentIndexKey = joinKey(acc.lastIndexKey, part);
|
|
|
|
acc.lastIndexKey = currentIndexKey;
|
|
|
|
const testPathRegx = p => new RegExp(`${p.pathRegx()}$`).test(currentIndexKey);
|
|
|
|
const nodeMatch = find(testPathRegx)(flatHierarchy);
|
|
|
|
|
|
|
|
if (isNothing(nodeMatch)) { return acc; }
|
|
|
|
|
|
|
|
if (!isRecord(nodeMatch)
|
|
|
|
|| nodeMatch.indexes.length === 0) { return acc; }
|
|
|
|
|
|
|
|
const indexes = $(nodeMatch.indexes, [
|
|
|
|
filter(i => i.indexType === indexTypes.ancestor
|
|
|
|
&& (i.allowedRecordNodeIds.length === 0
|
|
|
|
|| includes(nodeId)(i.allowedRecordNodeIds))),
|
|
|
|
]);
|
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
const currentRecordDir = getRecordInfo(hierarchy, currentIndexKey).dir;
|
|
|
|
|
2019-07-15 08:12:52 +02:00
|
|
|
each(v => acc.nodesAndKeys.push(
|
2019-12-22 08:12:21 +01:00
|
|
|
makeindexNodeAndDir_ForAncestorIndex(v, currentRecordDir),
|
2019-07-15 08:12:52 +02:00
|
|
|
))(indexes);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, { lastIndexKey: '', nodesAndKeys: [] })(keyParts).nodesAndKeys;
|
|
|
|
|
|
|
|
const rootIndexes = $(flatHierarchy, [
|
|
|
|
filter(n => isGlobalIndex(n) && recordNodeIdIsAllowed(n)(nodeId)),
|
2019-12-22 08:12:21 +01:00
|
|
|
map(i => makeIndexNodeAndDir(
|
|
|
|
i,
|
|
|
|
getIndexDir(hierarchy, i.nodeKey()))),
|
2019-07-15 08:12:52 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
return union(traverseAncestorIndexesInPath())(rootIndexes);
|
|
|
|
};
|
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
export const getRelevantReverseReferenceIndexes = (hierarchy, record) => $(record.key, [
|
|
|
|
getExactNodeForKey(hierarchy),
|
2019-07-15 08:12:52 +02:00
|
|
|
n => n.fields,
|
|
|
|
filter(f => f.type === 'reference'
|
|
|
|
&& isSomething(record[f.name])
|
|
|
|
&& isNonEmptyString(record[f.name].key)),
|
|
|
|
map(f => $(f.typeOptions.reverseIndexNodeKeys, [
|
|
|
|
map(n => ({
|
2019-12-22 08:12:21 +01:00
|
|
|
recordNode: getNode(hierarchy, n),
|
2019-07-15 08:12:52 +02:00
|
|
|
field: f,
|
|
|
|
})),
|
|
|
|
])),
|
|
|
|
flatten,
|
2019-12-22 08:12:21 +01:00
|
|
|
map(n => makeIndexNodeAndDir(
|
2019-07-15 08:12:52 +02:00
|
|
|
n.recordNode,
|
2019-12-22 08:12:21 +01:00
|
|
|
joinKey(
|
|
|
|
getRecordInfo(hierarchy, record[n.field.name].key).dir,
|
|
|
|
n.recordNode.name),
|
2019-07-15 08:12:52 +02:00
|
|
|
)),
|
|
|
|
]);
|
|
|
|
|
2019-12-22 08:12:21 +01:00
|
|
|
const makeIndexNodeAndDir = (indexNode, indexDir) => ({ indexNode, indexDir });
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
export default getRelevantAncestorIndexes;
|