282 lines
8.4 KiB
TypeScript
282 lines
8.4 KiB
TypeScript
import { addDoc, arrayUnion, collection, doc, DocumentReference, getDoc, getDocs, query, Timestamp, updateDoc, where } from "firebase/firestore";
|
|
import { Chantier, Reservation, Ressources, User } from "../class/class";
|
|
import { db } from "../firebase_config";
|
|
|
|
///////////////////////////////////USER/////////////////////////////////////
|
|
export async function getUsers(): Promise<User[]> {
|
|
try {
|
|
const colRef = collection(db, "user");
|
|
const snapshot = await getDocs(colRef);
|
|
return snapshot.docs.map((doc) => {
|
|
const data = doc.data();
|
|
return {
|
|
id: doc.id,
|
|
...data,
|
|
//allocation: data.allocation?.map(convertReservation) || [],
|
|
} as User;
|
|
});
|
|
} catch (err) {
|
|
console.error("Firestore Users Error:", err);
|
|
return [];
|
|
}
|
|
}
|
|
///////////////////////////////////RESSOURCE////////////////////////////////
|
|
export async function getRessources(): Promise<Ressources[]> {
|
|
try {
|
|
const colRef = collection(db, "ressources");
|
|
const snapshot = await getDocs(colRef);
|
|
return snapshot.docs.map((doc) => {
|
|
const data = doc.data();
|
|
return {
|
|
id: doc.id,
|
|
...data,
|
|
//allocation: data.allocation?.map(convertReservation) || [],
|
|
} as Ressources;
|
|
});
|
|
} catch (err) {
|
|
console.error("Firestore Ressources Error:", err);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
//ADD RESSOURCES
|
|
export async function addRessources(ressourceData: Ressources): Promise<string | null> {
|
|
try {
|
|
const ressourcesRef = await addDoc(collection(db, "ressources"), {
|
|
name:ressourceData.name,
|
|
type:ressourceData.type,
|
|
Image: ressourceData.Image,
|
|
quantity: ressourceData.quantity,
|
|
});
|
|
|
|
console.log(`Ressource ajoutée avec ID: ${ressourcesRef.id}`);
|
|
return ressourcesRef.id;
|
|
} catch (err) {
|
|
console.error("Error adding:", err);
|
|
return null;
|
|
}
|
|
}
|
|
///////////////////////////////////CHANTIER/////////////////////////////////
|
|
export async function getChantiers(): Promise<Chantier[]> {
|
|
const chantiers: Chantier[] = [];
|
|
try {
|
|
const snap = await getDocs(collection(db, "chantier"));
|
|
|
|
|
|
|
|
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);
|
|
|
|
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 {
|
|
const chantierRef = doc(db, "chantier", chantierId);
|
|
await updateDoc(chantierRef, { etat: newStatus });
|
|
console.log("Chantier ${chantierId} status updated to ${newStatus}");
|
|
} catch (err) {
|
|
console.error("Error", err);
|
|
}
|
|
}
|
|
|
|
//ADD CHANTIER
|
|
export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<string | null> {
|
|
try {
|
|
const colRef = collection(db, "chantier");
|
|
const chantierRef = await addDoc(colRef, chantierData);
|
|
//console.log(`Chantier added with ID: ${chantierRef.id}`);
|
|
return chantierRef.id;
|
|
} catch (err) {
|
|
console.error("Error adding:", err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//CHANGE CHANTIER ANOMALIE STATUS
|
|
export async function addAnomalie(chantierId: string, anomalie_String: string): Promise<void> {
|
|
try {
|
|
const chantierRef = doc(db, "chantier", chantierId);
|
|
await updateDoc(chantierRef, {
|
|
anomalies: arrayUnion(anomalie_String)
|
|
});
|
|
console.log("Anomalie added");
|
|
} catch (err) {
|
|
console.error("Error adding anomalie:", err);
|
|
}
|
|
}
|
|
|
|
//DELETE CHANTIER ANOMALIE STATUS
|
|
export async function deleteAnomalie(chantierId: string, anomalie_String: string): Promise<void> {
|
|
try {
|
|
const chantierRef = doc(db, "chantier", chantierId);
|
|
const chantierSnap = await getDoc(chantierRef);
|
|
if (chantierSnap.exists()) {
|
|
const chantierData = chantierSnap.data();
|
|
const anomalies = chantierData.anomalies || [];
|
|
//Filtage
|
|
const updatedAnomalies = anomalies.filter((anomaly: string) => anomaly !== anomalie_String);
|
|
await updateDoc(chantierRef, { anomalies: updatedAnomalies });
|
|
console.log("Anomalie deleted");
|
|
} else {
|
|
console.error("Chantier not found");
|
|
}
|
|
} catch (err) {
|
|
console.error("Error", err);
|
|
}
|
|
}
|
|
|
|
type ReservationFirestore = {
|
|
chantier: DocumentReference;
|
|
ressource: DocumentReference;
|
|
quantity: number;
|
|
};
|
|
|
|
async function convertReservation(res: any): Promise<Reservation|null> {
|
|
try {
|
|
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,
|
|
chantier: {
|
|
id: chantierSnap.id,
|
|
...(chantierSnap.data() as Omit<Chantier, "id">),
|
|
},
|
|
ressource: {
|
|
id: ressourceSnap.id,
|
|
...(ressourceSnap.data() as Omit<Ressources, "id">),
|
|
},
|
|
quantity: data.quantity,
|
|
};
|
|
} catch (err) {
|
|
console.warn("Reservation ignorée :", res.id , err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//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, "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[]
|
|
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> {
|
|
list.forEach(reservation => {
|
|
console.log("log: " + reservation.ressource.id);
|
|
});
|
|
|
|
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);
|
|
}
|