import db from "../db" ; import { getProviderId } from "../routers/manga-page" ; import { ProviderId } from "../scrapers/types" ; import { Progress , ScraperResponse } from "../types" ; import getProgressData from "./getProgressData" ; export default async function getMangaProgress ( provider , slug , where = "last" ) { const dbString = `reading_new.${ getProviderId ( provider ) || provider }.${ slug }.${ where . replace ( /\./g , "_" ) }` ; const entry = db . get ( dbString ) ; let data = entry ?? null ; if ( data ) { data = getProgressData ( data ) ; } return data ; } export async function setMangaProgress ( manga ) { if ( manga . success ) { manga . progress = await getMangaProgress ( manga . provider , manga . constant . slug ) ; manga . realProgress = Object . assign ( { } , manga . progress ) ; if ( manga . progress && manga . progress . percentage > 90 ) { const { chapterId } = manga . progress ; const currentChapter = manga . data . chapters . find ( ( ch ) => ch . hrefString === chapterId ) ; const nextChapter = manga . data . chapters [ manga . data . chapters . indexOf ( currentChapter ) + 1 ] ?? null ; if ( nextChapter ) { const progressLast = manga . progress . at ; const chapterDateObj = new Date ( nextChapter . date ) ; chapterDateObj . setHours ( 23 ) ; chapterDateObj . setMinutes ( 59 ) ; const chapterDate = chapterDateObj . getTime ( ) ; manga . progress = { ... manga . progress , percentage : 0 , chapterId : nextChapter . hrefString , current : 0 , total : null , at : progressLast > chapterDate ? progressLast : Date . now ( ) , new : chapterDate > progressLast } ; } } } return manga ; }