From ac2bd97b93c3ab2a8b4b145d7987017b8c23e6be Mon Sep 17 00:00:00 2001 From: Scott Benton Date: Wed, 29 May 2024 23:11:46 -0400 Subject: [PATCH] fix(homebrew): Fixed homebrew not being removed from characters when deleted --- CHANGELOG.MD | 1 + .../homebrew/deleteHomebrewExpansion.ts | 52 +++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 0a5645b3..5932a653 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -20,6 +20,7 @@ - Fixed "replaced" oracle tables not showing children - Fixed "replaced" move tables showing up twice - Fixed adds being removed from the move dialogs +- Fixed homebrew collections not being removed from characters when deleted ## 3.0.0 diff --git a/src/api-calls/homebrew/deleteHomebrewExpansion.ts b/src/api-calls/homebrew/deleteHomebrewExpansion.ts index 765ff2ce..54df0630 100644 --- a/src/api-calls/homebrew/deleteHomebrewExpansion.ts +++ b/src/api-calls/homebrew/deleteHomebrewExpansion.ts @@ -1,22 +1,56 @@ -import { deleteDoc } from "firebase/firestore"; +import { + arrayRemove, + deleteDoc, + getDocs, + query, + updateDoc, + where, +} from "firebase/firestore"; import { getHomebrewCollectionDoc } from "./_getRef"; import { createApiFunction } from "api-calls/createApiFunction"; +import { getCharacterCollection } from "api-calls/character/_getRef"; +import { getCampaignCollection } from "api-calls/campaign/_getRef"; export const deleteHomebrewExpansion = createApiFunction<{ id: string }, void>( - (params) => { + async (params) => { const { id } = params; - return new Promise((resolve, reject) => { - const promises: Promise[] = []; + const characterLoadPromise = getDocs( + query( + getCharacterCollection(), + where("expansionIds", "array-contains", id) + ) + ); - promises.push(deleteDoc(getHomebrewCollectionDoc(id))); + const campaignLoadPromise = getDocs( + query( + getCampaignCollection(), + where("expansionIds", "array-contains", id) + ) + ); - Promise.all(promises) - .then(() => { - resolve(); + const characterSnapshot = await characterLoadPromise; + const campaignSnapshot = await campaignLoadPromise; + + const promises: Promise[] = []; + + characterSnapshot.docs.forEach((doc) => { + promises.push( + updateDoc(doc.ref, { + expansionIds: arrayRemove(id), }) - .catch(reject); + ); }); + campaignSnapshot.docs.forEach((doc) => { + promises.push( + updateDoc(doc.ref, { + expansionIds: arrayRemove(id), + }) + ); + }); + + await Promise.all(promises); + deleteDoc(getHomebrewCollectionDoc(id)); }, "Failed to delete expansion." );