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.
This commit is contained in:
2026-04-21 15:00:28 -04:00
parent 946b81384c
commit c38117fbe7
+29 -1
View File
@@ -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,
},