test with firebase

This commit is contained in:
tuanvu
2025-12-09 13:32:09 +01:00
parent b0be1e98d9
commit 3bd08147e3
5 changed files with 307 additions and 297 deletions

View File

@@ -1,139 +1,126 @@
export class Chantier{
id: number;
adresse: string;
etat: string;
contact: string;
chef: Chef;
equipe : Equipier[];
materiel : Materiel[];
dateDep : string;
tempsEst : number;
vehicules : Vehicule[];
anomalies : string[];
import { collection, getDocs, doc, updateDoc } from "firebase/firestore";
import { db } from "../firebase_config";
import { Timestamp } from "firebase/firestore";
constructor(
id: number,
adresse: string,
etat: string,
contact: string,
chef: Chef,
equipe: Equipier[],
materiel: Materiel[],
dateDep: string,
tempsEst: number,
vehicules: Vehicule[],
anomalies: string[]
) {
this.id = id;
this.adresse = adresse;
this.etat = etat;
this.contact = contact;
this.chef = chef;
this.equipe = equipe;
this.materiel = materiel;
this.dateDep = dateDep;
this.tempsEst = tempsEst;
this.vehicules = vehicules;
this.anomalies = anomalies;
}
export type Chantier = {
id: number;
adresse: string;
etat: string;
contact: string;
chef: User;
equipe: User[];
materiel: Ressources[];
dateDep: Date;
tempsEst: number;
vehicules: Ressources[];
anomalies: string[];
};
export async function getChantiers(): Promise<Chantier[]> {
try {
const colRef = collection(db, "chantiers");
const snapshot = await getDocs(colRef);
return snapshot.docs.map((doc) => {
const data = doc.data() as any;
return {
...data,
chef: {
...data.chef,
allocation: data.chef?.allocation?.map(convertReservation) || [],
},
equipe:
data.equipe?.map((u: any) => ({
...u,
allocation: u.allocation?.map(convertReservation) || [],
})) || [],
materiel:
data.materiel?.map((m: any) => ({
...m,
allocation: m.allocation?.map(convertReservation) || [],
})) || [],
vehicules:
data.vehicules?.map((v: any) => ({
...v,
allocation: v.allocation?.map(convertReservation) || [],
})) || [],
anomalies: data.anomalies || [],
} as Chantier;
});
} catch (err) {
console.error("Firestore Chantiers Error:", err);
return [];
}
}
export abstract class Equipier{
id: number;
nom: string;
prenom: string;
allocation: Reservation[];
export type User = {
id: string;
name: string;
last_name: string;
allocation: Reservation[];
role: string;
qualifications: string;
};
constructor(id: number, nom:string, prenom:string, allocation: Reservation[]){
this.id = id;
this.nom = nom;
this.prenom = prenom;
this.allocation = allocation;
}
export async function getUsers(): Promise<User[]> {
try {
const colRef = collection(db, "users");
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 [];
}
}
export class Chef extends Equipier{
constructor(id: number, nom:string, prenom:string, allocation: Reservation[]){
super(id,nom,prenom,allocation);
}
export type Ressources = {
id: number;
name: string;
type: string;
Image: string;
quantity: number;
available_quantity: number;
allocation: Reservation[];
};
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 [];
}
}
export class Ouvrier extends Equipier{
qualification : string;
export type Reservation = {
id: string;
dateChantier: Date;
dateFin: Date;
};
constructor(id: number, nom:string, prenom:string, allocation: Reservation[], qualification: string){
super(id,nom,prenom,allocation);
this.qualification=qualification;
}
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),
};
}
export class ResponsableChantier extends Equipier{
constructor(id: number, nom:string, prenom:string, allocation: Reservation[]){
super(id,nom,prenom,allocation);
}
}
export class Materiel {
id: number;
nom: string;
prenom: string;
type: string;
allocation: Reservation[];
constructor(
id: number,
nom: string,
prenom: string,
type: string,
allocation: Reservation[]
) {
this.id = id;
this.nom = nom;
this.prenom = prenom;
this.type = type;
this.allocation = allocation;
}
}
export class Reservation {
id: string;
dateChantier: Date;
dateFin: Date;
duree: number;
constructor(
id: string,
dateChantier: Date,
dateFin: Date,
duree: number
) {
this.id = id;
this.dateChantier = dateChantier;
this.dateFin = dateFin;
this.duree = duree;
}
}
export class Vehicule {
id: number;
nom: string;
type: string;
allocation: Reservation[];
constructor(
id: number,
nom: string,
type: string,
allocation: Reservation[]
) {
this.id = id;
this.nom = nom;
this.type = type;
this.allocation = allocation;
}
}
export const exempleChantier = new Chantier(1,"Rennes","en cours","contact",new Chef(1,"Chyeef","YEE",[]),[],[],"01/01/25",10,[],["YEE"])