All files / src/apps/dictionaries logic.ts

9.38% Statements 3/32
0% Branches 0/6
0% Functions 0/11
10.34% Lines 3/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77        1x                     1x             1x                                                                                                            
import api from "./api";
import { APIMapping, InternalAPIMapping } from "../concepts";
import { union } from "lodash";
 
const fetchConceptMappings = async (
  sourceUrl: string,
  fromConceptIds: string[]
): Promise<APIMapping[]> => {
  try {
    return (await api.retrieveMappings(sourceUrl, fromConceptIds)).data;
  } catch (e) {
    return [];
  }
};
 
const filterUnneededMappings = (mappings: APIMapping[]) =>
  mappings
    .filter(m => m.to_concept_url !== undefined)
    .filter(
      m => m.map_type === "Q-AND-A" || m.map_type === "CONCEPT-SET"
    ) as InternalAPIMapping[];
 
const recursivelyFetchToConcepts = async (
  fromSource: string,
  fromConceptIds: string[],
  updateNotification: (message: string) => void,
  levelsToCheck: number = 20,
  fetchMappings: (
    sourceUrl: string,
    fromConceptIds: string[]
  ) => Promise<APIMapping[]> = fetchConceptMappings
): Promise<string[]> => {
  const getConceptUrls = (mappingsLists: InternalAPIMapping[][]): string[] => {
    const toConceptUrls = union(...mappingsLists).map(
      mapping => mapping.to_concept_url
    );
    return toConceptUrls.filter(toConceptUrl => !!toConceptUrl);
  };
 
  updateNotification("Finding dependent concepts...");
  const startingConceptMappings: APIMapping[] = await fetchMappings(
    fromSource,
    fromConceptIds
  );
 
  const mappingsLists = [filterUnneededMappings(startingConceptMappings)];
 
  updateNotification(
    `Found ${getConceptUrls(mappingsLists).length} dependent concepts to add...`
  );
 
  const loadedConcepts = new Set();
  for (let i = 0; i < levelsToCheck; i += 1) {
    const toConceptCodes = mappingsLists[i]
      .map(mapping => mapping.to_concept_code)
      .filter(code => !loadedConcepts.has(code));
 
    if (!toConceptCodes.length) {
      break;
    }
 
    toConceptCodes.forEach(code => loadedConcepts.add(code));
 
    const conceptMappings = await fetchMappings(fromSource, toConceptCodes);
    mappingsLists.push(filterUnneededMappings(conceptMappings));
    updateNotification(
      `Found ${
        getConceptUrls(mappingsLists).length
      } dependent concepts to add...`
    );
  }
 
  return getConceptUrls(mappingsLists);
};
 
export { recursivelyFetchToConcepts };