gestion des ressources fonctionnelle, mais pas fini
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { addDoc, arrayUnion, collection, doc, Firestore, getDoc, getDocs, Timestamp, updateDoc, DocumentReference } from "firebase/firestore";
|
||||
import { addDoc, arrayUnion, collection, doc, Firestore, getDoc, getDocs, Timestamp, updateDoc, DocumentReference, query, where } from "firebase/firestore";
|
||||
import { Chantier, Reservation, Ressources, User } from "../class/class";
|
||||
import { db } from "../firebase_config";
|
||||
|
||||
@@ -53,67 +53,100 @@ export async function addRessources(ressourceData: Omit<Ressources, 'id'>): Prom
|
||||
}
|
||||
///////////////////////////////////CHANTIER/////////////////////////////////
|
||||
export async function getChantiers(): Promise<Chantier[]> {
|
||||
const snap = await getDocs(collection(db, "chantier"));
|
||||
const chantiers: Chantier[] = [];
|
||||
try {
|
||||
const snap = await getDocs(collection(db, "chantier"));
|
||||
|
||||
for (const docSnap of snap.docs) {
|
||||
const data = docSnap.data();
|
||||
//Faut convertir les Timestamp en Date ( merci à firebase :) )
|
||||
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: 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 Reservation) : null;
|
||||
})
|
||||
).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[];
|
||||
}
|
||||
for (const docSnap of snap.docs) {
|
||||
try {
|
||||
const data = docSnap.data();
|
||||
//Faut convertir les Timestamp en Date ( merci à firebase :) )
|
||||
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;
|
||||
}
|
||||
}
|
||||
const equipe:Reservation[] = [];
|
||||
const vehicules:Reservation[] = [];
|
||||
const materiel:Reservation[] = [];
|
||||
const all:Reservation[] = await getReservationsByChantier(docSnap.id);
|
||||
|
||||
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[];
|
||||
}
|
||||
*/
|
||||
var equipe:Reservation[] = [];
|
||||
var vehicules:Reservation[] = [];
|
||||
var materiel:Reservation[] = [];
|
||||
chantiers.push({
|
||||
...data,
|
||||
id: docSnap.id,
|
||||
dateDep,
|
||||
chef,
|
||||
equipe,
|
||||
vehicules,
|
||||
materiel,
|
||||
} as Chantier);
|
||||
all.forEach(element => {
|
||||
if(element.ressource.type==="Ouvrier"){
|
||||
equipe.push(element)
|
||||
}
|
||||
else if(element.ressource.type==="Machine"){
|
||||
vehicules.push(element)
|
||||
}
|
||||
else if(element.ressource.type==="Outil"){
|
||||
materiel.push(element)
|
||||
}
|
||||
});
|
||||
|
||||
chantiers.push({
|
||||
...data,
|
||||
id: docSnap.id,
|
||||
dateDep,
|
||||
chef,
|
||||
equipe,
|
||||
vehicules,
|
||||
materiel,
|
||||
} as Chantier);
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la lecture d'un chantiers : " + error);
|
||||
//alert("Erreur lors de la lecture d'un chantiers : " + error);
|
||||
}
|
||||
}
|
||||
return chantiers;
|
||||
} catch (error) {
|
||||
alert("Erreur lors de la lecture des chantiers : " + error);
|
||||
}
|
||||
return chantiers
|
||||
}
|
||||
|
||||
//récupère les reservations d'un chantier
|
||||
export async function getReservationsByChantier(chantierId: string): Promise<Reservation[]> {
|
||||
const q = query(
|
||||
collection(db, "Reservation"),
|
||||
where("chantier", "==", doc(db, "chantier", chantierId)),
|
||||
);
|
||||
|
||||
const snap = await getDocs(q);
|
||||
|
||||
const results = await Promise.all(
|
||||
snap.docs.map(convertReservation)
|
||||
);
|
||||
|
||||
return results.filter(
|
||||
(r): r is Reservation => r !== null
|
||||
);
|
||||
}
|
||||
|
||||
///////////////////////////////////RESERVATION/////////////////////////////////
|
||||
export async function getReservations(): Promise<Reservation[]> {
|
||||
try {
|
||||
const snap = await getDocs(collection(db, "Reservation"));
|
||||
|
||||
const results = await Promise.all(
|
||||
snap.docs.map(convertReservation)
|
||||
);
|
||||
|
||||
return results.filter(
|
||||
(r): r is Reservation => r !== null
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la lecture des Reservations : " + error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//CHANGE CHANTIER STATUS
|
||||
export async function changeChantierStatus(chantierId: string, newStatus: string): Promise<void> {
|
||||
try {
|
||||
@@ -177,18 +210,23 @@ type ReservationFirestore = {
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
async function convertReservation(res: any): Promise<Reservation> {
|
||||
async function convertReservation(res: any): Promise<Reservation|null> {
|
||||
try {
|
||||
const data = res.data() as ReservationFirestore;
|
||||
|
||||
const data = res.data() as ReservationFirestore;
|
||||
const chantierSnap = await getDoc(data.chantier as DocumentReference);
|
||||
const ressourceSnap = await getDoc(data.ressource as DocumentReference);
|
||||
const chantierSnap = await getDoc(data.chantier as DocumentReference);
|
||||
const ressourceSnap = await getDoc(data.ressource as DocumentReference);
|
||||
|
||||
return {
|
||||
id: res.id,
|
||||
chantier: chantierSnap.data() as Chantier,
|
||||
ressource: ressourceSnap.data() as Ressources,
|
||||
quantity: data.quantity,
|
||||
};
|
||||
return {
|
||||
id: res.id,
|
||||
chantier: chantierSnap.data() as Chantier,
|
||||
ressource: ressourceSnap.data() as Ressources,
|
||||
quantity: data.quantity,
|
||||
};
|
||||
} catch (err) {
|
||||
console.warn("Reservation ignorée :", res.id , err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +239,7 @@ export async function sendNewChantier(chantier:Chantier): Promise<void> {
|
||||
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é
|
||||
chef: doc(db, "user", chantier.chef.id), //un objet déjà dans la base de donné
|
||||
date: Timestamp.fromDate(chantier.dateDep),
|
||||
tempsEst: chantier.tempsEst,
|
||||
anomalies: chantier.anomalies ?? [], //strings[]
|
||||
|
||||
Reference in New Issue
Block a user