async fetchSegmentEfforts(page = 1, allEfforts = [], opts = {}) {
    if (!this.createdAt) {
      // We'd hit this if the Athlete document doesn't include athlete.createdAt
      // which means that we need to run the update athlete command for that user
      const apiAthlete = await fetchStravaAPI('/athlete', this.athleteDoc);
      this.createdAt = apiAthlete.created_at;
      slackError(`Update required for athlete ${this.athleteDoc._id}`);
    }
    const {
      createdAt,
      athleteDoc: {
        _id: athleteId,
      },
    } = this;

    const {
      limitPages = DEFAULT_FETCH_OPTS.limitPages,
      limitPerPage = DEFAULT_FETCH_OPTS.limitPerPage,
      start_date_local = createdAt,
    } = opts;
    const end_date_local = incrementDate(start_date_local, INCREMENT_DATE_BY);

    const params = {
      per_page: limitPerPage,
      segment_id: this.segmentId,
      start_date_local,
      end_date_local,
    };

    // Test if athlete is subscriber and has enough activities to require iterating by date
    // If athlete *does* have enough activities, this request will be wasted. oh well.
    // Can fix that eventually, or not.
    if (page === 1) {
      console.log(`Fetching segment efforts for athleteId ${athleteId} | createdAt ${createdAt}`);
      const prelimEfforts = await fetchStravaAPI('/segment_efforts', this.athleteDoc, {
        per_page: limitPerPage,
        segment_id: this.segmentId,
      });
      if (prelimEfforts.status === 402) {
        const msg = `athleteId ${athleteId} is not a subscriber`;
        console.log(msg);
        slackSuccess(msg);
        return [];
      }
      if (prelimEfforts.length < limitPerPage) {
        const msg = `Athlete only has ${prelimEfforts.length} efforts, no need to iterate by date`;
        console.log(msg);
        slackSuccess(msg);
        return prelimEfforts;
      }
    }

    console.log(`fetchSegmentEfforts | page ${page} | start_date_local ${params.start_date_local} | end_date_local ${params.end_date_local} | allEfforts.length ${allEfforts.length}`);
    const efforts = await fetchStravaAPI('/segment_efforts', this.athleteDoc, params);

    if (efforts.status && efforts.status !== 200) {
      if (efforts.status === 500 && this.retry500 > 0) {
        this.retry500 -= 1;
        console.log(`Strava API 500 error; ${this.retry500} retries remaining`);
        return this.fetchSegmentEfforts(
          page + 1,
          allEfforts,
          { start_date_local },
        );
      }
      captureSentry('Strava API response error', 'LocationIngest', {
        athleteId,
        path: '/segment_efforts',
        status: efforts.status,
        params,
      });
      return allEfforts;
    }

    console.log(`Received ${efforts.length} efforts`);

    if (
      allEfforts.length
      && efforts.length
      && allEfforts.slice(-1)[0].id === efforts[0].id
    ) {
      // I guess this could happen if a segment effort started
      // exactly on the previous end_date_local
      // which is the same as the new start_date_local
      efforts.shift();
    }

    const returnEfforts = [...allEfforts, ...efforts];

    if (new Date(end_date_local).valueOf() > Date.now() || page > 500) {
      console.log(`fetchSegmentEfforts finished with ${returnEfforts.length} efforts`);
      return returnEfforts;
    }

    // Enforce page limit when testing, Strava doesn't have this param
    if (limitPages && page >= limitPages) {
      console.log(`fetchSegmentEfforts hit page limit ${limitPages} with ${returnEfforts.length} efforts`);
      return returnEfforts;
    }

    return this.fetchSegmentEfforts(
      (page + 1),
      returnEfforts,
      {
        start_date_local: end_date_local, // start next request period from end of this request period
      },
    );
  }
/**
   * Get complete history of athlete's efforts for canonical segment
   *
   * @return {Array}
   */
