PROBLÈME RÉSOLU: - Erreur "Code already used" répétée dans les logs Keycloak - Boucle infinie de tentatives d'échange du code d'autorisation OAuth - Utilisateurs bloqués à la connexion CORRECTIONS APPLIQUÉES: 1. Ajout de useRef pour protéger contre les exécutions multiples - hasExchanged.current: Flag pour prévenir les réexécutions - isProcessing.current: Protection pendant le traitement 2. Modification des dépendances useEffect - AVANT: [searchParams, router] → exécution à chaque changement - APRÈS: [] → exécution unique au montage du composant 3. Amélioration du logging - Console logs pour debug OAuth flow - Messages emoji pour faciliter le suivi 4. Nettoyage de l'URL - window.history.replaceState() pour retirer les paramètres OAuth - Évite les re-renders causés par les paramètres dans l'URL 5. Gestion d'erreurs améliorée - Capture des erreurs JSON du serveur - Messages d'erreur plus explicites FICHIERS AJOUTÉS: - app/(main)/aide/* - 4 pages du module Aide (documentation, tutoriels, support) - app/(main)/messages/* - 4 pages du module Messages (inbox, envoyés, archives) - app/auth/callback/page.tsx.backup - Sauvegarde avant modification IMPACT: ✅ Un seul échange de code par authentification ✅ Plus d'erreur "Code already used" ✅ Connexion fluide et sans boucle ✅ Logs propres et lisibles 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
236 lines
7.5 KiB
TypeScript
236 lines
7.5 KiB
TypeScript
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.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.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.put(`/notifications/${notificationId}/read`);
|
|
} catch (error) {
|
|
console.error('Erreur lors du marquage de la notification comme lue:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Marquer toutes les notifications comme lues
|
|
*/
|
|
async markAllAsRead(): Promise<void> {
|
|
try {
|
|
await apiService.put('/notifications/mark-all-read');
|
|
} catch (error) {
|
|
console.error('Erreur lors du marquage de toutes les notifications comme lues:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Créer une nouvelle notification
|
|
*/
|
|
async createNotification(notification: Omit<Notification, 'id' | 'date'>): Promise<Notification> {
|
|
try {
|
|
const response = await apiService.post('/notifications', notification);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Erreur lors de la création de la notification:', error);
|
|
return {
|
|
...notification,
|
|
id: Math.random().toString(36).substring(2, 11),
|
|
date: new Date(),
|
|
lu: false
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Supprimer une notification
|
|
*/
|
|
async deleteNotification(notificationId: string): Promise<void> {
|
|
try {
|
|
await apiService.delete(`/notifications/${notificationId}`);
|
|
} catch (error) {
|
|
console.error('Erreur lors de la suppression de la notification:', error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Récupérer les statistiques des notifications
|
|
*/
|
|
async getNotificationStats(): Promise<NotificationStats> {
|
|
try {
|
|
const response = await apiService.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.post('/notifications/broadcast', notification);
|
|
} catch (error) {
|
|
console.error('Erreur lors de la diffusion de la notification:', 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(); |