@accounter/israeli-vat-scraper
Version:
Scraper app for fetching Israeli VAT data
116 lines (115 loc) • 5.22 kB
JavaScript
import { newPageByMonth, newPageByYear } from '../utils/browser-util.js';
import { getReportDetails, getReportExpansionTitle } from '../utils/evaluation-functions.js';
import { waitAndClick, waitForSelectorPlus } from '../utils/page-util.js';
import { MonthFixesHandler } from './month-fixes-handler.js';
import { MonthInputsHandler } from './month-inputs-handler.js';
import { MonthSalesHandler } from './month-sales-handler.js';
export 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 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 waitAndClick(this.page, selector, logger);
const detailsTable = await waitForSelectorPlus(this.page, '#ContentUsersPage_ucPratimNosafimDuchot1_TblPerutDoch', logger);
const additionalDetails = await detailsTable?.evaluate(getReportDetails);
this.page.browser().close();
return additionalDetails;
};
getExpansions = async (logger) => {
try {
this.prompt.update(this.location, 'Fetching expansion', logger);
const page = await newPageByMonth(this.config.visibleBrowser, this.location[0], this.index, logger);
const expansionCorePromise = this.getReportExpansion(page, logger);
const inputsPromise = new MonthInputsHandler(this.config, this.prompt, this.location, this.index).handle(logger);
const salesPromise = new MonthSalesHandler(this.config, this.prompt, this.location, this.index).handle(logger);
const fixedInvoicesPromise = new 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 waitForSelectorPlus(page, '#shaamcontent', logger);
const titleTable = await 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(getReportExpansionTitle);
this.prompt.update(location, 'Done', logger);
return reportExpansion;
}
catch (e) {
this.prompt.addError(location, e?.message || e, logger);
return undefined;
}
};
}