Get complete history of athlete's efforts for canonical segment
@return {Array}
Get complete history of athlete ' s efforts for canonical segment @return { Array }
async fetchSegmentEfforts(page = 1, allEfforts = [], opts = {}) {
    if (!this.createdAt) {
      const apiAthlete = await fetchStravaAPI('/athlete', this.athleteDoc);
      this.createdAt = apiAthlete.created_at;
      slackError(`Update required for athlete ${this.athleteDoc._id}`);
    }
    const {
      createdAt,
      athleteDoc: {
        _id: athleteId,
      },
    } = this;
    const {
      limitPages = DEFAULT_FETCH_OPTS.limitPages,
      limitPerPage = DEFAULT_FETCH_OPTS.limitPerPage,
      start_date_local = createdAt,
    } = opts;
    const end_date_local = incrementDate(start_date_local, INCREMENT_DATE_BY);
    const params = {
      per_page: limitPerPage,
      segment_id: this.segmentId,
      start_date_local,
      end_date_local,
    };
    if (page === 1) {
      console.log(`Fetching segment efforts for athleteId ${athleteId} | createdAt ${createdAt}`);
      const prelimEfforts = await fetchStravaAPI('/segment_efforts', this.athleteDoc, {
        per_page: limitPerPage,
        segment_id: this.segmentId,
      });
      if (prelimEfforts.status === 402) {
        const msg = `athleteId ${athleteId} is not a subscriber`;
        console.log(msg);
        slackSuccess(msg);
        return [];
      }
      if (prelimEfforts.length < limitPerPage) {
        const msg = `Athlete only has ${prelimEfforts.length} efforts, no need to iterate by date`;
        console.log(msg);
        slackSuccess(msg);
        return prelimEfforts;
      }
    }
    console.log(`fetchSegmentEfforts | page ${page} | start_date_local ${params.start_date_local} | end_date_local ${params.end_date_local} | allEfforts.length ${allEfforts.length}`);
    const efforts = await fetchStravaAPI('/segment_efforts', this.athleteDoc, params);
    if (efforts.status && efforts.status !== 200) {
      if (efforts.status === 500 && this.retry500 > 0) {
        this.retry500 -= 1;
        console.log(`Strava API 500 error; ${this.retry500} retries remaining`);
        return this.fetchSegmentEfforts(
          page + 1,
          allEfforts,
          { start_date_local },
        );
      }
      captureSentry('Strava API response error', 'LocationIngest', {
        athleteId,
        path: '/segment_efforts',
        status: efforts.status,
        params,
      });
      return allEfforts;
    }
    console.log(`Received ${efforts.length} efforts`);
    if (
      allEfforts.length
      && efforts.length
      && allEfforts.slice(-1)[0].id === efforts[0].id
    ) {
      efforts.shift();
    }
    const returnEfforts = [...allEfforts, ...efforts];
    if (new Date(end_date_local).valueOf() > Date.now() || page > 500) {
      console.log(`fetchSegmentEfforts finished with ${returnEfforts.length} efforts`);
      return returnEfforts;
    }
    if (limitPages && page >= limitPages) {
      console.log(`fetchSegmentEfforts hit page limit ${limitPages} with ${returnEfforts.length} efforts`);
      return returnEfforts;
    }
    return this.fetchSegmentEfforts(
      (page + 1),
      returnEfforts,
      {
        start_date_local: end_date_local, 
      },
    );
  }
async fetchSegmentEfforts ( page = 1 , allEfforts = [ ] , opts = { } ) { if ( ! this . createdAt ) { const apiAthlete = await fetchStravaAPI ( '/athlete' , this . athleteDoc ) ; this . createdAt = apiAthlete . created_at ; slackError ( ` ${ this . athleteDoc . _id } ` ) ; } const { createdAt , athleteDoc : { _id : athleteId , } , } = this ; const { limitPages = DEFAULT_FETCH_OPTS . limitPages , limitPerPage = DEFAULT_FETCH_OPTS . limitPerPage , start_date_local = createdAt , } = opts ; const end_date_local = incrementDate ( start_date_local , INCREMENT_DATE_BY ) ; const params = { per_page : limitPerPage , segment_id : this . segmentId , start_date_local , end_date_local , } ; if ( page === 1 ) { console . log ( ` ${ athleteId } ${ createdAt } ` ) ; const prelimEfforts = await fetchStravaAPI ( '/segment_efforts' , this . athleteDoc , { per_page : limitPerPage , segment_id : this . segmentId , } ) ; if ( prelimEfforts . status === 402 ) { const msg = ` ${ athleteId } ` ; console . log ( msg ) ; slackSuccess ( msg ) ; return [ ] ; } if ( prelimEfforts . length < limitPerPage ) { const msg = ` ${ prelimEfforts . length } ` ; console . log ( msg ) ; slackSuccess ( msg ) ; return prelimEfforts ; } } console . log ( ` ${ page } ${ params . start_date_local } ${ params . end_date_local } ${ allEfforts . length } ` ) ; const efforts = await fetchStravaAPI ( '/segment_efforts' , this . athleteDoc , params ) ; if ( efforts . status && efforts . status !== 200 ) { if ( efforts . status === 500 && this . retry500 > 0 ) { this . retry500 -= 1 ; console . log ( ` ${ this . retry500 } ` ) ; return this . fetchSegmentEfforts ( page + 1 , allEfforts , { start_date_local } , ) ; } captureSentry ( 'Strava API response error' , 'LocationIngest' , { athleteId , path : '/segment_efforts' , status : efforts . status , params , } ) ; return allEfforts ; } console . log ( ` ${ efforts . length } ` ) ; if ( allEfforts . length && efforts . length && allEfforts . slice ( - 1 ) [ 0 ] . id === efforts [ 0 ] . id ) { efforts . shift ( ) ; } const returnEfforts = [ ... allEfforts , ... efforts ] ; if ( new Date ( end_date_local ) . valueOf ( ) > Date . now ( ) || page > 500 ) { console . log ( ` ${ returnEfforts . length } ` ) ; return returnEfforts ; } if ( limitPages && page >= limitPages ) { console . log ( ` ${ limitPages } ${ returnEfforts . length } ` ) ; return returnEfforts ; } return this . fetchSegmentEfforts ( ( page + 1 ) , returnEfforts , { start_date_local : end_date_local , } , ) ; }
Get complete history of athlete's efforts for canonical segment
 @return {Array}
Get complete history of athlete ' s efforts for canonical segment @return { Array }
// We'd hit this if the Athlete document doesn't include athlete.createdAt // which means that we need to run the update athlete command for that user // Test if athlete is subscriber and has enough activities to require iterating by date // If athlete *does* have enough activities, this request will be wasted. oh well. // Can fix that eventually, or not. // I guess this could happen if a segment effort started // exactly on the previous end_date_local // which is the same as the new start_date_local // Enforce page limit when testing, Strava doesn't have this param // start next request period from end of this request period