async function ingestAthleteHistory(
  athleteDoc,
  recdLocationsForIngest = null,
  isDryRun = false,
) {
  // Make sure that we're not trying to ingest an empty array.
  // That would break stuff.
  let locationsForIngest;
  let locationsToLog;
  if (Array.isArray(recdLocationsForIngest) && recdLocationsForIngest.length) {
    locationsForIngest = recdLocationsForIngest;
    locationsToLog = `${locationsForIngest.join(', ')}
**Data for multi-location activites may be destroyed**`;
  } else {
    locationsForIngest = null;
    locationsToLog = 'All';
  }

  console.log(`----------------
Ingesting history for ${getAthleteIdentifier(athleteDoc)}
Locations: ${locationsToLog}
-------------------`);


  // Will check all known locations unless specified
  const asyncIngestAllLocations = makeArrayAsyncIterable(
    locationsForIngest || getLocationNames(),
    (loc) => ingestSingleLocation(loc, athleteDoc, { isDryRun }),
  );

  if (!isDryRun) {
    await clearAthleteHistoryV2(
      athleteDoc,
      locationsForIngest ? { location: { $in: locationsForIngest } } : {},
    );
  }

  // @todo revert back to existing athlete stats propery
  const allLocationsStats = {};

  // eslint-disable-next-line
  for await (const ingestResult of asyncIngestAllLocations) {
    if (ingestResult) {
      const {
        locationName,
        stats,
      } = ingestResult;
      allLocationsStats[locationName] = stats;
    }
  }

  if (!isDryRun) {
    await setAthleteAvailableYears(athleteDoc, allLocationsStats);
    // @todo
  }

  if (!isDryRun) {
    // YAY WE MADE IT!
    athleteDoc.set({ status: 'ready' });
    await athleteDoc.save();
  }

  if (!isDryRun) {
    const summary = Object.keys(allLocationsStats).reduce((acc, loc) => ({
      ...acc,
      [loc]: {
        total: allLocationsStats[loc].allTime,
        activites: allLocationsStats[loc].numActivities,
      },
    }), {});
    slackSuccess(`Ingested athlete history for ${athleteDoc.id}`, summary);
  }

  console.log(`
--------RESULTS---------
Ingested athlete history for ${athleteDoc.id}:
${JSON.stringify(allLocationsStats, null, 2)}
${isDryRun ? '**THIS WAS A DRY RUN**' : ''}`);
}
/**
 * Handle historical activities for new athlete
 *
 * @param {Athlete} Athlete athleteDoc
 * @param {Array|null} recdLocationsForIngest Array of location names, default to null
 * @param {Boolean} isDryRun Default to false
 */
