diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 298ffd3..32efd28 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -266,6 +266,34 @@ async function backfillUserHistory(user: { ); } + // Map Plex username to Tautulli user ID + const usersResponse = await axios.get(`${TAUTULLI_URL}/api/v2`, { + params: { + apikey: TAUTULLI_API_KEY, + cmd: "get_users", + }, + }); + + const tautulliUsers = usersResponse.data?.response?.data || []; + const tautulliUser = tautulliUsers.find( + (u: any) => + u.username?.toLowerCase() === user.plexUsername.toLowerCase() || + u.email?.toLowerCase() === user.plexUsername.toLowerCase() || + u.friendly_name?.toLowerCase() === user.plexUsername.toLowerCase(), + ); + + if (!tautulliUser) { + console.log( + `Tautulli user not found for Plex username ${user.plexUsername}, skipping backfill`, + ); + return; + } + + const tautulliUserId = tautulliUser.user_id; + console.log( + `Mapped ${user.plexUsername} to Tautulli user ID ${tautulliUserId}`, + ); + // Calculate 30 days ago const thirtyDaysAgo = new Date(); thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30); @@ -276,7 +304,7 @@ async function backfillUserHistory(user: { params: { apikey: TAUTULLI_API_KEY, cmd: "get_history", - user_id: user.plexId, + user_id: tautulliUserId, start_date: startDate, length: 1000, },