Initial commit
This commit is contained in:
236
services/notificationService.ts
Normal file
236
services/notificationService.ts
Normal file
@@ -0,0 +1,236 @@
|
||||
import { apiService } from './api';
|
||||
|
||||
export interface Notification {
|
||||
id: string;
|
||||
type: 'info' | 'warning' | 'success' | 'error';
|
||||
titre: string;
|
||||
message: string;
|
||||
date: Date;
|
||||
lu: boolean;
|
||||
userId?: string;
|
||||
metadata?: {
|
||||
chantierId?: string;
|
||||
chantierNom?: string;
|
||||
clientId?: string;
|
||||
clientNom?: string;
|
||||
action?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface NotificationStats {
|
||||
total: number;
|
||||
nonLues: number;
|
||||
parType: Record<string, number>;
|
||||
tendance: {
|
||||
periode: string;
|
||||
nombre: number;
|
||||
}[];
|
||||
}
|
||||
|
||||
class NotificationService {
|
||||
/**
|
||||
* Récupérer toutes les notifications
|
||||
*/
|
||||
async getNotifications(): Promise<Notification[]> {
|
||||
try {
|
||||
const response = await apiService.api.get('/notifications');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des notifications:', error);
|
||||
return this.getMockNotifications();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupérer les notifications non lues
|
||||
*/
|
||||
async getUnreadNotifications(): Promise<Notification[]> {
|
||||
try {
|
||||
const response = await apiService.api.get('/notifications/unread');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des notifications non lues:', error);
|
||||
return this.getMockNotifications().filter(n => !n.lu);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marquer une notification comme lue
|
||||
*/
|
||||
async markAsRead(notificationId: string): Promise<void> {
|
||||
try {
|
||||
await apiService.api.put(`/notifications/${notificationId}/read`);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du marquage comme lu:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Marquer toutes les notifications comme lues
|
||||
*/
|
||||
async markAllAsRead(): Promise<void> {
|
||||
try {
|
||||
await apiService.api.put('/notifications/read-all');
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du marquage global:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Créer une nouvelle notification
|
||||
*/
|
||||
async createNotification(notification: Omit<Notification, 'id' | 'date'>): Promise<Notification> {
|
||||
try {
|
||||
const response = await apiService.api.post('/notifications', {
|
||||
...notification,
|
||||
date: new Date().toISOString()
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création de notification:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Supprimer une notification
|
||||
*/
|
||||
async deleteNotification(notificationId: string): Promise<void> {
|
||||
try {
|
||||
await apiService.api.delete(`/notifications/${notificationId}`);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupérer les statistiques des notifications
|
||||
*/
|
||||
async getNotificationStats(): Promise<NotificationStats> {
|
||||
try {
|
||||
const response = await apiService.api.get('/notifications/stats');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des statistiques:', error);
|
||||
return this.getMockNotificationStats();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Diffuser une notification à plusieurs utilisateurs
|
||||
*/
|
||||
async broadcastNotification(notification: {
|
||||
type: 'info' | 'warning' | 'success' | 'error';
|
||||
titre: string;
|
||||
message: string;
|
||||
userIds?: string[];
|
||||
roles?: string[];
|
||||
}): Promise<void> {
|
||||
try {
|
||||
await apiService.api.post('/notifications/broadcast', notification);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la diffusion:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifications mockées
|
||||
*/
|
||||
private getMockNotifications(): Notification[] {
|
||||
return [
|
||||
{
|
||||
id: '1',
|
||||
type: 'info',
|
||||
titre: 'Nouveau devis disponible',
|
||||
message: 'Le devis pour votre extension cuisine est maintenant disponible',
|
||||
date: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
|
||||
lu: false,
|
||||
metadata: {
|
||||
chantierId: 'chantier-1',
|
||||
chantierNom: 'Extension cuisine',
|
||||
action: 'devis_cree'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
type: 'warning',
|
||||
titre: 'Rendez-vous prévu',
|
||||
message: 'Rendez-vous avec votre gestionnaire demain à 14h00',
|
||||
date: new Date(Date.now() - 24 * 60 * 60 * 1000),
|
||||
lu: false,
|
||||
metadata: {
|
||||
action: 'rendez_vous_planifie'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
type: 'success',
|
||||
titre: 'Phase terminée',
|
||||
message: 'La phase "Gros œuvre" de votre chantier a été terminée',
|
||||
date: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
|
||||
lu: true,
|
||||
metadata: {
|
||||
chantierId: 'chantier-2',
|
||||
chantierNom: 'Rénovation appartement',
|
||||
action: 'phase_terminee'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '4',
|
||||
type: 'error',
|
||||
titre: 'Retard détecté',
|
||||
message: 'Le chantier "Villa moderne" accuse un retard de 3 jours',
|
||||
date: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000),
|
||||
lu: true,
|
||||
metadata: {
|
||||
chantierId: 'chantier-3',
|
||||
chantierNom: 'Villa moderne',
|
||||
action: 'retard_detecte'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: '5',
|
||||
type: 'info',
|
||||
titre: 'Nouveau client attribué',
|
||||
message: 'Vous avez été désigné gestionnaire pour M. Durand',
|
||||
date: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000),
|
||||
lu: true,
|
||||
metadata: {
|
||||
clientId: 'client-4',
|
||||
clientNom: 'M. Durand',
|
||||
action: 'client_attribue'
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Statistiques mockées
|
||||
*/
|
||||
private getMockNotificationStats(): NotificationStats {
|
||||
const notifications = this.getMockNotifications();
|
||||
return {
|
||||
total: notifications.length,
|
||||
nonLues: notifications.filter(n => !n.lu).length,
|
||||
parType: {
|
||||
info: notifications.filter(n => n.type === 'info').length,
|
||||
warning: notifications.filter(n => n.type === 'warning').length,
|
||||
success: notifications.filter(n => n.type === 'success').length,
|
||||
error: notifications.filter(n => n.type === 'error').length
|
||||
},
|
||||
tendance: [
|
||||
{ periode: 'Lundi', nombre: 3 },
|
||||
{ periode: 'Mardi', nombre: 7 },
|
||||
{ periode: 'Mercredi', nombre: 2 },
|
||||
{ periode: 'Jeudi', nombre: 5 },
|
||||
{ periode: 'Vendredi', nombre: 4 },
|
||||
{ periode: 'Samedi', nombre: 1 },
|
||||
{ periode: 'Dimanche', nombre: 0 }
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default new NotificationService();
|
||||
Reference in New Issue
Block a user