import { getRandomNumberLarge } from "../../../cshub-shared/src/utilities/Random" ; import { IStudy } from "../../../cshub-shared/src/entities/study" ; import { getRepository } from "typeorm" ; import { Topic } from "../db/entities/topic" ; import { ITopic } from "../../../cshub-shared/src/entities/topic" ; export const generateRandomTopicHash = ( ) : Promise < number > => { const hash = getRandomNumberLarge ( ) ; return getTopicTree ( ) . then ( ( topics ) => { if ( topics ) { const topic = findTopicInTree ( hash , topics ) ; if ( topic === null ) { return hash ; } else { return generateRandomTopicHash ( ) ; } } else { return generateRandomTopicHash ( ) ; } } ) ; } ; export const findTopicInTree = ( topicHash , topics ) : Topic | null => { for ( const topic of topics ) { if ( topic . hash === topicHash ) { return topic ; } else if ( topic . children ) { const topicInChildren = findTopicInTree ( topicHash , topic . children ) ; if ( topicInChildren ) { return topicInChildren ; } } } return null ; } ; export const findStudyIdsOfTopic = ( topic ) : IStudy [ ] => { const studyIds = [ ] ; const study = topic . study ; if ( study ) { studyIds . push ( study ) ; } if ( topic . parent ) { const parentStudyIds = findStudyIdsOfTopic ( topic . parent ) ; studyIds . push ( ... parentStudyIds ) ; } return studyIds ; } ; export const getChildHashes = ( inputTopic ) : number [ ] => { const currentTopicHashes = [ ] ; for ( const topic of inputTopic ) { currentTopicHashes . push ( ... getChildHashes ( topic . children ) ) ; currentTopicHashes . push ( topic . hash ) ; } return currentTopicHashes ; } ; export const getStudiesFromTopic = ( topicHash ) : Promise < IStudy [ ] > => { return getTopicTree ( ) . then ( ( value ) => { if ( value ) { const topic = findTopicInTree ( topicHash , value ) ; if ( topic ) { return findStudyIdsOfTopic ( topic ) ; } } return [ ] ; } ) ; } ; export const getTopicTree = ( study ? ) : Promise < Topic [ ] | null > => { const topicRepository = getRepository ( Topic ) ; return topicRepository . find ( { relations : [ "parent" , "study" ] , } ) . then ( ( topics ) => { const parseCurrentLayer = ( parent ) => { const topicsWithParent = topics . filter ( ( topic ) => { if ( parent === null ) { return topic . parent === null ; } else { return topic . parent && topic . parent . id === parent . id ; } } ) ; if ( parent ) { parent . children = topicsWithParent ; } for ( const topic of topicsWithParent ) { const children = topics . filter ( ( childTopic ) => { return childTopic . parent && childTopic . parent . id === topic . id ; } ) ; topic . children = children ; topic . parent = parent ; for ( const child of children ) { child . parent = topic ; parseCurrentLayer ( child ) ; } } } ; parseCurrentLayer ( null ) ; const getTreeForStudy = ( topicTree , study ) : Topic | null => { for ( const topic of topicTree ) { if ( topic . study && topic . study . id === study ) { return topic ; } else { const treeForStudy = getTreeForStudy ( topic . children , study ) ; if ( treeForStudy ) { return treeForStudy ; } } } return null ; } ; if ( study ) { const treeForStudy = getTreeForStudy ( topics , study ) ; if ( treeForStudy ) { return [ treeForStudy ] ; } } else { return topics . filter ( ( topic ) => topic . parent === null ) ; } return null ; } ) . catch ( ( err ) => { return null ; } ) ; } ;