fix watch history normalization

This commit is contained in:
Pas 2026-01-16 19:45:58 -07:00
parent a5cdb2f137
commit 84b907c012
2 changed files with 31 additions and 58 deletions

View file

@ -59,11 +59,11 @@ export default defineEventHandler(async event => {
return items.map(item => ({ return items.map(item => ({
tmdbId: item.tmdb_id, tmdbId: item.tmdb_id,
episode: { episode: {
id: item.episode_id || undefined, id: item.episode_id === '\n' ? undefined : item.episode_id || undefined,
number: item.episode_number || undefined, number: item.episode_number || undefined,
}, },
season: { season: {
id: item.season_id || undefined, id: item.season_id === '\n' ? undefined : item.season_id || undefined,
number: item.season_number || undefined, number: item.season_number || undefined,
}, },
meta: item.meta, meta: item.meta,

View file

@ -54,30 +54,23 @@ export default defineEventHandler(async event => {
if (method === 'PUT') { if (method === 'PUT') {
try { try {
const body = await readBody(event); const body = await readBody(event);
console.error('Watch history PUT body:', JSON.stringify(body, null, 2));
let validatedBody; const validatedBody = watchHistoryItemSchema.parse(body);
try {
validatedBody = watchHistoryItemSchema.parse(body);
console.error('Validation successful');
} catch (validationError) {
console.error('Validation error:', validationError);
throw createError({
statusCode: 400,
message: `Validation error: ${validationError.message}`,
});
}
const watchedAt = defaultAndCoerceDateTime(validatedBody.watchedAt); const watchedAt = defaultAndCoerceDateTime(validatedBody.watchedAt);
const now = new Date(); const now = new Date();
// Normalize IDs for movies (use '\n' instead of null to satisfy unique constraint)
const normSeasonId = validatedBody.meta.type === 'movie' ? '\n' : validatedBody.seasonId || null;
const normEpisodeId = validatedBody.meta.type === 'movie' ? '\n' : validatedBody.episodeId || null;
const existingItem = await prisma.watch_history.findUnique({ const existingItem = await prisma.watch_history.findUnique({
where: { where: {
tmdb_id_user_id_season_id_episode_id: { tmdb_id_user_id_season_id_episode_id: {
tmdb_id: tmdbId, tmdb_id: tmdbId,
user_id: userId, user_id: userId,
season_id: validatedBody.seasonId || null, season_id: normSeasonId,
episode_id: validatedBody.episodeId || null, episode_id: normEpisodeId,
}, },
}, },
}); });
@ -93,31 +86,27 @@ export default defineEventHandler(async event => {
updated_at: now, updated_at: now,
}; };
try { if (existingItem) {
if (existingItem) { watchHistoryItem = await prisma.watch_history.update({
console.error('Updating existing watch history item'); where: {
watchHistoryItem = await prisma.watch_history.update({ id: existingItem.id,
where: { },
id: existingItem.id, data,
}, });
data, } else {
}); watchHistoryItem = await prisma.watch_history.create({
} else { data: {
console.error('Creating new watch history item'); id: randomUUID(),
watchHistoryItem = await prisma.watch_history.create({ tmdb_id: tmdbId,
data: { user_id: userId,
id: randomUUID(), season_id: normSeasonId,
tmdb_id: tmdbId, episode_id: normEpisodeId,
user_id: userId, season_number: validatedBody.seasonNumber || null,
season_id: validatedBody.seasonId || null, episode_number: validatedBody.episodeNumber || null,
episode_id: validatedBody.episodeId || null, ...data,
season_number: validatedBody.seasonNumber || null, },
episode_number: validatedBody.episodeNumber || null, });
...data, }
},
});
}
console.error('Database operation successful');
return { return {
success: true, success: true,
@ -137,27 +126,11 @@ export default defineEventHandler(async event => {
}; };
} catch (dbError) { } catch (dbError) {
console.error('Database error:', dbError); console.error('Database error:', dbError);
console.error('Database error details:', {
message: dbError.message,
code: dbError.code,
meta: dbError.meta,
name: dbError.constructor.name
});
throw createError({ throw createError({
statusCode: 500, statusCode: 500,
message: `Database error: ${dbError.message}`, message: 'Failed to save watch history',
}); });
} }
} catch (error) {
console.error('Error in watch history PUT:', error);
if (error.statusCode) {
throw error; // Re-throw createError instances
}
throw createError({
statusCode: 500,
message: 'Failed to save watch history',
});
}
} }
if (method === 'DELETE') { if (method === 'DELETE') {