UNPKG

@accounter/israeli-vat-scraper

Version:
120 lines (119 loc) 5.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MonthHandler = 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_fixes_handler_js_1 = require("./month-fixes-handler.js"); const month_inputs_handler_js_1 = require("./month-inputs-handler.js"); const month_sales_handler_js_1 = require("./month-sales-handler.js"); class MonthHandler { config; prompt; location; report; index; page = null; constructor(config, prompt, location, report, index, page) { this.config = config; this.prompt = prompt; this.location = [...location, report.reportMonth.substring(0, 2)]; this.report = report; this.index = index; this.page = page || null; } handle = async (logger) => { try { this.prompt.update(this.location, 'Fetching details', logger); const additionalDetailsPromise = this.getReportAdditionalDetails(logger).catch((e) => { this.prompt.addError(this.location, e.message, logger); this.page?.browser().close(); return undefined; }); const reportExpansionPromise = this.config.expandData ? this.getExpansions(logger) : undefined; await Promise.all([additionalDetailsPromise, reportExpansionPromise]).then(res => { if (!res[0]) { throw new Error('Failed to fetch report additional details'); } this.report.additionalDetails = res[0]; if (!res[1]) { throw new Error('Failed to fetch report expansion'); } this.report.reportExpansion = res[1]; }); this.prompt.update(this.location, 'Done', logger); return this.report; } catch (e) { this.prompt.addError(this.location, e?.message || e, logger); return undefined; } }; getReportAdditionalDetails = async (logger) => { this.page ||= await (0, browser_util_js_1.newPageByYear)(this.config.visibleBrowser, this.location[0], logger); const selector = `#dgDuchot > tbody > tr:nth-child(${this.index + 2}) > td:nth-child(7) > table > tbody > tr > td:nth-child(1) > input`; await (0, page_util_js_1.waitAndClick)(this.page, selector, logger); const detailsTable = await (0, page_util_js_1.waitForSelectorPlus)(this.page, '#ContentUsersPage_ucPratimNosafimDuchot1_TblPerutDoch', logger); const additionalDetails = await detailsTable?.evaluate(evaluation_functions_js_1.getReportDetails); this.page.browser().close(); return additionalDetails; }; getExpansions = async (logger) => { try { this.prompt.update(this.location, 'Fetching expansion', logger); const page = await (0, browser_util_js_1.newPageByMonth)(this.config.visibleBrowser, this.location[0], this.index, logger); const expansionCorePromise = this.getReportExpansion(page, logger); const inputsPromise = new month_inputs_handler_js_1.MonthInputsHandler(this.config, this.prompt, this.location, this.index).handle(logger); const salesPromise = new month_sales_handler_js_1.MonthSalesHandler(this.config, this.prompt, this.location, this.index).handle(logger); const fixedInvoicesPromise = new month_fixes_handler_js_1.MonthFixesHandler(this.config, this.prompt, this.location, this.index).handle(logger); const reportExpansion = await Promise.all([ expansionCorePromise, inputsPromise, salesPromise, fixedInvoicesPromise, ]).then(res => { if (!res[0] || !res[1] || !res[2] || !res[3]) { return undefined; } return { ...res[0], inputs: res[1], sales: res[2], fixedInvoices: res[3], }; }); page.browser().close(); if (!reportExpansion) { return undefined; } return reportExpansion; } catch (e) { this.prompt.addError([...this.location, 'Expansions'], e?.message || e, logger); return undefined; } }; getReportExpansion = async (page, logger) => { const location = [...this.location, 'Title']; try { // get title this.prompt.update(location, 'Fetching title...', logger); await (0, page_util_js_1.waitForSelectorPlus)(page, '#shaamcontent', logger); const titleTable = await (0, page_util_js_1.waitForSelectorPlus)(page, '#shaamcontent > table > tbody > tr:nth-child(2) > td > table', logger); if (!titleTable) { this.prompt.addError(location, 'Error fetching title', logger); return undefined; } const reportExpansion = await titleTable.evaluate(evaluation_functions_js_1.getReportExpansionTitle); this.prompt.update(location, 'Done', logger); return reportExpansion; } catch (e) { this.prompt.addError(location, e?.message || e, logger); return undefined; } }; } exports.MonthHandler = MonthHandler;