Sanity.io & react-query: Chain deleting documents with references

60 views Asked by At

I have a situation where I want to archive a number of documents inside a document, then delete them, and then delete a document to which the documents were referenced to, and I get the usual delete error where Sanity tells me the document has references(even tho they just have been deleted). If I delete the document again after, it works. It is like my code is not waiting for the initial deletion. Any help would be appreciated

I have a document named register that has a collection of devices and a collection of nonConformites that reference to a device and to the register.

I have a archivedDevices array inside the register. I create a register doc that has the device and non conformities that I want to archive inside. I take a way all the references, keeping only the data I need to archive.

const deviceDoc = {
 ...device,
archivedNonConformities: [...nonConformitiesToArchive] 
}

const registerDoc = {
...register,
archivedDevices: [deviceDoc]
}

At first I update the register document using CreateRelace, where I add the registerDoc. in the code I also add any existing archived devices, all without any references.

using react-query, on success is now time to delete the orniginal nonConformities (that have the reference to the device). Once this is successful, is now time to finally delete the device, and here I get the error.

here is the delete documents function that I use to delete the nonConformities

const deleteDocuments = async (documents=[]) => {
  
        const queue = new PromiseQueue({
          concurrency: 11,
          intervalCap: 15,
          interval: 900,
        })
        let completedArray = []
        documents.forEach((item) => {

            queue.add(() => {
      
            return deleteDocument({documentId: item?._id},{
                onSuccess:()=>{
                    console.log("=====success deleted document====")
                    completedArray?.push(true)
                },
                onError:(err)=>{
                    console.log("===== error deleted document ====", err?.message, item?._id)
                    completedArray?.push(false)
                }
            })
          })
        })
        if(completedArray?.length == documents?.length && completedArray.every((x=>x==true)) ) return true
        return false
  
}

and this is the code i run onSuccess of the register update that should do the deletion.

        onSuccess:()=>{
            console.log("=====success updated register====")

            new Promise((myResolve, myReject) => {

                    const result =  deleteDocuments(nonConformities)
                    if(result) myResolve(console.log("_____resolved_____")); 

                }).then(()=>{
                    console.log("_____deleting device_____")
                    deleteDocuments([device]);
                })
            }

it should delete the device after the non conformities are deleted, instead it still finds the deleted references. If there are no nonConformities, the device is deleted with no problem, which of course is what happens if I run it a second time.

Maybe it is something simple, but I am really stuck. Thank you for your help!

0

There are 0 answers