From 5550f0b9dd1e6a8fe02fae866abe1a227333ce3a Mon Sep 17 00:00:00 2001 From: tuanvu Date: Tue, 9 Dec 2025 15:21:08 +0100 Subject: [PATCH] test chantier.user.getDoc --- services/ressourcesService.ts | 50 ++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/services/ressourcesService.ts b/services/ressourcesService.ts index 4006db4..8c9d4f2 100644 --- a/services/ressourcesService.ts +++ b/services/ressourcesService.ts @@ -1,4 +1,4 @@ -import { collection, getDocs, doc, updateDoc } from "firebase/firestore"; +import { collection, getDocs, getDoc } from "firebase/firestore"; import { db } from "../firebase_config"; import { Timestamp } from "firebase/firestore"; import { Chantier, User, Ressources, Reservation } from "../class/class"; @@ -38,25 +38,37 @@ export async function getRessources(): Promise { } export async function getChantiers(): Promise { - try { - const colRef = collection(db, "chantier"); - const snapshot = await getDocs(colRef); - return snapshot.docs.map((doc) => { - const data = doc.data() as any; - return { - ...data, - dateDep: data.dateDep.toDate(), - chef: { ...data.chef }, - equipe: data.equipe ?? [], - materiel: data.materiel ?? [], - vehicules: data.vehicules ?? [], - anomalies: data.anomalies ?? [] - } as Chantier; - }); - } catch (err) { - console.error("Firestore Chantiers Error:", err); - return []; + const snap = await getDocs(collection(db, "chantiers")); + const chantiers: Chantier[] = []; + + for (const docSnap of snap.docs) { + const data = docSnap.data(); + const dateDep = data.dateDep instanceof Timestamp ? data.dateDep.toDate() : new Date(data.dateDep); + let chef: User | null = null; + if (data.chef) { + const chefSnap = await getDoc(data.chef); + if (chefSnap.exists()) { + chef = chefSnap.data() as User; + } + } + let equipe: User[] = []; + if (Array.isArray(data.equipe)) { + equipe = await Promise.all( + data.equipe.map(async (ref: any) => { + const snap = await getDoc(ref); + return snap.exists() ? (snap.data() as User) : null; + }) + ).then(list => list.filter(x => x !== null)) as User[]; + } + + chantiers.push({ + ...data, + dateDep, + chef, + equipe + } as Chantier); } + return chantiers; } function convertReservation(res: any): Reservation {