async function doCommand({
  dryRun: isDryRun = false,
  reset: shouldReset = false,
  subargs = [],
  limit = 0,
  skip = 0,
}) {
  if (!subargs.length || getLocationNames().indexOf(subargs[0]) === -1) {
    console.warn(`Requires valid location, one of: ${JSON.stringify(getLocationNames())}`);
    return;
  }
  const location = subargs[0];

  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }

  const migrationKey = `ingest${location.toLowerCase()}`;
  const athleteDocs = await Athlete.find(
    { [`migration.${migrationKey}`]: { $ne: !shouldReset } },
    null,
    { limit, skip },
  );

  const migrateLocationForAthlete = async (athleteDoc) => {
    try {
      if (shouldReset) {
        await resetLocationForAthlete(
          athleteDoc,
          location,
          migrationKey,
          isDryRun,
        );
      } else {
        await ingestLocationForAthlete(
          athleteDoc,
          location,
          migrationKey,
          isDryRun,
        );
      }
      return { success: true };
    } catch (err) {
      captureSentry(err, 'ingestlocation', {
        athleteId: athleteDoc.id,
        location,
      });
      return { success: false, id: athleteDoc.id };
    }
  };

  console.log(`${shouldReset ? 'Resetting' : 'Ingesting'} location ${location} for ${athleteDocs.length} athletes`);

  const iterable = makeArrayAsyncIterable(
    athleteDocs,
    migrateLocationForAthlete,
  );

  const results = {
    Success: 0,
    Error: 0,
    ErrorIDs: [],
  };

  // eslint-disable-next-line
  for await (const result of iterable) {
    const {
      success,
      id = 0,
    } = result;
    if (success) {
      results.Success += 1;
    } else {
      results.Error += 1;
      results.ErrorIDs.push(id);
    }
  }
  console.table(results);

  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
}
/**
 * Ingest a specified location for all athletes
 */
Ingest a specified location for all athletes
Ingest a specified location for all athletes
async function doCommand({
  dryRun: isDryRun = false,
  reset: shouldReset = false,
  subargs = [],
  limit = 0,
  skip = 0,
}) {
  if (!subargs.length || getLocationNames().indexOf(subargs[0]) === -1) {
    console.warn(`Requires valid location, one of: ${JSON.stringify(getLocationNames())}`);
    return;
  }
  const location = subargs[0];
  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
  const migrationKey = `ingest${location.toLowerCase()}`;
  const athleteDocs = await Athlete.find(
    { [`migration.${migrationKey}`]: { $ne: !shouldReset } },
    null,
    { limit, skip },
  );
  const migrateLocationForAthlete = async (athleteDoc) => {
    try {
      if (shouldReset) {
        await resetLocationForAthlete(
          athleteDoc,
          location,
          migrationKey,
          isDryRun,
        );
      } else {
        await ingestLocationForAthlete(
          athleteDoc,
          location,
          migrationKey,
          isDryRun,
        );
      }
      return { success: true };
    } catch (err) {
      captureSentry(err, 'ingestlocation', {
        athleteId: athleteDoc.id,
        location,
      });
      return { success: false, id: athleteDoc.id };
    }
  };
  console.log(`${shouldReset ? 'Resetting' : 'Ingesting'} location ${location} for ${athleteDocs.length} athletes`);
  const iterable = makeArrayAsyncIterable(
    athleteDocs,
    migrateLocationForAthlete,
  );
  const results = {
    Success: 0,
    Error: 0,
    ErrorIDs: [],
  };
  for await (const result of iterable) {
    const {
      success,
      id = 0,
    } = result;
    if (success) {
      results.Success += 1;
    } else {
      results.Error += 1;
      results.ErrorIDs.push(id);
    }
  }
  console.table(results);
  if (isDryRun) {
    console.log(DRY_RUN_MSG);
  }
}
async function doCommand ( { dryRun : isDryRun = false , reset : shouldReset = false , subargs = [ ] , limit = 0 , skip = 0 , } ) { if ( ! subargs . length || getLocationNames ( ) . indexOf ( subargs [ 0 ] ) === - 1 ) { console . warn ( ` ${ JSON . stringify ( getLocationNames ( ) ) } ` ) ; return ; } const location = subargs [ 0 ] ; if ( isDryRun ) { console . log ( DRY_RUN_MSG ) ; } const migrationKey = ` ${ location . toLowerCase ( ) } ` ; const athleteDocs = await Athlete . find ( { [ ` ${ migrationKey } ` ] : { $ne : ! shouldReset } } , null , { limit , skip } , ) ; const migrateLocationForAthlete = async ( athleteDoc ) => { try { if ( shouldReset ) { await resetLocationForAthlete ( athleteDoc , location , migrationKey , isDryRun , ) ; } else { await ingestLocationForAthlete ( athleteDoc , location , migrationKey , isDryRun , ) ; } return { success : true } ; } catch ( err ) { captureSentry ( err , 'ingestlocation' , { athleteId : athleteDoc . id , location , } ) ; return { success : false , id : athleteDoc . id } ; } } ; console . log ( ` ${ shouldReset ? 'Resetting' : 'Ingesting' } ${ location } ${ athleteDocs . length } ` ) ; const iterable = makeArrayAsyncIterable ( athleteDocs , migrateLocationForAthlete , ) ; const results = { Success : 0 , Error : 0 , ErrorIDs : [ ] , } ; for await ( const result of iterable ) { const { success , id = 0 , } = result ; if ( success ) { results . Success += 1 ; } else { results . Error += 1 ; results . ErrorIDs . push ( id ) ; } } console . table ( results ) ; if ( isDryRun ) { console . log ( DRY_RUN_MSG ) ; } }
Ingest a specified location for all athletes
Ingest a specified location for all athletes
// eslint-disable-next-line