fix(ai-query): two-step query for recently-watched endpoints to avoid genre join row multiplication

This commit is contained in:
X4Lo committed 2026-05-29 02:57:28 +01:00
1 parent 0459a1b4bf
commit 702cd537d1
1 file changed
+54 -10
+54 -10
View File
@@ -49,7 +49,28 @@ class AiQueryController {
getRecentlyWatchedMovies = asyncWrapper(async (req, res) => { getRecentlyWatchedMovies = asyncWrapper(async (req, res) => {
const limit = Math.min(parseInt(req.query.limit) || 10, 50); const limit = Math.min(parseInt(req.query.limit) || 10, 50);
// Step 1: Get the last N EntrySeen IDs for movies (simple query, no genre joins)
const seenIds = await EntrySeen.findAll({
attributes: ['id'],
include: [{
model: Entry,
as: 'entry',
attributes: [],
where: { is_tvseries: false, hidden: false }
}],
order: [['date_seen', 'DESC'], ['id', 'DESC']],
limit
});
if (seenIds.length === 0) {
return res.status(200).json([]);
}
const idList = seenIds.map(s => s.id);
// Step 2: Fetch full data for those IDs with all joins
const entries = await EntrySeen.findAll({ const entries = await EntrySeen.findAll({
where: { id: idList },
include: [{ include: [{
model: Entry, model: Entry,
as: 'entry', as: 'entry',
@@ -59,13 +80,7 @@ class AiQueryController {
], ],
attributes: ['id', 'title', 'is_tvseries', 'poster_path', 'banner_path', 'runtime', 'release_date', 'favorit', 'watchlist'] attributes: ['id', 'title', 'is_tvseries', 'poster_path', 'banner_path', 'runtime', 'release_date', 'favorit', 'watchlist']
}], }],
where: { order: [['date_seen', 'DESC'], ['id', 'DESC']]
'$entry.is_tvseries$': false,
'$entry.hidden$': false
},
order: [['date_seen', 'DESC'], ['id', 'DESC']],
limit,
subQuery: false
}); });
const result = entries.map(es => ({ const result = entries.map(es => ({
@@ -94,7 +109,38 @@ class AiQueryController {
getRecentlyWatchedEpisodes = asyncWrapper(async (req, res) => { getRecentlyWatchedEpisodes = asyncWrapper(async (req, res) => {
const limit = Math.min(parseInt(req.query.limit) || 10, 50); const limit = Math.min(parseInt(req.query.limit) || 10, 50);
// Step 1: Get the last N EpisodeSeen IDs (simple query, no nested joins)
const seenIds = await EpisodeSeen.findAll({
attributes: ['id'],
include: [{
model: Episode,
as: 'episode',
attributes: [],
include: [{
model: Season,
as: 'season',
attributes: [],
include: [{
model: Entry,
as: 'entry',
attributes: [],
where: { hidden: false }
}]
}]
}],
order: [['date_seen', 'DESC'], ['id', 'DESC']],
limit
});
if (seenIds.length === 0) {
return res.status(200).json([]);
}
const idList = seenIds.map(s => s.id);
// Step 2: Fetch full data for those IDs with all joins
const records = await EpisodeSeen.findAll({ const records = await EpisodeSeen.findAll({
where: { id: idList },
include: [{ include: [{
model: Episode, model: Episode,
as: 'episode', as: 'episode',
@@ -111,9 +157,7 @@ class AiQueryController {
}] }]
}] }]
}], }],
order: [['date_seen', 'DESC'], ['id', 'DESC']], order: [['date_seen', 'DESC'], ['id', 'DESC']]
limit,
subQuery: false
}); });
const result = records.map(es => ({ const result = records.map(es => ({