Files
btpxpress-frontend/services/chantierActionsService.ts
2025-10-13 05:29:32 +02:00

303 lines
8.3 KiB
TypeScript

/**
* Service pour les actions sur les chantiers
*/
import { apiClient } from './api-client';
export interface ChantierActionResult {
success: boolean;
message: string;
data?: any;
pdfUrl?: string;
amendmentId?: string;
}
class ChantierActionsService {
private readonly basePath = '/chantiers';
/**
* Suspendre ou reprendre un chantier
*/
async toggleSuspend(chantierId: string, suspend: boolean): Promise<ChantierActionResult> {
try {
const action = suspend ? 'suspend' : 'resume';
const response = await apiClient.put(`${this.basePath}/${chantierId}/${action}`);
return {
success: true,
message: suspend ? 'Chantier suspendu avec succès' : 'Chantier repris avec succès',
data: response.data
};
} catch (error) {
return {
success: false,
message: 'Erreur lors de la modification du statut',
data: error
};
}
}
/**
* Clôturer un chantier
*/
async closeChantier(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.put(`${this.basePath}/${chantierId}/close`, {
dateFinReelle: new Date().toISOString(),
statut: 'TERMINE'
});
return {
success: true,
message: 'Chantier clôturé avec succès',
data: response.data
};
} catch (error) {
return {
success: false,
message: 'Erreur lors de la clôture du chantier',
data: error
};
}
}
/**
* Archiver un chantier
*/
async archiveChantier(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.put(`${this.basePath}/${chantierId}/archive`, {
actif: false,
dateArchivage: new Date().toISOString()
});
return {
success: true,
message: 'Chantier archivé avec succès',
data: response.data
};
} catch (error) {
return {
success: false,
message: 'Erreur lors de l\'archivage du chantier',
data: error
};
}
}
/**
* Générer un rapport pour un chantier
*/
async generateReport(chantierId: string, format: 'pdf' | 'excel' = 'pdf'): Promise<ChantierActionResult> {
try {
const response = await apiClient.get(`${this.basePath}/${chantierId}/report`, {
params: { format },
responseType: 'blob'
});
// Créer un lien de téléchargement
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `rapport-chantier-${chantierId}.${format}`);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
return {
success: true,
message: 'Rapport généré avec succès',
data: response.data
};
} catch (error) {
return {
success: false,
message: 'Erreur lors de la génération du rapport',
data: error
};
}
}
/**
* Exporter les données d'un chantier
*/
async exportChantier(chantierId: string, format: 'pdf' | 'excel'): Promise<ChantierActionResult> {
try {
const response = await apiClient.get(`${this.basePath}/${chantierId}/export`, {
params: { format },
responseType: 'blob'
});
// Créer un lien de téléchargement
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', `export-chantier-${chantierId}.${format === 'excel' ? 'xlsx' : format}`);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
return {
success: true,
message: `Export ${format.toUpperCase()} réussi`,
data: response.data
};
} catch (error) {
return {
success: false,
message: `Erreur lors de l'export ${format.toUpperCase()}`,
data: error
};
}
}
/**
* Obtenir un résumé rapide d'un chantier
*/
async getQuickSummary(chantierId: string): Promise<any> {
try {
const response = await apiClient.get(`${this.basePath}/${chantierId}/summary`);
return response.data;
} catch (error) {
console.error('Erreur lors de la récupération du résumé:', error);
throw error;
}
}
/**
* Obtenir les statistiques d'un chantier
*/
async getChantierStats(chantierId: string): Promise<any> {
try {
const response = await apiClient.get(`${this.basePath}/${chantierId}/stats`);
return response.data;
} catch (error) {
console.error('Erreur lors de la récupération des statistiques:', error);
throw error;
}
}
/**
* Obtenir les alertes d'un chantier
*/
async getChantierAlerts(chantierId: string): Promise<any> {
try {
const response = await apiClient.get(`${this.basePath}/${chantierId}/alerts`);
return response.data;
} catch (error) {
console.error('Erreur lors de la récupération des alertes:', error);
throw error;
}
}
// === NOUVELLES ACTIONS PRIORITAIRES BTP ===
/**
* Suspendre temporairement un chantier
*/
async suspendChantier(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.put(`${this.basePath}/${chantierId}/suspend`, {
datesSuspension: new Date().toISOString(),
motif: 'Suspension temporaire',
statut: 'SUSPENDU'
});
return {
success: true,
message: 'Chantier suspendu temporairement',
data: response.data
};
} catch (error) {
console.error('Erreur lors de la suspension:', error);
throw error;
}
}
/**
* Clôturer définitivement un chantier avec rapport final
*/
async closeChantierDefinitively(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.put(`${this.basePath}/${chantierId}/close-definitively`, {
dateFinReelle: new Date().toISOString(),
statut: 'TERMINE',
generateFinalReport: true
});
return {
success: true,
message: 'Chantier clôturé définitivement',
data: response.data
};
} catch (error) {
console.error('Erreur lors de la clôture:', error);
throw error;
}
}
/**
* Envoyer notification au client
*/
async notifyClient(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.post(`${this.basePath}/${chantierId}/notify-client`, {
type: 'progress_update',
includePhotos: true,
includeProgress: true,
timestamp: new Date().toISOString()
});
return {
success: true,
message: 'Notification envoyée au client',
data: response.data
};
} catch (error) {
console.error('Erreur lors de la notification client:', error);
throw error;
}
}
/**
* Générer facture intermédiaire
*/
async generateIntermediateInvoice(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.post(`${this.basePath}/${chantierId}/invoice/intermediate`, {
dateGeneration: new Date().toISOString(),
type: 'INTERMEDIAIRE',
basedOnProgress: true
});
return {
success: true,
message: 'Facture intermédiaire générée',
data: response.data,
pdfUrl: response.data.pdfUrl
};
} catch (error) {
console.error('Erreur lors de la génération de facture:', error);
throw error;
}
}
/**
* Créer un avenant budgétaire
*/
async createAmendment(chantierId: string): Promise<ChantierActionResult> {
try {
const response = await apiClient.post(`${this.basePath}/${chantierId}/amendment`, {
dateCreation: new Date().toISOString(),
type: 'BUDGETAIRE',
status: 'DRAFT'
});
return {
success: true,
message: 'Avenant budgétaire créé',
data: response.data,
amendmentId: response.data.id
};
} catch (error) {
console.error('Erreur lors de la création d\'avenant:', error);
throw error;
}
}
}
export const chantierActionsService = new ChantierActionsService();