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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | 1x 1x 1x 1x 1x | import { AppState, completeAction, createActionThunk, indexedAction, progressAction, resetAction, startAction } from "../../../redux"; import api from "../api"; import { APIConcept, Concept, Mapping } from "../types"; import { addConceptsToDictionaryAction as addConceptsToDictionary, removeReferencesFromDictionaryAction as removeReferencesFromDictionary } from "../../dictionaries"; import { RETRIEVE_CONCEPT_ACTION, RETRIEVE_CONCEPTS_ACTION, RETRIEVE_ACTIVE_CONCEPTS_ACTION, UPSERT_CONCEPT_ACTION, UPSERT_CONCEPT_AND_MAPPINGS, UPSERT_MAPPING_ACTION } from "./actionTypes"; import { ANSWERS_BATCH_INDEX, MAPPINGS_BATCH_INDEX, SETS_BATCH_INDEX } from "./constants"; export const retrieveConceptAction = createActionThunk( RETRIEVE_CONCEPT_ACTION, api.concept.retrieve ); export const upsertConceptAndMappingsAction = ( data: Concept, sourceUrl: string, linkedDictionary?: string ) => { return async (dispatch: Function, getState: Function) => { dispatch(startAction(UPSERT_CONCEPT_AND_MAPPINGS)); const { answers, sets, mappings, ...concept } = data; const updating: boolean = !!concept.url; let response: APIConcept | boolean; function finish() { dispatch(progressAction(UPSERT_CONCEPT_AND_MAPPINGS, "")); dispatch(completeAction(UPSERT_CONCEPT_AND_MAPPINGS)); } dispatch( progressAction( UPSERT_CONCEPT_AND_MAPPINGS, `${updating ? "Updating" : "Creating"} concept...` ) ); const [action, url] = concept.url ? [ createActionThunk(UPSERT_CONCEPT_ACTION, api.concept.update), concept.url ] : [ createActionThunk(UPSERT_CONCEPT_ACTION, api.concepts.create), sourceUrl ]; response = await dispatch(action<APIConcept>(url, concept)); if (response === false) { // I think that at this point, it is generally sane not to try dealing with the mappings if the concept can't be updated. todo could improve. dispatch( progressAction( UPSERT_CONCEPT_AND_MAPPINGS, `Couldn't ${updating ? "update" : "create"} concept` ) ); dispatch(completeAction(UPSERT_CONCEPT_AND_MAPPINGS)); return false; } const conceptResponse: APIConcept = response as APIConcept; await dispatch(resetAction(UPSERT_MAPPING_ACTION)); const upsertMappings = async ( rawMappings: Mapping[], batchIndex: number, message: string ) => { if (rawMappings.length) dispatch( progressAction( UPSERT_CONCEPT_AND_MAPPINGS, `${updating ? "Updating" : "Creating"} ${message}...` ) ); const mappings = rawMappings.map(mapping => { const { to_source_url, to_concept_code, to_concept_url, external_id, map_type, to_concept_name, retired, url, extras } = mapping; const common = { external_id, map_type, to_concept_name, retired, url, extras }; return to_concept_url ? { ...common, from_concept_url: conceptResponse.url, to_concept_url } : { ...common, from_concept_url: conceptResponse.url, to_source_url, to_concept_code }; }); const actions: [ Mapping, CallableFunction, string ][] = mappings.map((mapping, index) => [ mapping, createActionThunk( indexedAction(UPSERT_MAPPING_ACTION, Number(`${batchIndex}${index}`)), mapping.url ? api.mapping.update : api.mappings.create ), mapping.url ? mapping.url : sourceUrl ]); for (const [mapping, action, url] of actions) { // See point below about race condition. We want to stop immediately any of these fails const response = await dispatch(action(url, mapping)); if (response === false) return false; } return true; }; // I know you're thinking, oh, we could have done these in parallel // I see your in-parallel and raise you my race-condition // If a user duplicates a mapping say in answers and sets, we want to be able to sequentially point this out // todo some more robust error handling const mappingsToProcess: [Mapping[], number, string][] = [ [answers, ANSWERS_BATCH_INDEX, "answers"], [sets, SETS_BATCH_INDEX, "sets"], [mappings, MAPPINGS_BATCH_INDEX, "mappings"] ]; for (const [values, batchIndex, message] of mappingsToProcess) { const response = await upsertMappings(values, batchIndex, message); if (updating && response === false) { // short circuit this here because we don't want to lose the current concept version // and don't really care about the updates the user made if not everything went smoothly // I'd previously relied on the fact that collections don't allow you to add multiple versions // per concept, but that behaviour seems buggy from the api end and this was simply a further precaution // todo handle the same scenario when on the create concept page, involves copying the version_url into the form return finish(); } } if (linkedDictionary) { dispatch( progressAction( UPSERT_CONCEPT_AND_MAPPINGS, "Updating concept in dictionary..." ) ); const state: AppState = getState(); // we *only* want to import either concept answers or members of the concept set const toConceptUrls: string[] = [ ...state.concepts.mappings .filter( mapping => mapping.map_type === "CONCEPT-SET" || mapping.map_type === "Q-AND-A" ) .map(mapping => mapping.to_concept_url) ].filter(reference => reference) as string[]; try { // ideally, this block should be atomic if (updating) { let referencesToRemove = [ // we don't remove the toConceptUrls because we can't be sure no other mapping depends on them // that would break the OCL module importer // ...state.concepts.mappings.map(mapping => mapping.url), todo ensure the cascade is working and delete this if so concept.version_url ].filter(reference => reference) as string[]; await dispatch( removeReferencesFromDictionary(linkedDictionary, referencesToRemove) ); } await Promise.all([ dispatch( addConceptsToDictionary(linkedDictionary, [conceptResponse.url]) ), dispatch( addConceptsToDictionary(linkedDictionary, toConceptUrls, "none") ) ]); } catch (e) { // whatever happens, make sure we never loose access to the reference from the dictionary // even if it means keeping the old version in await dispatch( addConceptsToDictionary(linkedDictionary, [ conceptResponse.url || (concept.url as string) ]) ); } } dispatch(progressAction(UPSERT_CONCEPT_AND_MAPPINGS, "")); dispatch(completeAction(UPSERT_CONCEPT_AND_MAPPINGS)); }; }; export const retrieveConceptsAction = createActionThunk( RETRIEVE_CONCEPTS_ACTION, api.concepts.retrieve ); export const retrieveActiveConceptsAction = createActionThunk( RETRIEVE_ACTIVE_CONCEPTS_ACTION, api.concepts.retrieveActive ); export const resetConceptFormAction = () => { return (dispatch: Function) => { dispatch(resetAction(UPSERT_CONCEPT_ACTION)); dispatch(resetAction(UPSERT_CONCEPT_AND_MAPPINGS)); }; }; |