ndimatteo / HULL

💀 Headless Shopify Starter – powered by Next.js + Sanity.io
https://hull.dev
MIT License
1.36k stars 167 forks source link

Product data changes not geting properly synced to Sanity if there's another product with same slug #116

Closed ansmlc closed 1 year ago

ansmlc commented 1 year ago

In scenario where there's two products with an identical slug field present in Sanity Studio, changing product data in Shopify doesn't update (any of) the auto-synced product data in Sanity. This seems strange because the update API takes into consideration product's id, which is of course different. At the same time, the functions log in Vercel shows successfully sync message every time.

If there's a new brand new product added (that doesn't have a duplicate) sync works as expected, which suggests the issue is specifically related to there being a duplicate slug field:

vercel_function_logs

The yellow-marked part is a console-log is from product-update.js, checking the "URL Handle" Shopify field :


api/shopify/product-update.js (line 78)

console.log(handle, '**** Current handle ****')

The handle that's printed is in fact the correct, newly edited URL Handle from Shopify. However, it doesn't get reflected in Sanity:

image

I than tried making some changes the API to force a change for the slug field, in :

// api/shopify/product-update.js

 const slugField = {
    slug: {
      current: handle,
    }
  }

  // patch (update) slug, missing or not 
  stx = stx.patch(`product-${id}`, (patch) => patch.set(slugField))

// Add to productCompare set:
  const productCompare = {
    ...product,
    ...productFields,
    ...slugField,
    ......
  }

That didn't work so I tried to unset the field:


 // unset slug field

 const slugField = ["slug"];
 stx = stx.patch(`product-${id}`, (patch) => patch.unset(slugField))

which didn't work either.

As to why there are products with duplicate slug values, it's a result of switching to different Shopify store which had cloned all product data from previous store, so now there's duplicate products with duplicate slugs, neither of which can be deleted, and the new products can't be published either because of the duplicate slug validation error.

ansmlc commented 1 year ago

I've managed to "remove" one of the duplicate products by hardcoding its ID to product-delete.js API: stx = stx.patch('product-${specific-id}', (patch) => patch.set({ wasDeleted: true }))

The product is now marked as "removed" in Sanity but the document is still there, and still giving the same "slug in use" warning:

image

Because of this slug conflict error the client still can't publish the proper product.

Any idea what other patch or mutation would perhaps be more useful?

ndimatteo commented 1 year ago

Hey there @ansmlc by default Sanity checks for uniqueness on slug fields for documents.

This is why it's not updating anything in Sanity when the sync function occurs. You can actually see the error message after the sync starts in your first screenshot:

Screenshot 2022-12-28 at 4 18 02 PM

You'll need to either delete the previous documents (and remove all references to those elsewhere first), or you can try overriding the default validation for the product slug fields in your schema to allow it to create product documents with the same slug values.

Let me know if that helps! 🤘

raqibnur commented 1 year ago

is there any way to check the localhost sanity error log? mine is not syncing

ndimatteo commented 1 year ago

@Raqibnur please open a separate issue with more details about what you are experiencing, instead of commenting on other people's issues that are still open 🤘

ansmlc commented 1 year ago

@ndimatteo This has been resolved by rolling back to an older dataset backup and avoiding having same-slug documents.