Handle historical activities for new athlete
@param {Athlete} Athlete athleteDoc
@param {Array|null} recdLocationsForIngest Array of location names, default to null
@param {Boolean} isDryRun Default to false
Handle historical activities for new athlete @param { Athlete } Athlete athleteDoc @param { Array|null } recdLocationsForIngest Array of location names default to null @param { Boolean } isDryRun Default to false
async function ingestAthleteHistory(
  athleteDoc,
  recdLocationsForIngest = null,
  isDryRun = false,
) {
  let locationsForIngest;
  let locationsToLog;
  if (Array.isArray(recdLocationsForIngest) && recdLocationsForIngest.length) {
    locationsForIngest = recdLocationsForIngest;
    locationsToLog = `${locationsForIngest.join(', ')}
**Data for multi-location activites may be destroyed**`;
  } else {
    locationsForIngest = null;
    locationsToLog = 'All';
  }
  console.log(`----------------
Ingesting history for ${getAthleteIdentifier(athleteDoc)}
Locations: ${locationsToLog}
-------------------`);
  const asyncIngestAllLocations = makeArrayAsyncIterable(
    locationsForIngest || getLocationNames(),
    (loc) => ingestSingleLocation(loc, athleteDoc, { isDryRun }),
  );
  if (!isDryRun) {
    await clearAthleteHistoryV2(
      athleteDoc,
      locationsForIngest ? { location: { $in: locationsForIngest } } : {},
    );
  }
  const allLocationsStats = {};
  for await (const ingestResult of asyncIngestAllLocations) {
    if (ingestResult) {
      const {
        locationName,
        stats,
      } = ingestResult;
      allLocationsStats[locationName] = stats;
    }
  }
  if (!isDryRun) {
    await setAthleteAvailableYears(athleteDoc, allLocationsStats);
  }
  if (!isDryRun) {
    athleteDoc.set({ status: 'ready' });
    await athleteDoc.save();
  }
  if (!isDryRun) {
    const summary = Object.keys(allLocationsStats).reduce((acc, loc) => ({
      ...acc,
      [loc]: {
        total: allLocationsStats[loc].allTime,
        activites: allLocationsStats[loc].numActivities,
      },
    }), {});
    slackSuccess(`Ingested athlete history for ${athleteDoc.id}`, summary);
  }
  console.log(`
--------RESULTS---------
Ingested athlete history for ${athleteDoc.id}:
${JSON.stringify(allLocationsStats, null, 2)}
${isDryRun ? '**THIS WAS A DRY RUN**' : ''}`);
}
async function ingestAthleteHistory ( athleteDoc , recdLocationsForIngest = null , isDryRun = false , ) { let locationsForIngest ; let locationsToLog ; if ( Array . isArray ( recdLocationsForIngest ) && recdLocationsForIngest . length ) { locationsForIngest = recdLocationsForIngest ; locationsToLog = ` ${ locationsForIngest . join ( ', ' ) } ` ; } else { locationsForIngest = null ; locationsToLog = 'All' ; } console . log ( ` ${ getAthleteIdentifier ( athleteDoc ) } ${ locationsToLog } ` ) ; const asyncIngestAllLocations = makeArrayAsyncIterable ( locationsForIngest || getLocationNames ( ) , ( loc ) => ingestSingleLocation ( loc , athleteDoc , { isDryRun } ) , ) ; if ( ! isDryRun ) { await clearAthleteHistoryV2 ( athleteDoc , locationsForIngest ? { location : { $in : locationsForIngest } } : { } , ) ; } const allLocationsStats = { } ; for await ( const ingestResult of asyncIngestAllLocations ) { if ( ingestResult ) { const { locationName , stats , } = ingestResult ; allLocationsStats [ locationName ] = stats ; } } if ( ! isDryRun ) { await setAthleteAvailableYears ( athleteDoc , allLocationsStats ) ; } if ( ! isDryRun ) { athleteDoc . set ( { status : 'ready' } ) ; await athleteDoc . save ( ) ; } if ( ! isDryRun ) { const summary = Object . keys ( allLocationsStats ) . reduce ( ( acc , loc ) => ( { ... acc , [ loc ] : { total : allLocationsStats [ loc ] . allTime , activites : allLocationsStats [ loc ] . numActivities , } , } ) , { } ) ; slackSuccess ( ` ${ athleteDoc . id } ` , summary ) ; } console . log ( ` ${ athleteDoc . id } ${ JSON . stringify ( allLocationsStats , null , 2 ) } ${ isDryRun ? '**THIS WAS A DRY RUN**' : '' } ` ) ; }
Handle historical activities for new athlete
 @param {Athlete} Athlete athleteDoc
 @param {Array|null} recdLocationsForIngest Array of location names, default to null
 @param {Boolean} isDryRun Default to false
Handle historical activities for new athlete @param { Athlete } Athlete athleteDoc @param { Array|null } recdLocationsForIngest Array of location names default to null @param { Boolean } isDryRun Default to false
// Make sure that we're not trying to ingest an empty array. // That would break stuff. // Will check all known locations unless specified // @todo revert back to existing athlete stats propery // eslint-disable-next-line // @todo // YAY WE MADE IT!