shift-org/shift-docs

Events endpoint daysInRange() is off by 1

Opened this issue · 1 comments

Adding a pagination object (#552) to the events endpoint exposed a bug in the daysInRange() function — it actually calculates the difference between the start and end dates, not the days in the range.

One example of this when requesting a single day, e.g. https://www.shift2bikes.org/api/events.php?startdate=2024-01-01&enddate=2024-01-01 — I was expecting a range of 1, but it returns 0 instead.

  "pagination": {
    "start": "2024-01-01",
    "end": "2024-01-01",
    "range": 0,
    "events": 2,
    "next": "https:\\/\\/shift2bikes.org\\/api\\/events.php?startdate=2024-01-02&enddate=2024-01-02"
  }

in the php, it looks like daysInRange() is used only for calculating "event range too large" and getPagination and ( the code is duplicated in both events.php and ical.php.)

in node, for range too large:

the node version uses dayjs diff ( https://day.js.org/docs/en/display/difference ) directly... where more obviously one day minus itself == 0 ( and compared against 100 for days )

in node for pagination:

the ported pagination procedure performs:

const range = lastDay.diff(firstDay, 'day');

so that value will be zero for the same day; could be given a +1 to change it into a "total number of days"

the actual database query

for both node and php the queries use greater/less than equal so they should be okay ( node example )

      where('eventdate', '>=', firstDay.toDate())
      .where('eventdate', '<=', lastDay.toDate())