UNPKG

@accounter/israeli-vat-scraper

Version:
72 lines (71 loc) 3.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.monthExpansionRecordsHandler = 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"); class monthExpansionRecordsHandler { config; prompt; location; tabSelector; page = null; index; secondaryIndex; constructor(config, prompt, location, tabSelector, index, secondaryIndex) { this.config = config; this.prompt = prompt; this.location = [...location, 'Records']; this.tabSelector = tabSelector; this.index = index; this.secondaryIndex = secondaryIndex; } handle = async (logger) => { try { this.prompt.update(this.location, 'Fetching', logger); this.page = await (0, browser_util_js_1.newPageByMonth)(this.config.visibleBrowser, this.location[0], this.index, logger); await (0, page_util_js_1.waitAndClick)(this.page, this.tabSelector, logger); await (0, page_util_js_1.waitAndClick)(this.page, `#tblSikum > tbody > tr:nth-child(${this.index}) > td:nth-child(${this.secondaryIndex}) > a`, logger); const recordsTable = await (0, page_util_js_1.waitForSelectorPlus)(this.page, '#dgHeshboniot', logger); const records = (await recordsTable?.evaluate(evaluation_functions_js_1.getReportExpansionInputRecords)) ?? []; this.prompt.update(this.location, 'Fetching details', logger); for (let i = 0; i < records.length; i++) { const details = await this.getRecordDetails(i, logger); if (!details) { throw new Error(`Failed to fetch details for record ${i + 1} of ${records.length}`); } records[i].details = details; } await (0, page_util_js_1.waitAndClick)(this.page, '#btnGoBack', logger); this.prompt.update(this.location, 'Done', logger); return records; } catch (e) { this.prompt.addError(this.location, e?.message || e, logger); return []; } }; getRecordDetails = async (index, logger) => { try { if (!this.page) { this.page = await (0, browser_util_js_1.newPageByMonth)(this.config.visibleBrowser, this.location[0], this.index, logger); await (0, page_util_js_1.waitAndClick)(this.page, this.tabSelector, logger); const button = await (0, page_util_js_1.waitForSelectorPlus)(this.page, `#tblSikum > tbody > tr:nth-child(${this.index}) > td:nth-child(${this.secondaryIndex}) > a`, logger); if (!button) { return undefined; } await button.click(); } await (0, page_util_js_1.waitAndClick)(this.page, `#ContentUsersPage_dgHeshboniot_btnPratimNosafim_${index}`, logger); const detailsTable = await (0, page_util_js_1.waitForSelectorPlus)(this.page, '#ContentUsersPage_ucPratimNosafimHsb1_TblPerutHeshbonit', logger); const details = await detailsTable?.evaluate(evaluation_functions_js_1.getReportExpansionInputRecordDetails); await (0, page_util_js_1.waitAndClick)(this.page, '#BtnCloseDlgPrtNsf', logger); return details; } catch (e) { this.prompt.addError([...this.location, 'Details'], e?.message || e, logger); return undefined; } }; } exports.monthExpansionRecordsHandler = monthExpansionRecordsHandler;