function updateTieFormat({
  updateInProgressMatchUps,
  tournamentRecord,
  drawDefinition,
  structure,
  tieFormat,
  eventId,
  matchUp,
  event,
}) {
  const stack = 'updateTieFormat';

  const collectionMap = tieFormat?.collectionDefinitions.reduce(
    (instanceMap, def) => {
      instanceMap[def.collectionId] =
        (instanceMap[def.collectionId] || 0) + def.matchUpCount;
      return instanceMap;
    },
    {}
  );

  if (event && eventId) {
    if (event.tieFormat) {
      updateCollectionDefinitions(event, tieFormat);
    } else {
      // ensure that all matchUps in the event contain tieMatchUps referenced by tieFormat
      const { matchUps } = allEventMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        event.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on event',
          stack,
        });
      }
    }
  } else if (matchUp) {
    if (matchUp.tieFormat) {
      updateCollectionDefinitions(matchUp, tieFormat);
    } else {
      // ensure that all tieMatchUps are referenced by tieFormat
      const matchUpMap = instanceCount(
        matchUp.tieMatchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        matchUp.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on matchUp',
          stack,
        });
      }
    }
    modifyMatchUpNotice({
      tournamentId: tournamentRecord?.tournamentId,
      eventId: event?.eventId,
      drawDefinition,
      matchUp,
    });
  } else if (structure) {
    if (structure.tieFormat) {
      updateCollectionDefinitions(structure, tieFormat);
    } else {
      // ensure that all matchUps in the structure contain tieMatchUps referenced by tieFormat
      const { matchUps } = getAllStructureMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        structure.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on structure',
          stack,
        });
      }
    }
    updateStructureMatchUps({
      updateInProgressMatchUps,
      eventId: event?.eventId,
      tournamentRecord,
      drawDefinition,
      structure,
      tieFormat,
    });
    modifyDrawNotice({
      structureIds: [structure.structureId],
      eventId: event?.eventId,
      drawDefinition,
    });
  } else if (drawDefinition) {
    if (drawDefinition.tieFormat) {
      updateCollectionDefinitions(drawDefinition, tieFormat);
    } else {
      // ensure that all matchUps in the draw contain tieMatchUps referenced by tieFormat
      const { matchUps } = getAllDrawMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        drawDefinition.tieFormat = copyTieFormat(tieFormat);
      }
    }
    const modifiedStructureIds = [];

    for (const structure of drawDefinition.structures || []) {
      updateStructureMatchUps({
        updateInProgressMatchUps,
        eventId: event?.eventId,
        tournamentRecord,
        drawDefinition,
        structure,
        tieFormat,
      });
      modifiedStructureIds.push(structure.structureId);
    }
    modifyDrawNotice({
      structureIds: modifiedStructureIds,
      eventId: event?.eventId,
      drawDefinition,
    });
  } else {
    return { error: MISSING_DRAW_DEFINITION };
  }

  return { ...SUCCESS };
}
// only allows update to collectionName and matchUpFormat
only allows update to collectionName and matchUpFormat
only allows update to collectionName and matchUpFormat
function updateTieFormat({
  updateInProgressMatchUps,
  tournamentRecord,
  drawDefinition,
  structure,
  tieFormat,
  eventId,
  matchUp,
  event,
}) {
  const stack = 'updateTieFormat';
  const collectionMap = tieFormat?.collectionDefinitions.reduce(
    (instanceMap, def) => {
      instanceMap[def.collectionId] =
        (instanceMap[def.collectionId] || 0) + def.matchUpCount;
      return instanceMap;
    },
    {}
  );
  if (event && eventId) {
    if (event.tieFormat) {
      updateCollectionDefinitions(event, tieFormat);
    } else {
      const { matchUps } = allEventMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        event.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on event',
          stack,
        });
      }
    }
  } else if (matchUp) {
    if (matchUp.tieFormat) {
      updateCollectionDefinitions(matchUp, tieFormat);
    } else {
      const matchUpMap = instanceCount(
        matchUp.tieMatchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        matchUp.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on matchUp',
          stack,
        });
      }
    }
    modifyMatchUpNotice({
      tournamentId: tournamentRecord?.tournamentId,
      eventId: event?.eventId,
      drawDefinition,
      matchUp,
    });
  } else if (structure) {
    if (structure.tieFormat) {
      updateCollectionDefinitions(structure, tieFormat);
    } else {
      const { matchUps } = getAllStructureMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        structure.tieFormat = copyTieFormat(tieFormat);
      } else {
        return decorateResult({
          context: { collectionMap, matchUpMap },
          result: { error: INVALID_TIE_FORMAT },
          info: 'on structure',
          stack,
        });
      }
    }
    updateStructureMatchUps({
      updateInProgressMatchUps,
      eventId: event?.eventId,
      tournamentRecord,
      drawDefinition,
      structure,
      tieFormat,
    });
    modifyDrawNotice({
      structureIds: [structure.structureId],
      eventId: event?.eventId,
      drawDefinition,
    });
  } else if (drawDefinition) {
    if (drawDefinition.tieFormat) {
      updateCollectionDefinitions(drawDefinition, tieFormat);
    } else {
      const { matchUps } = getAllDrawMatchUps({
        matchUpFilters: { matchUpTypes: [SINGLES, DOUBLES] },
        structure,
      });
      const matchUpMap = instanceCount(
        matchUps.map(({ collectionId }) => collectionId)
      );
      if (mapsCheck(collectionMap, matchUpMap)) {
        drawDefinition.tieFormat = copyTieFormat(tieFormat);
      }
    }
    const modifiedStructureIds = [];
    for (const structure of drawDefinition.structures || []) {
      updateStructureMatchUps({
        updateInProgressMatchUps,
        eventId: event?.eventId,
        tournamentRecord,
        drawDefinition,
        structure,
        tieFormat,
      });
      modifiedStructureIds.push(structure.structureId);
    }
    modifyDrawNotice({
      structureIds: modifiedStructureIds,
      eventId: event?.eventId,
      drawDefinition,
    });
  } else {
    return { error: MISSING_DRAW_DEFINITION };
  }
  return { ...SUCCESS };
}
function updateTieFormat ( { updateInProgressMatchUps , tournamentRecord , drawDefinition , structure , tieFormat , eventId , matchUp , event , } ) { const stack = 'updateTieFormat' ; const collectionMap = tieFormat ?. collectionDefinitions . reduce ( ( instanceMap , def ) => { instanceMap [ def . collectionId ] = ( instanceMap [ def . collectionId ] || 0 ) + def . matchUpCount ; return instanceMap ; } , { } ) ; if ( event && eventId ) { if ( event . tieFormat ) { updateCollectionDefinitions ( event , tieFormat ) ; } else { const { matchUps } = allEventMatchUps ( { matchUpFilters : { matchUpTypes : [ SINGLES , DOUBLES ] } , structure , } ) ; const matchUpMap = instanceCount ( matchUps . map ( ( { collectionId } ) => collectionId ) ) ; if ( mapsCheck ( collectionMap , matchUpMap ) ) { event . tieFormat = copyTieFormat ( tieFormat ) ; } else { return decorateResult ( { context : { collectionMap , matchUpMap } , result : { error : INVALID_TIE_FORMAT } , info : 'on event' , stack , } ) ; } } } else if ( matchUp ) { if ( matchUp . tieFormat ) { updateCollectionDefinitions ( matchUp , tieFormat ) ; } else { const matchUpMap = instanceCount ( matchUp . tieMatchUps . map ( ( { collectionId } ) => collectionId ) ) ; if ( mapsCheck ( collectionMap , matchUpMap ) ) { matchUp . tieFormat = copyTieFormat ( tieFormat ) ; } else { return decorateResult ( { context : { collectionMap , matchUpMap } , result : { error : INVALID_TIE_FORMAT } , info : 'on matchUp' , stack , } ) ; } } modifyMatchUpNotice ( { tournamentId : tournamentRecord ?. tournamentId , eventId : event ?. eventId , drawDefinition , matchUp , } ) ; } else if ( structure ) { if ( structure . tieFormat ) { updateCollectionDefinitions ( structure , tieFormat ) ; } else { const { matchUps } = getAllStructureMatchUps ( { matchUpFilters : { matchUpTypes : [ SINGLES , DOUBLES ] } , structure , } ) ; const matchUpMap = instanceCount ( matchUps . map ( ( { collectionId } ) => collectionId ) ) ; if ( mapsCheck ( collectionMap , matchUpMap ) ) { structure . tieFormat = copyTieFormat ( tieFormat ) ; } else { return decorateResult ( { context : { collectionMap , matchUpMap } , result : { error : INVALID_TIE_FORMAT } , info : 'on structure' , stack , } ) ; } } updateStructureMatchUps ( { updateInProgressMatchUps , eventId : event ?. eventId , tournamentRecord , drawDefinition , structure , tieFormat , } ) ; modifyDrawNotice ( { structureIds : [ structure . structureId ] , eventId : event ?. eventId , drawDefinition , } ) ; } else if ( drawDefinition ) { if ( drawDefinition . tieFormat ) { updateCollectionDefinitions ( drawDefinition , tieFormat ) ; } else { const { matchUps } = getAllDrawMatchUps ( { matchUpFilters : { matchUpTypes : [ SINGLES , DOUBLES ] } , structure , } ) ; const matchUpMap = instanceCount ( matchUps . map ( ( { collectionId } ) => collectionId ) ) ; if ( mapsCheck ( collectionMap , matchUpMap ) ) { drawDefinition . tieFormat = copyTieFormat ( tieFormat ) ; } } const modifiedStructureIds = [ ] ; for ( const structure of drawDefinition . structures || [ ] ) { updateStructureMatchUps ( { updateInProgressMatchUps , eventId : event ?. eventId , tournamentRecord , drawDefinition , structure , tieFormat , } ) ; modifiedStructureIds . push ( structure . structureId ) ; } modifyDrawNotice ( { structureIds : modifiedStructureIds , eventId : event ?. eventId , drawDefinition , } ) ; } else { return { error : MISSING_DRAW_DEFINITION } ; } return { ... SUCCESS } ; }
only allows update to collectionName and matchUpFormat
only allows update to collectionName and matchUpFormat
// ensure that all matchUps in the event contain tieMatchUps referenced by tieFormat // ensure that all tieMatchUps are referenced by tieFormat // ensure that all matchUps in the structure contain tieMatchUps referenced by tieFormat // ensure that all matchUps in the draw contain tieMatchUps referenced by tieFormat