nylas / nylas-nodejs

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

Bad Request: `start_time` must be < `end_time` for free-busy/availability. #443

Closed AlexanderTserkovniy closed 1 year ago

AlexanderTserkovniy commented 1 year ago

Describe the bug Simple as a hell, just went to documentation and tried call for availability via Node.js sdk and instantly got issue.

To Reproduce Basically copy paste code from doc:

availability2 = await nylas.calendars.freeBusy({
      startTime: new Date().valueOf() + 100000,
      endTime: new Date().valueOf() + 10000000,
      calendars: [{
        accountId: "...", // 100% correct
        calendarIds: [
          "...",  // 100% correct
        ],
      }],
    });

Expected behavior You API should work, at least from documentation.

SDK Version: Latest for Node.js, mine is "^6.9.0"

AlexanderTserkovniy commented 1 year ago

Also I tried this endTime: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).valueOf()

AlexanderTserkovniy commented 1 year ago

Also it is reprodused in cURL:

curl --request POST \
  --url https://api.nylas.com/calendars/free-busy \
  --header 'Authorization: Bearer ....' \
  --header 'Content-Type: application/json' \
  --data '{
  "start_time": "1680324032597",
  "end_time": "1680325032597",
  "emails": [
    "..."
  ],
  "calendars": [
    {
      "account_id": "...",
      "calendar_ids": [
        "..."
      ]
    }
  ]
}

{"message":"start_timemust be <end_timefor free-busy/availability.","type":"invalid_request_error"}

AlexanderTserkovniy commented 1 year ago

After some trials I figured out that startTime and endTime are actually not number types...... here is new code, which still does not work:

const freeBusy = await nylas.calendars.freeBusy({
      startTime: new Date(),
      endTime: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
      emails: [...], // 100% correct
      calendar_ids: ["..."], // 100% correct 
    });

Error [Server Error]: An internal error occurred. If this issue persists, please contact support and copy/paste this request_uid: "ba3c40ba-9b8e-4082-8142-ea94f2e69869".

mrashed-dev commented 1 year ago

Hey @AlexanderTserkovniy Thanks for opening this issue. I think the issue is stemming from what value format you are setting for start and end time. It should be Epoch seconds, the values your providing looks like it's in milliseconds. That's why you're getting the same error on the SDK and the API. Here's a working code example, essentially the value you get back from new Date() needs to be divided by 1000 to get the Epoch seconds:

const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);

nylas.calendars.freeBusy({
    startTime: today / 1000,
    endTime: tomorrow / 1000,
    calendars: [{
        calendarIds: ["4vvcycguvlejcn0h3a3j64g8o"],
        accountId: "7rizprzv2xvupxn8oistnkdcq"
    }]
}).then((result) => {
    console.log(result);
});

Let me know if it doesn't work or if you have any other questions!