Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ansmlc opened this issue Dec 20, 2022 · 5 comments
Labels
needs info Waiting on more info to confirm issue.

Comments

@ansmlc
Copy link

ansmlc commented Dec 20, 2022

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 ansmlc changed the title Product doesn't get synced if there's another product with same slug Product data changes not geting properly synced to Sanity if there's another product with same slug Dec 20, 2022
@ansmlc
Copy link
Author

ansmlc commented Dec 23, 2022

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 ndimatteo added the needs info Waiting on more info to confirm issue. label Dec 28, 2022
@ndimatteo
Copy link
Owner

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
Copy link

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

@ndimatteo
Copy link
Owner

@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
Copy link
Author

ansmlc commented Jan 28, 2023

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

@ansmlc ansmlc closed this as completed Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs info Waiting on more info to confirm issue.
Projects
None yet
Development

No branches or pull requests

3 participants