test reservation todo corriger getChantier avec la nouvelelle structure

This commit is contained in:
Rochas
2025-12-14 03:04:12 +01:00
parent 1f698076df
commit 27d13ace9d
10 changed files with 190 additions and 100 deletions

View File

@@ -1,4 +1,4 @@
import { addDoc, arrayUnion, collection, doc, getDoc, getDocs, Timestamp, updateDoc } from "firebase/firestore";
import { addDoc, arrayUnion, collection, doc, Firestore, getDoc, getDocs, Timestamp, updateDoc, DocumentReference } from "firebase/firestore";
import { Chantier, Reservation, Ressources, User } from "../class/class";
import { db } from "../firebase_config";
@@ -28,6 +28,7 @@ export async function getRessources(): Promise<Ressources[]> {
return snapshot.docs.map((doc) => {
const data = doc.data();
return {
id: doc.id,
...data,
allocation: data.allocation?.map(convertReservation) || [],
} as Ressources;
@@ -66,22 +67,46 @@ export async function getChantiers(): Promise<Chantier[]> {
chef = chefSnap.data() as User;
}
}
let equipe: User[] = [];
let equipe: Reservation[] = [];
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;
return snap.exists() ? (snap.data() as Reservation) : null;
})
).then(list => list.filter(x => x !== null)) as User[];
).then(list => list.filter(x => x !== null)) as Reservation[];
}
let vehicules: Reservation[] = [];
if (Array.isArray(data.vehicules)) {
vehicules = await Promise.all(
data.vehicules.map(async (ref: any) => {
const snap = await getDoc(ref);
return snap.exists() ? (snap.data() as Reservation) : null;
})
).then(list => list.filter(x => x !== null)) as Reservation[];
}
let materiel: Reservation[] = [];
if (Array.isArray(data.materiel)) {
materiel = await Promise.all(
data.materiel.map(async (ref: any) => {
const snap = await getDoc(ref);
return snap.exists() ? (snap.data() as Reservation) : null;
})
).then(list => list.filter(x => x !== null)) as Reservation[];
}
chantiers.push({
...data,
id: docSnap.id,
dateDep,
chef,
equipe
equipe,
vehicules,
materiel,
} as Chantier);
}
return chantiers;
@@ -144,12 +169,58 @@ export async function deleteAnomalie(chantierId: string, anomalie_String: string
}
}
function convertReservation(res: any): Reservation {
type ReservationFirestore = {
chantier: DocumentReference;
ressource: DocumentReference;
quantity: number;
};
async function convertReservation(res: any): Promise<Reservation> {
const data = res.data() as ReservationFirestore;
const chantierSnap = await getDoc(data.chantier as DocumentReference);
const ressourceSnap = await getDoc(data.ressource as DocumentReference);
return {
id: res.id,
dateChantier:
res.dateChantier instanceof Timestamp ? res.dateChantier.toDate() : new Date(res.dateChantier),
dateFin:
res.dateFin instanceof Timestamp ? res.dateFin.toDate() : new Date(res.dateFin),
chantier: chantierSnap.data() as Chantier,
ressource: ressourceSnap.data() as Ressources,
quantity: data.quantity,
};
}
}
//ENVOYER CHANTIER
export async function sendNewChantier(chantier:Chantier): Promise<void> {
const chantierRef = await addDoc(collection(db, "chantier"), {
name:chantier.name,
adresse:chantier.adresse,
etat:chantier.etat,
contact:chantier.contact,
chef: doc(db, "users", chantier.chef.id), //un objet déjà dans la base de donné
date: Timestamp.fromDate(chantier.dateDep),
tempsEst: chantier.tempsEst,
anomalies: chantier.anomalies ?? [], //strings[]
latitude: chantier.latitude,
longitude: chantier.longitude,
})
await Promise.all([
sendNewReservation(chantier.equipe, chantierRef.id),
sendNewReservation(chantier.materiel, chantierRef.id),
sendNewReservation(chantier.vehicules, chantierRef.id),
]);
}
export async function sendNewReservation(list: Reservation[],chantierId:string): Promise<void> {
const promises = list.map((reservation) =>
addDoc(collection(db,"Reservation"),{
chantier: doc(db, "chantier", chantierId),
ressource: doc(db, "ressources", reservation.ressource.id),
quantity: reservation.quantity,
})
);
await Promise.all(promises);
}