All files / src/apps/concepts types.ts

8.51% Statements 4/47
0% Branches 0/58
0% Functions 0/8
8.89% Lines 4/45

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230                                                                                                                                                                                            1x           1x                                                             1x                                                                       1x                                                                                                                            
import { Extras, MAP_TYPE_CONCEPT_SET, MAP_TYPE_Q_AND_A } from "../../utils";
 
export interface ConceptName {
  name: string;
  locale: string;
  external_id: string;
  locale_preferred: boolean;
  name_type: string | null;
}
 
export interface ConceptDescription {
  description: string;
  locale: string;
  external_id: string;
  locale_preferred: boolean;
}
 
export interface Mapping {
  map_type: string;
  external_id: string;
  from_concept_url: string;
  to_concept_url?: string | null; // internal mapping
  to_source_url?: string; // external mapping
  to_concept_code?: string; // external mapping
  to_concept_name_resolved?: string | null;
  to_concept_name?: string | null;
  url?: string;
  retired?: boolean;
  extras?: { sort_weight?: number } | Omit<Extras, "sort_weight">;
}
 
export interface APIMapping extends Mapping {
  to_concept_code: string;
  url: string;
  retired: boolean;
}
 
export interface InternalAPIMapping extends APIMapping {
  to_concept_url: string;
}
 
export interface NumericConceptExtras {
  hi_absolute?: number;
  hi_critical?: number;
  hi_normal?: number;
  low_normal?: number;
  low_critical?: number;
  low_absolute?: number;
  units?: string;
  precise?: boolean;
}
 
export type ConceptExtras =
  | NumericConceptExtras
  | Omit<Extras, keyof NumericConceptExtras>;
 
export interface BaseConcept {
  id: string;
  external_id: string;
  concept_class: string;
  datatype: string;
  names: ConceptName[];
  descriptions: ConceptDescription[];
  url?: string;
  version_url?: string;
  extras?: ConceptExtras | null;
  added?: boolean;
  uuid?: string;
}
 
export interface Concept extends BaseConcept {
  answers: Mapping[];
  sets: Mapping[];
  mappings: Mapping[];
  retired: boolean;
}
 
export interface APIConcept extends BaseConcept {
  display_name: string;
  url: string;
  version_url: string;
  mappings: APIMapping[];
  retired: boolean;
  source_url: string;
  source: string;
}
 
export interface ConceptsState {
  concept?: APIConcept;
  concepts: { items: APIConcept[]; responseMeta?: {} };
  activeConcepts: { items: APIConcept[]; responseMeta?: {} };
  mappings: APIMapping[];
}
 
const apiNamesToName = (names: ConceptName[]) =>
  names.map((name: ConceptName) => ({
    ...name,
    name_type: name.name_type === null ? "null" : name.name_type // api represents 'Synonym' name_type as null
  }));
 
const apiConceptToConcept = (
  apiConcept: APIConcept | undefined,
  mappings: APIMapping[] = [],
  convertNames = true
): Concept | undefined => {
  if (!apiConcept) return apiConcept;
 
  let { names, descriptions, display_name, ...theRest } = apiConcept;
  descriptions = descriptions || [];
 
  let sortedNames = names.slice().sort(sortConceptName);
  let sortedDescriptions = descriptions.slice().sort(sortConceptDescriptions);
 
  return {
    names: convertNames ? apiNamesToName(sortedNames) : sortedNames,
    descriptions: sortedDescriptions,
    ...theRest,
    answers: mappings.filter(
      mapping => mapping.map_type === MAP_TYPE_Q_AND_A.value
    ),
    sets: mappings.filter(
      mapping => mapping.map_type === MAP_TYPE_CONCEPT_SET.value
    ),
    mappings: mappings.filter(
      mapping =>
        mapping.map_type !== MAP_TYPE_Q_AND_A.value &&
        mapping.map_type !== MAP_TYPE_CONCEPT_SET.value
    )
  };
};
 
const sortConceptName = (n1: ConceptName, n2: ConceptName) => {
  if (typeof n1 === "undefined") {
    return typeof n2 === "undefined" ? 0 : 1;
  }
 
  if (typeof n2 === "undefined") {
    return -1;
  }
 
  if (n1.name === null) {
    return n2.name == null ? 0 : 1;
  }
 
  if (n2.name == null) {
    return -1;
  }
 
  if (n1.locale !== n2.locale) {
    return n1.locale < n2.locale ? -1 : 1;
  }
 
  if (n1.name_type !== n2.name_type) {
    if (n1.name_type === "FULLY_SPECIFIED") {
      return -1;
    } else if (n2.name_type === "FULLY_SPECIFIED") {
      return 1;
    }
  }
 
  if (n1.locale_preferred !== n2.locale_preferred) {
    return n1.locale_preferred ? -1 : 1;
  }
 
  return n1.name.localeCompare(n2.name);
};
 
const sortConceptDescriptions = (
  d1: ConceptDescription,
  d2: ConceptDescription
) => {
  if (typeof d1 === "undefined") {
    return typeof d2 === "undefined" ? 0 : 1;
  }
 
  if (typeof d2 === "undefined") {
    return -1;
  }
 
  if (d1.description === null) {
    return d2.description == null ? 0 : 1;
  }
 
  if (d2.description == null) {
    return -1;
  }
 
  if (d1.locale !== d2.locale) {
    return d1.locale < d2.locale ? -1 : 1;
  }
 
  if (d1.locale_preferred !== d2.locale_preferred) {
    return d1.locale_preferred ? -1 : 1;
  }
 
  return d1.description.localeCompare(d2.description);
};
 
export type SortableField =
  | "_score"
  | "lastUpdate"
  | "name"
  | "id"
  | "datatype"
  | "source"
  | "conceptClass";
 
export interface OptionalQueryParams {
  q?: string;
  page?: number;
  sortDirection?: "sortAsc" | "sortDesc";
  sortBy?: SortableField;
  limit?: number;
  classFilters?: string[];
  dataTypeFilters?: string[];
  generalFilters?: string[];
  sourceFilters?: string[];
  addToDictionary?: string;
}
 
export interface QueryParams {
  q: string;
  page: number;
  sortDirection: "sortAsc" | "sortDesc";
  sortBy: SortableField;
  limit: number;
}
 
export { apiConceptToConcept };