Skip to content

Commit

Permalink
fix: check for out-of-sync chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
oae committed Oct 27, 2022
1 parent a7323d8 commit caf6720
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions src/server/queue/checkChapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,39 @@ const checkChapters = async (manga: MangaWithLibraryAndMetadata) => {
const mangaDir = path.resolve(manga.library.path, sanitizer(manga.title));
const missingChapterFiles = await findMissingChapterFiles(mangaDir, manga.source, manga.title);

if (missingChapterFiles.length === 0) {
logger.info(`There are no missing chapter files for ${manga.title}`);
const localChapters = await getChaptersFromLocal(mangaDir);
const dbChapters = await prisma.chapter.findMany({
where: {
mangaId: manga.id,
},
});

const localChapters = await getChaptersFromLocal(mangaDir);
const dbChapters = await prisma.chapter.findMany({
where: {
mangaId: manga.id,
},
});

const dbOnlyChapters = dbChapters.filter(
(dbChapter) => localChapters.findIndex((localChapter) => localChapter.index === dbChapter.index) < 0,
);

await Promise.all(
dbOnlyChapters.map(async (chapter) => {
await prisma.chapter.delete({
where: {
id: chapter.id,
},
});
}),
);

const missingDbChapters = localChapters.filter(
(localChapter) => dbChapters.findIndex((dbChapter) => dbChapter.index === localChapter.index) < 0,
);

await Promise.all(
missingDbChapters.map(async (chapter) => {
return prisma.chapter.create({
data: {
...chapter,
mangaId: manga.id,
},
});
const dbOnlyChapters = dbChapters.filter(
(dbChapter) => localChapters.findIndex((localChapter) => localChapter.index === dbChapter.index) < 0,
);

const missingDbChapters = localChapters.filter(
(localChapter) => dbChapters.findIndex((dbChapter) => dbChapter.index === localChapter.index) < 0,
);

await prisma.$transaction([
...dbOnlyChapters.map((chapter) =>
prisma.chapter.delete({
where: {
id: chapter.id,
},
}),
);
),
prisma.chapter.createMany({
data: missingDbChapters.map((chapter) => ({
...chapter,
mangaId: manga.id,
})),
}),
]);

if (missingChapterFiles.length === 0) {
logger.info(`There are no missing chapter files for ${manga.title}`);
return;
}

Expand Down

0 comments on commit caf6720

Please sign in to comment.