change and delete anomaly

This commit is contained in:
tuanvu
2025-12-11 21:19:11 +01:00
parent 72dafa13c9
commit bc566c4f7f
3 changed files with 60 additions and 19 deletions

View File

@@ -2,6 +2,7 @@ import { addDoc, collection, doc, getDoc, getDocs, Timestamp, updateDoc } from "
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");
@@ -18,7 +19,7 @@ export async function getUsers(): Promise<User[]> {
return [];
}
}
///////////////////////////////////RESSOURCE////////////////////////////////
export async function getRessources(): Promise<Ressources[]> {
try {
const colRef = collection(db, "ressources");
@@ -35,7 +36,7 @@ export async function getRessources(): Promise<Ressources[]> {
return [];
}
}
///////////////////////////////////CHANTIER/////////////////////////////////
export async function getChantiers(): Promise<Chantier[]> {
const snap = await getDocs(collection(db, "chantier"));
const chantiers: Chantier[] = [];
@@ -72,16 +73,7 @@ export async function getChantiers(): Promise<Chantier[]> {
return chantiers;
}
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),
};
}
//CHANGE CHANTIER STATUS
export async function changeChantierStatus(chantierId: string, newStatus: string): Promise<void> {
try {
const chantierRef = doc(db, "chantier", chantierId);
@@ -92,6 +84,7 @@ export async function changeChantierStatus(chantierId: string, newStatus: string
}
}
//ADD CHANTIER
export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<string | null> {
try {
const colRef = collection(db, "chantier");
@@ -102,4 +95,57 @@ export async function addChantier(chantierData: Omit<Chantier, 'id'>): Promise<s
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),
};
}