Skip to content

Commit

Permalink
fix(homebrew): Fixed homebrew not being removed from characters when …
Browse files Browse the repository at this point in the history
…deleted
  • Loading branch information
scottbenton committed May 30, 2024
1 parent 003fa5a commit ac2bd97
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 43 additions & 9 deletions src/api-calls/homebrew/deleteHomebrewExpansion.ts
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."
);

0 comments on commit ac2bd97

Please sign in to comment.