I am working on a project using Firebase Firestore where I need to duplicate towns and associate them with an existing quest. Each journey contains multiple towns, and each town has a list of quests. The challenge is to find the correct journey for each town and duplicate the town while associating it with an existing quest.
Here’s the relevant part of my code:
<code>import { db } from "@src/firestore";
import {
arrayRemove,
arrayUnion,
collection,
deleteDoc,
doc,
getDocs,
orderBy,
setDoc,
updateDoc,
query,
collectionGroup,
where,
} from "firebase/firestore";
import toaster from "../components/common/Toaster";
import { getQuest } from "./QuestsServices";
import { sameDay } from "../components/utils";
import { currentJourney } from "../data/journeyConfig";
import { duplicateQuest } from "./QuestsServices";
...
export const duplicateTownsToExistingQuest = async (towns, existingQuestId) => {
try {
const journeysSnapshot = await getDocs(collection(db, "journey"));
const journeys = journeysSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
const promiseDuplicateTowns = towns.map(async (town) => {
const journey = journeys.find(journey => journey.towns.some(t => t.id === town.id));
if (!journey) {
throw new Error(`Aucun voyage ne contient la ville avec l'identifiant « ${town.id} ».`);
}
if (!town.quests.map(quest => quest.id).includes(existingQuestId)) {
throw new Error(`L'identifiant « ${existingQuestId} » de la quête n'a pas été trouvé dans les quêtes de la ville pour cette ville « ${town.title} ».`);
}
const journeyId = journey.id;
const newTown = {
...town,
quests: [existingQuestId],
title: `${town.title} (copie)`,
id: town.id + "-copy",
};
return createOrUpdateTown({ journeyId: journeyId, town: newTown });
});
await Promise.all(promiseDuplicateTowns);
return { success: true, message: "Les villes ont été dupliquées avec succès." };
} catch (error) {
console.error(error);
toaster.error({
title: "Ville erreur",
description: JSON.stringify(error),
});
return { success: false, message: error.message };
}
};
</code>
<code>import { db } from "@src/firestore";
import {
arrayRemove,
arrayUnion,
collection,
deleteDoc,
doc,
getDocs,
orderBy,
setDoc,
updateDoc,
query,
collectionGroup,
where,
} from "firebase/firestore";
import toaster from "../components/common/Toaster";
import { getQuest } from "./QuestsServices";
import { sameDay } from "../components/utils";
import { currentJourney } from "../data/journeyConfig";
import { duplicateQuest } from "./QuestsServices";
...
export const duplicateTownsToExistingQuest = async (towns, existingQuestId) => {
try {
const journeysSnapshot = await getDocs(collection(db, "journey"));
const journeys = journeysSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
const promiseDuplicateTowns = towns.map(async (town) => {
const journey = journeys.find(journey => journey.towns.some(t => t.id === town.id));
if (!journey) {
throw new Error(`Aucun voyage ne contient la ville avec l'identifiant « ${town.id} ».`);
}
if (!town.quests.map(quest => quest.id).includes(existingQuestId)) {
throw new Error(`L'identifiant « ${existingQuestId} » de la quête n'a pas été trouvé dans les quêtes de la ville pour cette ville « ${town.title} ».`);
}
const journeyId = journey.id;
const newTown = {
...town,
quests: [existingQuestId],
title: `${town.title} (copie)`,
id: town.id + "-copy",
};
return createOrUpdateTown({ journeyId: journeyId, town: newTown });
});
await Promise.all(promiseDuplicateTowns);
return { success: true, message: "Les villes ont été dupliquées avec succès." };
} catch (error) {
console.error(error);
toaster.error({
title: "Ville erreur",
description: JSON.stringify(error),
});
return { success: false, message: error.message };
}
};
</code>
import { db } from "@src/firestore";
import {
arrayRemove,
arrayUnion,
collection,
deleteDoc,
doc,
getDocs,
orderBy,
setDoc,
updateDoc,
query,
collectionGroup,
where,
} from "firebase/firestore";
import toaster from "../components/common/Toaster";
import { getQuest } from "./QuestsServices";
import { sameDay } from "../components/utils";
import { currentJourney } from "../data/journeyConfig";
import { duplicateQuest } from "./QuestsServices";
...
export const duplicateTownsToExistingQuest = async (towns, existingQuestId) => {
try {
const journeysSnapshot = await getDocs(collection(db, "journey"));
const journeys = journeysSnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
const promiseDuplicateTowns = towns.map(async (town) => {
const journey = journeys.find(journey => journey.towns.some(t => t.id === town.id));
if (!journey) {
throw new Error(`Aucun voyage ne contient la ville avec l'identifiant « ${town.id} ».`);
}
if (!town.quests.map(quest => quest.id).includes(existingQuestId)) {
throw new Error(`L'identifiant « ${existingQuestId} » de la quête n'a pas été trouvé dans les quêtes de la ville pour cette ville « ${town.title} ».`);
}
const journeyId = journey.id;
const newTown = {
...town,
quests: [existingQuestId],
title: `${town.title} (copie)`,
id: town.id + "-copy",
};
return createOrUpdateTown({ journeyId: journeyId, town: newTown });
});
await Promise.all(promiseDuplicateTowns);
return { success: true, message: "Les villes ont été dupliquées avec succès." };
} catch (error) {
console.error(error);
toaster.error({
title: "Ville erreur",
description: JSON.stringify(error),
});
return { success: false, message: error.message };
}
};
How could I test this? I mean, I try, but fail because the db are not injected but imported. So I wonder how can I do this… Maybe you can write me a sample or if it is not possible, just let me know…