test chantier.user.getDoc
This commit is contained in:
@@ -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 { db } from "../firebase_config";
|
||||||
import { Timestamp } from "firebase/firestore";
|
import { Timestamp } from "firebase/firestore";
|
||||||
import { Chantier, User, Ressources, Reservation } from "../class/class";
|
import { Chantier, User, Ressources, Reservation } from "../class/class";
|
||||||
@@ -38,25 +38,37 @@ export async function getRessources(): Promise<Ressources[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getChantiers(): Promise<Chantier[]> {
|
export async function getChantiers(): Promise<Chantier[]> {
|
||||||
try {
|
const snap = await getDocs(collection(db, "chantiers"));
|
||||||
const colRef = collection(db, "chantier");
|
const chantiers: Chantier[] = [];
|
||||||
const snapshot = await getDocs(colRef);
|
|
||||||
return snapshot.docs.map((doc) => {
|
for (const docSnap of snap.docs) {
|
||||||
const data = doc.data() as any;
|
const data = docSnap.data();
|
||||||
return {
|
const dateDep = data.dateDep instanceof Timestamp ? data.dateDep.toDate() : new Date(data.dateDep);
|
||||||
...data,
|
let chef: User | null = null;
|
||||||
dateDep: data.dateDep.toDate(),
|
if (data.chef) {
|
||||||
chef: { ...data.chef },
|
const chefSnap = await getDoc(data.chef);
|
||||||
equipe: data.equipe ?? [],
|
if (chefSnap.exists()) {
|
||||||
materiel: data.materiel ?? [],
|
chef = chefSnap.data() as User;
|
||||||
vehicules: data.vehicules ?? [],
|
}
|
||||||
anomalies: data.anomalies ?? []
|
}
|
||||||
} as Chantier;
|
let equipe: User[] = [];
|
||||||
});
|
if (Array.isArray(data.equipe)) {
|
||||||
} catch (err) {
|
equipe = await Promise.all(
|
||||||
console.error("Firestore Chantiers Error:", err);
|
data.equipe.map(async (ref: any) => {
|
||||||
return [];
|
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 {
|
function convertReservation(res: any): Reservation {
|
||||||
|
|||||||
Reference in New Issue
Block a user