152 lines
5.0 KiB
TypeScript
152 lines
5.0 KiB
TypeScript
import { addDoc, collection, doc, getDoc, getDocs, Timestamp, updateDoc } 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 {
|
|
...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 {
|
|
...data,
|
|
allocation: data.allocation?.map(convertReservation) || [],
|
|
} as Ressources;
|
|
});
|
|
} catch (err) {
|
|
console.error("Firestore Ressources Error:", err);
|
|
return [];
|
|
}
|
|
}
|
|
///////////////////////////////////CHANTIER/////////////////////////////////
|
|
export async function getChantiers(): Promise<Chantier[]> {
|
|
const snap = await getDocs(collection(db, "chantier"));
|
|
const chantiers: 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: 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,
|
|
id: docSnap.id,
|
|
dateDep,
|
|
chef,
|
|
equipe
|
|
} as Chantier);
|
|
}
|
|
return chantiers;
|
|
}
|
|
|
|
//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 changeAnomalieStatus(chantierId: string, anomalie_String: string, newStatus: 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 || [];
|
|
const updatedAnomalies = anomalies.map((anomalie: any) => {
|
|
if (anomalie.description === anomalie_String) {
|
|
return { ...anomalie, status: newStatus };
|
|
}
|
|
return anomalie;
|
|
});
|
|
await updateDoc(chantierRef, { anomalies: updatedAnomalies });
|
|
console.log(`Anomalie status updated to ${newStatus}`);
|
|
} else {
|
|
console.error("Chantier not found");
|
|
}
|
|
} catch (err) {
|
|
console.error("Error", err);
|
|
}
|
|
}
|
|
|
|
//CHANGE 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 || [];
|
|
const updatedAnomalies = anomalies.filter((anomalie: any) => anomalie.description !== anomalie_String);
|
|
await updateDoc(chantierRef, { anomalies: updatedAnomalies });
|
|
console.log(`Anomalie deleted`);
|
|
} else {
|
|
console.error("Chantier not found");
|
|
}
|
|
} catch (err) {
|
|
console.error("Error", err);
|
|
}
|
|
}
|
|
|
|
function convertReservation(res: any): Reservation {
|
|
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),
|
|
};
|
|
}
|