UNPKG

@accounter/israeli-vat-scraper

Version:
61 lines (60 loc) 2.48 kB
import { newPageByYear } from '../utils/browser-util.js'; import { getReportsTable } from '../utils/evaluation-functions.js'; import { waitForSelectorPlus } from '../utils/page-util.js'; import { MonthHandler } from './month-handler.js'; export class YearHandler { config; prompt; location; months = null; page = null; constructor(config, prompt, location, months = null) { this.config = config; this.prompt = prompt; this.location = location; this.months = months; } handle = async (logger) => { try { this.prompt.update(this.location, 'Scraping', logger); const baseYearReports = await this.getBasicReport(logger); if (!baseYearReports || baseYearReports.length === 0) { this.prompt.update(this.location, 'Done - No data found', logger); return []; } const reports = []; await Promise.all(baseYearReports .map((report, index) => ({ info: report, index })) .filter(item => !this.months || this.months.includes(parseInt(item.info.reportMonth.substring(0, 2)))) .map(async (item) => { const monthHandler = new MonthHandler(this.config, this.prompt, this.location, item.info, item.index); return monthHandler.handle(logger).then(res => res || item.info); })) .then(reportsList => reportsList.filter(report => report)) .then(reportsList => { reports.push(...reportsList); }); this.prompt.update(this.location, 'Done', logger); return reports; } catch (e) { this.page?.browser().close(); this.prompt.addError(this.location, e?.message || e, logger); return []; } }; /** Get year's basic info */ getBasicReport = async (logger) => { try { this.page = await newPageByYear(this.config.visibleBrowser, this.location[0], logger); await waitForSelectorPlus(this.page, '#ContentUsersPage_TblDuhot', logger); const reports = await this.page.$eval('#dgDuchot', getReportsTable); this.page.browser().close(); return reports; } catch (e) { this.page?.browser().close(); throw new Error(`getReportsTable - ${e?.message || e}`, { cause: e }); } }; }