square / square-nodejs-sdk

Other
88 stars 40 forks source link

RetrieveCatalogObject returns results out of order for related object images #154

Closed kyeshmz closed 1 month ago

kyeshmz commented 1 month ago

Describe the bug retrieveCatalogObject returns results out of order for related object images

Expected behavior Return the same order as in the GUI.

To Reproduce Steps to reproduce the bug:

  1. Call batch retrieve API through sdk
    1. Check result images order with GUI

Square SDK version square": "^37.1.1",

zenmasterjobo commented 1 month ago

hi @kyeshmz - can you provide a little more detail about your request and response?

I don't think order responses are going to be guaranteed on any of these batch API calls, but I just want to make sure I'm understanding your question.

kyeshmz commented 1 month ago

@zenmasterjobo I am utilizing the

 await squareClient.catalogApi.retrieveCatalogObject(
                id,
                true
            )

API call. It returns a category and images that are related to the certain object. The order of the images differs from the one that you can setup at square.online, where you can configure the order of images that would appear, for example in a carousel.

zenmasterjobo commented 1 month ago

Ah yes, I see. Unfortunately Square Online operates on different APIs that that of the APIs exposed on our public platform. And again in this case, order isn't guaranteed. I Will forward this feedback to the relevant team as I did with your previous post.

Also, for issues like this that concern the API and not the SDK, please use our developer forums - Thank you!

kyeshmz commented 1 month ago

It would be great to put it in the documentation somewhere!

kyeshmz commented 1 month ago

Is there a possiblity of making this the same as the Square Online? Currently, I get rate limited because related object images returns the wrong order, but querying by image Ids returns in order @zenmasterjobo

zenmasterjobo commented 1 month ago

Hi @kyeshmz

I don't think we can make the related objects list have the same sort order because the related objects can contain way more than just the images. However, because the image_ids array is respecting the correct order, we can use this array to sort the related object images so that way we don't need to query the API again.

After you have queried the object you want with retrieveCatalogObject(id, true) you can do something like this in javascript

let sorted = related_objects.sort((a, b) => image_ids.indexOf(a.id) - image_ids.indexOf(b.id));

By using javascript's sort method, we can write our own compare function that will sort the related objects to match the order of the image_ids array. This does assume that your related objects for a given catalog item only has image objects in it. If it does not, you would first need to go through the related objects and make a new array of only the objects that have type: 'IMAGE'

Let me know if you have any questions about this!

kyeshmz commented 3 weeks ago

@zenmasterjobo This worked for my usecase, thanks!