redguava / cliniko-api

The API for Cliniko
73 stars 39 forks source link

Server not fetching unavailability blocks anymore. #329

Closed Phyxable closed 3 years ago

Phyxable commented 3 years ago

i ran into this issue a week back that my server is not fetching all of the unavailability blocks from cliniko api. What happened was that my subscription expired a week ago, then when i resumed it i changed my plan from 2-5 practitioners to just 1 practitioner. I don't know if that might have caused the problem. What makes it more weird is that its fetching the unavailability blocks from the saturdays and sundays only. 

Here is my code  app.get('/unavailable_blocks', async(req, res) => { var axios = require('axios'); var data = '';

var config = { method: 'get', url: 'https://api.ca1.cliniko.com/v1/unavailable_blocks', headers: headers, data: data };

axios(config) .then(function (response) { return res.status(200).json({ error: false, data: response.data }) }) .catch(function (error) {

return res.status(400).json({ error: true, error }) }); })

Please help me out.

bpinto commented 3 years ago

@Phyxable is it possible that you have more than 50 unavailable blocks? Read about paginations here.

Phyxable commented 3 years ago

The total number of entries that the JSON response is showing is 237. Are you referring to that?

bpinto commented 3 years ago

Yes, that's the total number of entries you can fetch using that end point + params, but these are paginated. I'm assuming you are not fetching all pages and thus only reading 50 of them.

Phyxable commented 3 years ago

But when i hit Cliniko's API end point app.get('/unavailable_blocks', async(req, res) =>{, the JSON response doesn't even contain the unavailable_blocks that are on the weekdays but it has the ones on weekends. Is that normal?

hagen commented 3 years ago

@Phyxable there's a few things we can check to make sure that you're receiving all your unavailable_blocks. The first, as @bpinto mentioned, is that in Cliniko's response to your request, we only send 50 unavailable_blocks at a time. If you have 237, you'll need to issue 5 calls all together, paginating through each set of 50 results. So it may just be that your current weekday unavailable_blocks are further down the list, beyond the initial 50.

There's a link further up in this conversation for pagination, but essentially you'd be adding query string parameters onto your request, like so: GET /v1/unavailable_blocks?page=3&per_page=50 << this request would get you the third set of 50 records.

In axios land, something like:

app.get('/unavailable_blocks?page=3&per_page=50', async(req, res) => {
  // do magic here
});

The other thing you may use are the filter parameters for unavailable_blocks, detailed here: filtering-unavailable-blocks

Let us know if either of these help, and if you're still only seeing weekend unavailable blocks 😊

Phyxable commented 3 years ago

@bpinto @hagen Thank you for suggesting that. i tried implementing the filter but still haven't been able to resolve the issue completely. url: https://api.ca1.cliniko.com/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&per_page=100. That's what i changed so far , but if i create any new unavailable blocks in Cliniko it's not fetching those ones but it's fetching the ones that exist already.

bpinto commented 3 years ago

To simplify the examples, I'm going to use a per_page=1 parameter so that only one unavailable block is returned.

There are 2 unavailable blocks that the API should return, which is confirmed in the field total_entries that displays the number of entries available to fetch, regardless of the number of records returned in that response. However as the response is paginated every 1 record, it only returns a single record.

# https://api.ca1.cliniko.com/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&per_page=1

{
   "unavailable_blocks":[
      {
         "id":"527973841729750818",
         "total_entries":2,
         "links":{
            "self":"https://local.cliniko.test/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&page=1&per_page=1",
            "next":"https://local.cliniko.test/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&page=2&per_page=1"
         }
      }
  ]
}

Alongside the total_entries attribute, there is also a links.next attribute that assists you on fetching the next page of responses. Using that attribute to fetch the next page will give me the remaining records.

# https://api.ca1.cliniko.com/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&per_page=1&page=2

{
   "unavailable_blocks":[
      {
         "id":"527973874772477731",
         "total_entries":2,
         "links":{
            "self":"https://local.cliniko.test/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&page=2&per_page=1",
            "previous":"https://local.cliniko.test/v1/unavailable_blocks?q=starts_at:>2020-12-21T18:00:00Z&page=2&per_page=1"
         }
      }
  ]
}

Do notice that at this point, there is no longer a links.next attribute since I've gone through all pages already.


I'm going to close this issue since it's not an issue with the API but rather an issue with understanding how pagination works. I do hope my explanation helps you moving forward but if that's not the case you may need to look for tutorials on the internet on how to use a rest API.