fixed tracking
This commit is contained in:
@@ -35,21 +35,60 @@ export async function upsertListEntry(entry: any) {
|
||||
is_private
|
||||
} = entry;
|
||||
|
||||
let prev: any = null;
|
||||
|
||||
try {
|
||||
prev = await getSingleListEntry(user_id, entry_id, source, entry_type);
|
||||
} catch {
|
||||
prev = null; // ✅ si AniList da 404 u otro error → se trata como nuevo
|
||||
}
|
||||
|
||||
const isNew = !prev;
|
||||
|
||||
// ✅ NO permitir retroceso SOLO si ya existía
|
||||
if (!isNew && prev?.progress != null && progress < prev.progress) {
|
||||
return { changes: 0, ignored: true };
|
||||
}
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
|
||||
// ✅ NUNCA borrar start_date si ya existía
|
||||
if (prev?.start_date && !entry.start_date) {
|
||||
entry.start_date = prev.start_date;
|
||||
}
|
||||
|
||||
// ✅ START DATE solo al comenzar de verdad
|
||||
if (!prev?.start_date && progress === 1) {
|
||||
entry.start_date = today;
|
||||
}
|
||||
|
||||
// ✅ TOTAL solo si existe
|
||||
const total =
|
||||
prev?.total_episodes ??
|
||||
prev?.total_chapters ??
|
||||
null;
|
||||
|
||||
// ✅ COMPLETED automático
|
||||
if (total && progress >= total) {
|
||||
entry.status = 'COMPLETED';
|
||||
entry.end_date = today;
|
||||
}
|
||||
|
||||
if (source === 'anilist') {
|
||||
const token = await getActiveAccessToken(user_id);
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
const result = await aniListService.updateAniListEntry(token, {
|
||||
mediaId: entry_id,
|
||||
status,
|
||||
progress,
|
||||
score,
|
||||
start_date,
|
||||
end_date,
|
||||
repeat_count,
|
||||
notes,
|
||||
is_private
|
||||
mediaId: entry.entry_id,
|
||||
status: entry.status,
|
||||
progress: entry.progress,
|
||||
score: entry.score,
|
||||
start_date: entry.start_date,
|
||||
end_date: entry.end_date,
|
||||
repeat_count: entry.repeat_count,
|
||||
notes: entry.notes,
|
||||
is_private: entry.is_private
|
||||
});
|
||||
|
||||
|
||||
@@ -85,20 +124,21 @@ export async function upsertListEntry(entry: any) {
|
||||
`;
|
||||
|
||||
const params = [
|
||||
user_id,
|
||||
entry_id,
|
||||
source,
|
||||
entry_type,
|
||||
status,
|
||||
progress,
|
||||
score ?? null,
|
||||
start_date || null,
|
||||
end_date || null,
|
||||
repeat_count ?? 0,
|
||||
notes || null,
|
||||
is_private ?? 0
|
||||
entry.user_id,
|
||||
entry.entry_id,
|
||||
entry.source,
|
||||
entry.entry_type,
|
||||
entry.status,
|
||||
entry.progress,
|
||||
entry.score ?? null,
|
||||
entry.start_date || null,
|
||||
entry.end_date || null,
|
||||
entry.repeat_count ?? 0,
|
||||
entry.notes || null,
|
||||
entry.is_private ?? 0
|
||||
];
|
||||
|
||||
|
||||
try {
|
||||
const result = await run(sql, params, USER_DB);
|
||||
return { changes: result.changes, lastID: result.lastID, external: false };
|
||||
|
||||
Reference in New Issue
Block a user