nylas / nylas-nodejs

A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.
MIT License
167 stars 117 forks source link

`/availability` `openHours` badly parsed causing only Monday data #502

Closed victorigualada closed 9 months ago

victorigualada commented 9 months ago

Describe the bug

When passing an openHours array to the CalendarRestfulModelCollection#availability it wrongly parses the data and sets the same day for all openHours received, being 0(Monday). The error happens in the following line: https://github.com/nylas/nylas-nodejs/blob/cb42d16b3c597d4297e71303066f2266a66084ee/src/models/calendar-restful-model-collection.ts#L151 when creating instantiating the OpenHours object.

To Reproduce

  1. Create the following SingleAvailabilityQuery. Note that there two openHours, one for Monday (0) and one for Tuesday (1) :

    const options: SingleAvailabilityQuery = {
    duration: 20,
    interval: 30,
    startTime: 1700434800,
    endTime: 1701385199.999,
    openHours: [
    {
      emails: [],
      days: [ 0 ],
      timezone: "Europe/Madrid",
      start: "09:00",
      end: "11:00"
    },
    {
      emails: [],
      days: [ 1 ],
      timezone: "Europe/Madrid",
      start: "12:00",
      end: "14:00"
    }
    ],
    calendars: [
    {
      accountId: "$accountId",
      calendarIds: [ "$calendarId" ]
    }
    ]
    }
  2. Call the availability method:

    const nylas = Nylas.with(accessToken)
    const availabilities: CalendarAvailability = await nylas.calendars.availability(options)
  3. This will return only availabilities for Monday as explained in the Bug description due to the bad parsing of openHours. The parsed openHourswill be:

    {
    "open_hours": [
    {
      "object_type": "open_hours",
      "emails": [],
      "days": [
        0
      ],
      "timezone": "Europe/Madrid",
      "start": "09:00",
      "end": "21:00"
    },
    {
      "object_type": "open_hours",
      "emails": [],
      "days": [
        0
      ],
      "timezone": "Europe/Madrid",
      "start": "09:00",
      "end": "21:00"
    }
    ]
    }

    Note how both open_hours.days are [0] while they should be [0] and [1]

Expected behavior

It should return availability for all the days passed as openHours

SDK Version:

6.10.0

victorigualada commented 9 months ago

In fact, the test that should validate this behavior is broken. This is the test: https://github.com/nylas/nylas-nodejs/blob/27c7f9147d44a8f11f13884c7bb0bea43d171791/__tests__/calendar-restful-model-collection-spec.js#L161-L224

If you look at the day passed to the test in openHours: https://github.com/nylas/nylas-nodejs/blob/27c7f9147d44a8f11f13884c7bb0bea43d171791/__tests__/calendar-restful-model-collection-spec.js#L172

It's Days.Sunday and it's value is 6: https://github.com/nylas/nylas-nodejs/blob/27c7f9147d44a8f11f13884c7bb0bea43d171791/src/models/calendar-availability.ts#L22

The test succeeds but is expecting that the openHours.days is 0 (Monday)

victorigualada commented 9 months ago

I've found that the problem comes from this function: https://github.com/nylas/nylas-nodejs/blob/5af2a320ded4fb9c0d560a00188d9d5d73a1288b/src/models/attributes.ts#L110-L121

The function iterates over the keys of the array: for (const num in json). So it will return always 0 independently of the value of the array. I changed it to get the value using the key, but it breaks other tests that are dependant on this function:

if (!isNaN(Number(json[num]))) { 
  nums.push(Number(json[num])); 
} 
mrashed-dev commented 9 months ago

Thanks for opening up this issue @victorigualada, a PR has been opened to address this issue.