UNPKG

@accounter/israeli-vat-scraper

Version:
65 lines (64 loc) 2.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.YearHandler = void 0; const browser_util_js_1 = require("../utils/browser-util.js"); const evaluation_functions_js_1 = require("../utils/evaluation-functions.js"); const page_util_js_1 = require("../utils/page-util.js"); const month_handler_js_1 = require("./month-handler.js"); 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 month_handler_js_1.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 (0, browser_util_js_1.newPageByYear)(this.config.visibleBrowser, this.location[0], logger); await (0, page_util_js_1.waitForSelectorPlus)(this.page, '#ContentUsersPage_TblDuhot', logger); const reports = await this.page.$eval('#dgDuchot', evaluation_functions_js_1.getReportsTable); this.page.browser().close(); return reports; } catch (e) { this.page?.browser().close(); throw new Error(`getReportsTable - ${e?.message || e}`, { cause: e }); } }; } exports.YearHandler = YearHandler;