feat: manual backfill for admin
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import axios from "axios";
|
||||
import { prisma } from "../utils/prisma";
|
||||
|
||||
const TAUTULLI_URL = process.env.TAUTULLI_URL || "";
|
||||
const TAUTULLI_API_KEY = process.env.TAUTULLI_API_KEY || "";
|
||||
|
||||
export async function backfillUserHistory(user: {
|
||||
id: string;
|
||||
plexId: string;
|
||||
plexUsername: string;
|
||||
}) {
|
||||
if (!TAUTULLI_URL || !TAUTULLI_API_KEY) return;
|
||||
try {
|
||||
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) return;
|
||||
|
||||
const startDate = (() => {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() - 30);
|
||||
return d.toISOString().split("T")[0];
|
||||
})();
|
||||
const historyResponse = await axios.get(`${TAUTULLI_URL}/api/v2`, {
|
||||
params: {
|
||||
apikey: TAUTULLI_API_KEY,
|
||||
cmd: "get_history",
|
||||
user_id: tautulliUser.user_id,
|
||||
start_date: startDate,
|
||||
length: 1000,
|
||||
},
|
||||
});
|
||||
const historyData = historyResponse.data?.response?.data?.data || [];
|
||||
if (!Array.isArray(historyData) || historyData.length === 0) return;
|
||||
|
||||
const settings = await prisma.systemSettings.findFirst();
|
||||
const creditsPerMinute = settings?.creditsPerMinute || 2;
|
||||
const minWatchPercent = settings?.minWatchPercent || 80;
|
||||
const minWatchMinutes = settings?.minWatchMinutes || 5;
|
||||
|
||||
let totalCredits = 0;
|
||||
let totalMinutes = 0;
|
||||
for (const item of historyData) {
|
||||
const sessionId = item.reference_id?.toString() || item.id?.toString();
|
||||
if (!sessionId) continue;
|
||||
if (await prisma.watchEvent.findUnique({ where: { sessionId } }))
|
||||
continue;
|
||||
const percentComplete = item.percent_complete || 0;
|
||||
const watchDurationMinutes = Math.floor(
|
||||
(item.stopped - item.started) / 60,
|
||||
);
|
||||
if (
|
||||
percentComplete < minWatchPercent ||
|
||||
watchDurationMinutes < minWatchMinutes
|
||||
)
|
||||
continue;
|
||||
const creditsEarned = watchDurationMinutes * creditsPerMinute;
|
||||
const watchEvent = await prisma.watchEvent.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
sessionId,
|
||||
ratingKey: item.rating_key?.toString() || "",
|
||||
contentType: item.media_type || "movie",
|
||||
title: item.title || item.full_title || "Unknown",
|
||||
grandparentTitle: item.grandparent_title || null,
|
||||
duration: item.stopped - item.started,
|
||||
percentComplete: Math.floor(percentComplete),
|
||||
creditsEarned,
|
||||
isProcessed: true,
|
||||
watchedAt: new Date(item.stopped * 1000),
|
||||
},
|
||||
});
|
||||
await prisma.transaction.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
type: "EARN",
|
||||
amount: creditsEarned,
|
||||
watchEventId: watchEvent.id,
|
||||
description: `Watched ${item.title || item.full_title || "Unknown"}`,
|
||||
contentTitle: item.title || item.full_title || "Unknown",
|
||||
},
|
||||
});
|
||||
totalCredits += creditsEarned;
|
||||
totalMinutes += watchDurationMinutes;
|
||||
}
|
||||
if (totalCredits > 0)
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
totalEarned: { increment: totalCredits },
|
||||
watchTimeMinutes: { increment: totalMinutes },
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to backfill history for ${user.plexUsername}:`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user