Initial commit
This commit is contained in:
143
services/clientService.ts
Normal file
143
services/clientService.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { clientService } from './api';
|
||||
import { Client, ClientFormData } from '../types/btp';
|
||||
|
||||
class ClientService {
|
||||
private readonly basePath = '/api/clients';
|
||||
|
||||
/**
|
||||
* Récupérer tous les clients
|
||||
*/
|
||||
async getAll(): Promise<Client[]> {
|
||||
return await clientService.getAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupérer un client par ID
|
||||
*/
|
||||
async getById(id: string): Promise<Client> {
|
||||
return await clientService.getById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Créer un nouveau client
|
||||
*/
|
||||
async create(client: ClientFormData): Promise<Client> {
|
||||
return await clientService.create(client as Partial<Client>);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifier un client existant
|
||||
*/
|
||||
async update(id: string, client: ClientFormData): Promise<Client> {
|
||||
return await clientService.update(id, client as Partial<Client>);
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprimer un client
|
||||
*/
|
||||
async delete(id: string): Promise<void> {
|
||||
await clientService.delete(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rechercher des clients
|
||||
*/
|
||||
async search(query: string): Promise<Client[]> {
|
||||
return await clientService.searchByNom(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupérer les chantiers d'un client
|
||||
*/
|
||||
async getChantiers(clientId: string): Promise<any[]> {
|
||||
// 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<Blob> {
|
||||
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.entreprise || '',
|
||||
c.email || '',
|
||||
c.telephone || '',
|
||||
c.adresse || '',
|
||||
c.ville || '',
|
||||
c.codePostal || '',
|
||||
c.pays || '',
|
||||
c.typeClient || '',
|
||||
c.actif ? 'Oui' : 'Non'
|
||||
].join(';'))
|
||||
].join('\n');
|
||||
|
||||
return new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
||||
}
|
||||
}
|
||||
|
||||
export default new ClientService();
|
||||
Reference in New Issue
Block a user