From c38117fbe79b11e1393582499aba066798a9bc17 Mon Sep 17 00:00:00 2001 From: hobokenchicken Date: Tue, 21 Apr 2026 15:00:28 -0400 Subject: [PATCH] fix(backfill): map Plex username to Tautulli user ID before fetching history Tautulli get_history requires internal user_id, not Plex ID. Now queries get_users first, matches by username/email/friendly_name, then uses matched user's ID for history fetch. --- backend/src/routes/auth.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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, },