function addEventEntries(params) {
  const {
    entryStatus = DIRECT_ACCEPTANCE,
    autoEntryPositions = true,
    participantIds = [],
    entryStageSequence,
    entryStage = MAIN,
    tournamentRecord,
    ignoreStageSpace,
    drawDefinition,
    roundTarget,
    extensions,
    extension,
    drawId,
    event,
  } = params;

  if (!event) return { error: MISSING_EVENT };
  if (!participantIds || !participantIds.length) {
    return { error: MISSING_PARTICIPANT_IDS };
  }

  if (!event || !event.eventId) return { error: EVENT_NOT_FOUND };

  const stack = 'addDrawEntry';

  if (
    (extensions &&
      (!Array.isArray(extensions) || !extensions.every(isValidExtension))) ||
    (extension && !isValidExtension(extension))
  ) {
    return decorateResult({
      result: { error: INVALID_VALUES },
      context: definedAttributes({ extension, extensions }),
      info: 'Invalid extension(s)',
      stack,
    });
  }

  const typedParticipantIds =
    tournamentRecord?.participants
      ?.filter((participant) => {
        if (
          event.eventType === SINGLES &&
          participant.participantType === INDIVIDUAL
        ) {
          return true;
        }
        if (
          event.eventType === DOUBLES &&
          participant.participantType === PAIR
        ) {
          return true;
        }
        if (
          event.eventType === DOUBLES &&
          participant.participantType === INDIVIDUAL &&
          isUngrouped(entryStatus)
        ) {
          return true;
        }
        if (
          event.eventType === TEAM &&
          (participant.participantType === TEAM ||
            (isUngrouped(entryStatus) &&
              participant.participantType === INDIVIDUAL))
        ) {
          return true;
        }
        return false;
      })
      .map((participant) => participant.participantId) || [];

  const validParticipantIds = participantIds.filter(
    (participantId) =>
      !typedParticipantIds.length || typedParticipantIds.includes(participantId)
  );

  if (!event.entries) event.entries = [];
  const existingIds = event.entries.map(
    (e) => e.participantId || (e.participant && e.participant.participantId)
  );

  validParticipantIds.forEach((participantId) => {
    if (!existingIds.includes(participantId)) {
      const entry = definedAttributes({
        participantId,
        entryStatus,
        entryStage,
        extensions,
      });

      if (extension) {
        addExtension({ element: entry, extension });
      }

      if (roundTarget) {
        addExtension({
          extension: { name: ROUND_TARGET, value: roundTarget },
          element: entry,
        });
      }
      if (entryStageSequence) entry.entryStageSequence = entryStageSequence;
      event.entries.push(entry);
    }
  });

  let info;
  if (drawId && !isUngrouped(entryStage)) {
    const result = addDrawEntries({
      participantIds: validParticipantIds,
      autoEntryPositions,
      entryStageSequence,
      ignoreStageSpace,
      drawDefinition,
      entryStatus,
      roundTarget,
      entryStage,
      extension,
      drawId,
      event,
    });

    // Ignore error if drawId is included but entry can't be added to drawDefinition/flightProfile
    // return error as info to client
    if (result.error) {
      info = result.error;
    }
  }

  // now remove any unpaired participantIds which exist as part of added paired participants
  if (event.eventType === DOUBLES) {
    const enteredParticipantIds = (event.entries || []).map(
      (entry) => entry.participantId
    );
    const unpairedIndividualParticipantIds = (event.entries || [])
      .filter((entry) => isUngrouped(entry.entryStatus))
      .map((entry) => entry.participantId);
    const tournamentParticipants = tournamentRecord?.participants || [];
    const pairedIndividualParticipantIds = tournamentParticipants
      .filter(
        (participant) =>
          enteredParticipantIds.includes(participant.participantId) &&
          participant.participantType === PAIR
      )
      .map((participant) => participant.individualParticipantIds)
      .flat(Infinity);
    const unpairedParticipantIdsToRemove =
      unpairedIndividualParticipantIds.filter((participantId) =>
        pairedIndividualParticipantIds.includes(participantId)
      );
    if (unpairedParticipantIdsToRemove.length) {
      removeEventEntries({
        tournamentRecord,
        participantIds: unpairedParticipantIdsToRemove,
        autoEntryPositions: false, // because the method will be called below if necessary
        event,
      });
    }
  }

  const invalidParticipantIds = !!(
    validParticipantIds.length !== participantIds.length
  );

  if (autoEntryPositions) {
    event.entries = refreshEntryPositions({
      entries: event.entries || [],
    });
  }

  return !invalidParticipantIds
    ? Object.assign({ info }, SUCCESS)
    : { error: INVALID_PARTICIPANT_IDS };
}
/**
 *
 * Add entries into an event; optionally add to specified drawDefinition/flightProfile, if possible.
 *
 * @param {object} tournamentRecord - passed in automatically by tournamentEngine
 * @param {string} eventId - tournamentEngine automatically retrieves event
 * @param {string} drawId - optional - also add to drawDefinition.entries & flightProfile.drawEntries (if possible)
 * @param {string[]} participantIds - ids of all participants to add to event
 * @param {string} enryStatus - entryStatus enum
 * @param {string} entryStage - entryStage enum
 *
 */
