From 7c01525bac533d3e53ed44bcf11d2d14d956210e Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 22:02:57 +0100 Subject: [PATCH] Fondation --- app/api/calendars/route.ts | 2 +- app/api/courrier/unread-counts/route.ts | 42 ++++++++++++------- app/api/notifications/update/route.ts | 2 +- app/api/rocket-chat/messages/route.ts | 2 +- app/api/rocket-chat/user-token/route.ts | 2 +- .../notifications/notification-registry.ts | 26 +++++++----- 6 files changed, 47 insertions(+), 29 deletions(-) diff --git a/app/api/calendars/route.ts b/app/api/calendars/route.ts index 7a73c11..52dcff0 100644 --- a/app/api/calendars/route.ts +++ b/app/api/calendars/route.ts @@ -117,7 +117,7 @@ export async function GET(req: NextRequest) { // Exclude "Privée"/"Default" calendars that are not actively synced if (isPrivateOrDefault && !hasActiveSync) { logger.debug('[CALENDAR] Filtering out calendar without active sync', { - calendarId: cal.id, + calendarIdHash: Buffer.from(cal.id).toString('base64').slice(0, 12), calendarName: cal.name, syncEnabled: cal.syncConfig?.syncEnabled, hasMailCredential: !!cal.syncConfig?.mailCredential, diff --git a/app/api/courrier/unread-counts/route.ts b/app/api/courrier/unread-counts/route.ts index 882381e..0199ba2 100644 --- a/app/api/courrier/unread-counts/route.ts +++ b/app/api/courrier/unread-counts/route.ts @@ -37,7 +37,9 @@ export async function GET(request: Request) { const cachedCounts = await redis.get(UNREAD_COUNTS_CACHE_KEY(userId)); if (cachedCounts) { // Use cached results if available - logger.debug('[UNREAD_API] Using cached unread counts', { userId }); + logger.debug('[UNREAD_API] Using cached unread counts', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); // If the cache is about to expire, schedule a background refresh const ttl = await redis.ttl(UNREAD_COUNTS_CACHE_KEY(userId)); @@ -52,12 +54,14 @@ export async function GET(request: Request) { ); if (lockAcquired) { - logger.debug('[UNREAD_API] Scheduling background refresh', { userId }); + logger.debug('[UNREAD_API] Scheduling background refresh', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); // Use Promise to run in background setTimeout(() => { refreshUnreadCounts(userId, redis) .catch(err => logger.error('[UNREAD_API] Background refresh error', { - userId, + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), error: err instanceof Error ? err.message : String(err) })) .finally(() => { @@ -71,7 +75,9 @@ export async function GET(request: Request) { return NextResponse.json(JSON.parse(cachedCounts)); } - logger.debug('[UNREAD_API] Cache miss, fetching unread counts', { userId }); + logger.debug('[UNREAD_API] Cache miss, fetching unread counts', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); // Try to acquire lock to prevent parallel refreshes const lockAcquired = await redis.set( @@ -83,7 +89,9 @@ export async function GET(request: Request) { ); if (!lockAcquired) { - logger.debug('[UNREAD_API] Another process is refreshing unread counts', { userId }); + logger.debug('[UNREAD_API] Another process is refreshing unread counts', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); // Return empty counts with short cache time if we can't acquire lock // The next request will likely get cached data @@ -123,7 +131,9 @@ export async function GET(request: Request) { */ async function refreshUnreadCounts(userId: string, redis: any): Promise { try { - logger.debug('[UNREAD_API] Background refresh started', { userId }); + logger.debug('[UNREAD_API] Background refresh started', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); const unreadCounts = await fetchUnreadCounts(userId); // Save to cache @@ -134,10 +144,12 @@ async function refreshUnreadCounts(userId: string, redis: any): Promise { UNREAD_COUNTS_CACHE_TTL ); - logger.debug('[UNREAD_API] Background refresh completed', { userId }); + logger.debug('[UNREAD_API] Background refresh completed', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); } catch (error) { logger.error('[UNREAD_API] Background refresh failed', { - userId, + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), error: error instanceof Error ? error.message : String(error) }); throw error; @@ -157,7 +169,10 @@ async function fetchUnreadCounts(userId: string): Promise b.timestamp.getTime() - a.timestamp.getTime()); logger.debug('[NOTIFICATION_REGISTRY] Notifications retrieved', { - userId, + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), total: allItems.length, limit, returned: Math.min(allItems.length, limit), @@ -274,10 +276,12 @@ export class NotificationRegistry { await redis.del(itemsKey); } - logger.debug('[NOTIFICATION_REGISTRY] Cache invalidated', { userId }); + logger.debug('[NOTIFICATION_REGISTRY] Cache invalidated', { + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), + }); } catch (error) { logger.error('[NOTIFICATION_REGISTRY] Error invalidating cache', { - userId, + userIdHash: Buffer.from(userId).toString('base64').slice(0, 12), error: error instanceof Error ? error.message : String(error), }); }