229 lines
7.2 KiB
TypeScript
229 lines
7.2 KiB
TypeScript
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";
|
|
|
|
///////////////////////////////////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: Omit<Ressources, 'id'>): Promise<string | null> {
|
|
try {
|
|
const colRef = collection(db, "ressources");
|
|
const ressourcesRef = await addDoc(colRef, ressourceData);
|
|
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 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: 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[];
|
|
}
|
|
|
|
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);
|
|
}
|
|
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 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> {
|
|
|
|
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: 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);
|
|
}
|