Add entries into an event; optionally add to specified drawDefinition/flightProfile, if possible.
Add entries into an event ; optionally add to specified drawDefinition / flightProfile if possible .
function addEventEntries(params) {
  const {
    entryStatus = DIRECT_ACCEPTANCE,
    autoEntryPositions = true,
    participantIds = [],
    entryStageSequence,
    entryStage = MAIN,
    tournamentRecord,
    ignoreStageSpace,
    drawDefinition,
    roundTarget,
    extensions,
    extension,
    drawId,
    event,
  } = params;
  if (!event) return { error: MISSING_EVENT };
  if (!participantIds || !participantIds.length) {
    return { error: MISSING_PARTICIPANT_IDS };
  }
  if (!event || !event.eventId) return { error: EVENT_NOT_FOUND };
  const stack = 'addDrawEntry';
  if (
    (extensions &&
      (!Array.isArray(extensions) || !extensions.every(isValidExtension))) ||
    (extension && !isValidExtension(extension))
  ) {
    return decorateResult({
      result: { error: INVALID_VALUES },
      context: definedAttributes({ extension, extensions }),
      info: 'Invalid extension(s)',
      stack,
    });
  }
  const typedParticipantIds =
    tournamentRecord?.participants
      ?.filter((participant) => {
        if (
          event.eventType === SINGLES &&
          participant.participantType === INDIVIDUAL
        ) {
          return true;
        }
        if (
          event.eventType === DOUBLES &&
          participant.participantType === PAIR
        ) {
          return true;
        }
        if (
          event.eventType === DOUBLES &&
          participant.participantType === INDIVIDUAL &&
          isUngrouped(entryStatus)
        ) {
          return true;
        }
        if (
          event.eventType === TEAM &&
          (participant.participantType === TEAM ||
            (isUngrouped(entryStatus) &&
              participant.participantType === INDIVIDUAL))
        ) {
          return true;
        }
        return false;
      })
      .map((participant) => participant.participantId) || [];
  const validParticipantIds = participantIds.filter(
    (participantId) =>
      !typedParticipantIds.length || typedParticipantIds.includes(participantId)
  );
  if (!event.entries) event.entries = [];
  const existingIds = event.entries.map(
    (e) => e.participantId || (e.participant && e.participant.participantId)
  );
  validParticipantIds.forEach((participantId) => {
    if (!existingIds.includes(participantId)) {
      const entry = definedAttributes({
        participantId,
        entryStatus,
        entryStage,
        extensions,
      });
      if (extension) {
        addExtension({ element: entry, extension });
      }
      if (roundTarget) {
        addExtension({
          extension: { name: ROUND_TARGET, value: roundTarget },
          element: entry,
        });
      }
      if (entryStageSequence) entry.entryStageSequence = entryStageSequence;
      event.entries.push(entry);
    }
  });
  let info;
  if (drawId && !isUngrouped(entryStage)) {
    const result = addDrawEntries({
      participantIds: validParticipantIds,
      autoEntryPositions,
      entryStageSequence,
      ignoreStageSpace,
      drawDefinition,
      entryStatus,
      roundTarget,
      entryStage,
      extension,
      drawId,
      event,
    });
    if (result.error) {
      info = result.error;
    }
  }
  if (event.eventType === DOUBLES) {
    const enteredParticipantIds = (event.entries || []).map(
      (entry) => entry.participantId
    );
    const unpairedIndividualParticipantIds = (event.entries || [])
      .filter((entry) => isUngrouped(entry.entryStatus))
      .map((entry) => entry.participantId);
    const tournamentParticipants = tournamentRecord?.participants || [];
    const pairedIndividualParticipantIds = tournamentParticipants
      .filter(
        (participant) =>
          enteredParticipantIds.includes(participant.participantId) &&
          participant.participantType === PAIR
      )
      .map((participant) => participant.individualParticipantIds)
      .flat(Infinity);
    const unpairedParticipantIdsToRemove =
      unpairedIndividualParticipantIds.filter((participantId) =>
        pairedIndividualParticipantIds.includes(participantId)
      );
    if (unpairedParticipantIdsToRemove.length) {
      removeEventEntries({
        tournamentRecord,
        participantIds: unpairedParticipantIdsToRemove,
        autoEntryPositions: false, 
        event,
      });
    }
  }
  const invalidParticipantIds = !!(
    validParticipantIds.length !== participantIds.length
  );
  if (autoEntryPositions) {
    event.entries = refreshEntryPositions({
      entries: event.entries || [],
    });
  }
  return !invalidParticipantIds
    ? Object.assign({ info }, SUCCESS)
    : { error: INVALID_PARTICIPANT_IDS };
}
function addEventEntries ( params ) { const { entryStatus = DIRECT_ACCEPTANCE , autoEntryPositions = true , participantIds = [ ] , entryStageSequence , entryStage = MAIN , tournamentRecord , ignoreStageSpace , drawDefinition , roundTarget , extensions , extension , drawId , event , } = params ; if ( ! event ) return { error : MISSING_EVENT } ; if ( ! participantIds || ! participantIds . length ) { return { error : MISSING_PARTICIPANT_IDS } ; } if ( ! event || ! event . eventId ) return { error : EVENT_NOT_FOUND } ; const stack = 'addDrawEntry' ; if ( ( extensions && ( ! Array . isArray ( extensions ) || ! extensions . every ( isValidExtension ) ) ) || ( extension && ! isValidExtension ( extension ) ) ) { return decorateResult ( { result : { error : INVALID_VALUES } , context : definedAttributes ( { extension , extensions } ) , info : 'Invalid extension(s)' , stack , } ) ; } const typedParticipantIds = tournamentRecord ?. participants ?. filter ( ( participant ) => { if ( event . eventType === SINGLES && participant . participantType === INDIVIDUAL ) { return true ; } if ( event . eventType === DOUBLES && participant . participantType === PAIR ) { return true ; } if ( event . eventType === DOUBLES && participant . participantType === INDIVIDUAL && isUngrouped ( entryStatus ) ) { return true ; } if ( event . eventType === TEAM && ( participant . participantType === TEAM || ( isUngrouped ( entryStatus ) && participant . participantType === INDIVIDUAL ) ) ) { return true ; } return false ; } ) . map ( ( participant ) => participant . participantId ) || [ ] ; const validParticipantIds = participantIds . filter ( ( participantId ) => ! typedParticipantIds . length || typedParticipantIds . includes ( participantId ) ) ; if ( ! event . entries ) event . entries = [ ] ; const existingIds = event . entries . map ( ( e ) => e . participantId || ( e . participant && e . participant . participantId ) ) ; validParticipantIds . forEach ( ( participantId ) => { if ( ! existingIds . includes ( participantId ) ) { const entry = definedAttributes ( { participantId , entryStatus , entryStage , extensions , } ) ; if ( extension ) { addExtension ( { element : entry , extension } ) ; } if ( roundTarget ) { addExtension ( { extension : { name : ROUND_TARGET , value : roundTarget } , element : entry , } ) ; } if ( entryStageSequence ) entry . entryStageSequence = entryStageSequence ; event . entries . push ( entry ) ; } } ) ; let info ; if ( drawId && ! isUngrouped ( entryStage ) ) { const result = addDrawEntries ( { participantIds : validParticipantIds , autoEntryPositions , entryStageSequence , ignoreStageSpace , drawDefinition , entryStatus , roundTarget , entryStage , extension , drawId , event , } ) ; if ( result . error ) { info = result . error ; } } if ( event . eventType === DOUBLES ) { const enteredParticipantIds = ( event . entries || [ ] ) . map ( ( entry ) => entry . participantId ) ; const unpairedIndividualParticipantIds = ( event . entries || [ ] ) . filter ( ( entry ) => isUngrouped ( entry . entryStatus ) ) . map ( ( entry ) => entry . participantId ) ; const tournamentParticipants = tournamentRecord ?. participants || [ ] ; const pairedIndividualParticipantIds = tournamentParticipants . filter ( ( participant ) => enteredParticipantIds . includes ( participant . participantId ) && participant . participantType === PAIR ) . map ( ( participant ) => participant . individualParticipantIds ) . flat ( Infinity ) ; const unpairedParticipantIdsToRemove = unpairedIndividualParticipantIds . filter ( ( participantId ) => pairedIndividualParticipantIds . includes ( participantId ) ) ; if ( unpairedParticipantIdsToRemove . length ) { removeEventEntries ( { tournamentRecord , participantIds : unpairedParticipantIdsToRemove , autoEntryPositions : false , event , } ) ; } } const invalidParticipantIds = ! ! ( validParticipantIds . length !== participantIds . length ) ; if ( autoEntryPositions ) { event . entries = refreshEntryPositions ( { entries : event . entries || [ ] , } ) ; } return ! invalidParticipantIds ? Object . assign ( { info } , SUCCESS ) : { error : INVALID_PARTICIPANT_IDS } ; }
Add entries into an event; optionally add to specified drawDefinition/flightProfile, if possible.
Add entries into an event ; optionally add to specified drawDefinition / flightProfile if possible .
// Ignore error if drawId is included but entry can't be added to drawDefinition/flightProfile // return error as info to client // now remove any unpaired participantIds which exist as part of added paired participants // because the method will be called below if necessary