import {EntityDefinition, HandlerSetter} from 'interfaces'; import { concat } from 'lodash'; // handler generators import getParentIdHandlers from './getParentIdHandlers'; import getParentIdsHandlers from './getParentIdsHandlers'; import getCreatedAtHandlers from './getCreatedAtHandlers'; import getUpdatedAtHandlers from './getUpdatedAtHandlers'; import getCountHandlers from './getCountHandlers'; import getSumHandlers from './getSumHandlers'; import getDuplicateChildrenHandlers from './getDuplicateChildrenHandlers'; export default function extractEntityHandlers( selfEntityType: string, // parent entityDefinition:EntityDefinition ): {selfHandlerSetters:Array, childHandlerSetters:Array} { let entireSelfHandlerSetters: Array = []; let entireChildHandlerSetters: Array = []; function concatToEntireHandlerSetters({ selfHandlerSetters, childHandlerSetters }) { entireSelfHandlerSetters = concat(entireSelfHandlerSetters, selfHandlerSetters); entireChildHandlerSetters = concat(entireChildHandlerSetters, childHandlerSetters); } for (const propKey in entityDefinition) { if (entityDefinition[propKey]) { const propValue = entityDefinition[propKey]; if (propValue.type === 'PARENT_ID') { concatToEntireHandlerSetters(getParentIdHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'PARENT_IDS') { concatToEntireHandlerSetters(getParentIdsHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'CREATED_AT') { concatToEntireHandlerSetters(getCreatedAtHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'UPDATED_AT') { concatToEntireHandlerSetters(getUpdatedAtHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'COUNT') { concatToEntireHandlerSetters(getCountHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'SUM') { concatToEntireHandlerSetters(getSumHandlers(selfEntityType, propKey, propValue)); } else if (propValue.type === 'DUPLICATE_CHILDREN') { concatToEntireHandlerSetters(getDuplicateChildrenHandlers(selfEntityType, propKey, propValue)); } } } return { selfHandlerSetters: entireSelfHandlerSetters, childHandlerSetters: entireChildHandlerSetters }; }