-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(homebrew): Fixed homebrew not being removed from characters when …
…deleted
- Loading branch information
1 parent
003fa5a
commit ac2bd97
Showing
2 changed files
with
44 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<unknown>[] = []; | ||
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<unknown>[] = []; | ||
|
||
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." | ||
); |