import { clientService } from './api'; import { Client } from '../types/btp'; type ClientFormData = Partial; class ClientService { private readonly basePath = '/api/clients'; /** * Récupérer tous les clients */ async getAll(): Promise { return await clientService.getAll(); } /** * Récupérer un client par ID */ async getById(id: string): Promise { return await clientService.getById(id); } /** * Créer un nouveau client */ async create(client: ClientFormData): Promise { return await clientService.create(client as Partial); } /** * Modifier un client existant */ async update(id: string, client: ClientFormData): Promise { return await clientService.update(id, client as Partial); } /** * Supprimer un client */ async delete(id: string): Promise { await clientService.delete(id); } /** * Rechercher des clients */ async search(query: string): Promise { return await clientService.searchByNom(query); } /** * Récupérer les chantiers d'un client */ async getChantiers(clientId: string): Promise { // Utiliser le service chantier pour récupérer les chantiers par client const { chantierService } = await import('./api'); return await chantierService.getByClient(clientId); } /** * Valider les données d'un client */ validateClient(client: ClientFormData): string[] { const errors: string[] = []; if (!client.nom || client.nom.trim().length === 0) { errors.push('Le nom est obligatoire'); } if (!client.prenom || client.prenom.trim().length === 0) { errors.push('Le prénom est obligatoire'); } if (client.email && !this.isValidEmail(client.email)) { errors.push('L\'email n\'est pas valide'); } if (client.telephone && !this.isValidPhoneNumber(client.telephone)) { errors.push('Le numéro de téléphone n\'est pas valide'); } return errors; } /** * Valider une adresse email */ private isValidEmail(email: string): boolean { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } /** * Valider un numéro de téléphone */ private isValidPhoneNumber(phone: string): boolean { const phoneRegex = /^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$/; return phoneRegex.test(phone); } /** * Formater le nom complet d'un client */ formatFullName(client: Client): string { let fullName = `${client.prenom} ${client.nom}`; if (client.entreprise) { fullName += ` - ${client.entreprise}`; } return fullName; } /** * Exporter les clients au format CSV */ async exportToCsv(): Promise { const clients = await this.getAll(); const headers = [ 'ID', 'Prénom', 'Nom', 'Entreprise', 'Email', 'Téléphone', 'Adresse', 'Ville', 'Code Postal', 'Pays', 'Type', 'Actif' ]; const csvContent = [ headers.join(';'), ...clients.map(c => [ c.id || '', c.prenom || '', c.nom || '', (c as any).entreprise || '', c.email || '', c.telephone || '', c.adresse || '', (c as any).ville || '', (c as any).codePostal || '', (c as any).pays || '', (c as any).typeClient || '', c.actif ? 'Oui' : 'Non' ].join(';')) ].join('\n'); return new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); } } export default new ClientService();