|
| 1 | +import {TinkoffForeignTaxReportParser} from './reportParser'; |
| 2 | +import {EntryValidatory} from './validator'; |
| 3 | +import {ReportEnricher} from './reportEnricher'; |
| 4 | +import {NalogRuReportMaker} from './reportMaker'; |
| 5 | +import {NalogRuPageController} from './nalogRuPageController'; |
| 6 | +import {EnrichedTaxEntry, TaxEntry} from './entities'; |
| 7 | +import path from 'path'; |
| 8 | +import puppeteer from 'puppeteer'; |
| 9 | + |
| 10 | +export class App { |
| 11 | + constructor(private _fileName: string) {} |
| 12 | + |
| 13 | + async run() { |
| 14 | + const items = await this._parseReport(); |
| 15 | + const enrichedItems = await this._enrichItems(items); |
| 16 | + const browserController = new NalogRuPageController(); |
| 17 | + try { |
| 18 | + const page = await this._preparePage(browserController); |
| 19 | + await this._makeReport(page, enrichedItems); |
| 20 | + } catch (e: any) { |
| 21 | + console.log('[❌] Something went wrong...'); |
| 22 | + console.log(`[❌] Error details: ${e.message}`); |
| 23 | + console.log('[❌] Screenshot is saved'); |
| 24 | + console.log('[❌] Press Enter to close browser'); |
| 25 | + await this._readEnter(); |
| 26 | + await browserController.screenshot(path.join(process.cwd(), 'error.png')); |
| 27 | + } finally { |
| 28 | + await browserController.finalize(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private async _parseReport() { |
| 33 | + console.log('[🚀] Parsing tax report...'); |
| 34 | + const report = new TinkoffForeignTaxReportParser(this._fileName); |
| 35 | + const items = await report.getTaxEntries(); |
| 36 | + const itemsValid = items.every((i) => EntryValidatory.isValid(i)); |
| 37 | + if (!itemsValid) { |
| 38 | + throw new Error('Invalid items found'); |
| 39 | + } |
| 40 | + console.log(`[✅] Done! ${items.length} parsed`); |
| 41 | + return items; |
| 42 | + } |
| 43 | + |
| 44 | + private async _enrichItems(items: TaxEntry[]) { |
| 45 | + console.log( |
| 46 | + `[🚀] Enriching items (loading company names). This may take a while...` |
| 47 | + ); |
| 48 | + const enricher = new ReportEnricher(); |
| 49 | + const enrichedItems = await enricher.enrichItems(items); |
| 50 | + console.log(`[✅] Done!`); |
| 51 | + return enrichedItems; |
| 52 | + } |
| 53 | + |
| 54 | + private async _preparePage(browserController: NalogRuPageController) { |
| 55 | + console.log('[❗️] After pressing Enter, browser will be started...'); |
| 56 | + console.log( |
| 57 | + '[❗️] Login with any suitable method, when return back and press Enter once again' |
| 58 | + ); |
| 59 | + await this._readEnter(); |
| 60 | + console.log('[🚀] Loading browser to make report...'); |
| 61 | + const page = await browserController.getNalogRuPage(); |
| 62 | + console.log( |
| 63 | + '[🖐] Please login to your Nalog.RU account, when go back here and press Enter' |
| 64 | + ); |
| 65 | + await this._readEnter(); |
| 66 | + |
| 67 | + return page; |
| 68 | + } |
| 69 | + |
| 70 | + private async _makeReport(page: puppeteer.Page, items: EnrichedTaxEntry[]) { |
| 71 | + console.log('[🚀] Making report. Please wait, this will take a while...'); |
| 72 | + const reportMaker = new NalogRuReportMaker(page); |
| 73 | + await reportMaker.makeReport(items); |
| 74 | + console.log(`[✅] Done! Draft URL: ${page.url()}`); |
| 75 | + } |
| 76 | + |
| 77 | + private async _readEnter() { |
| 78 | + const stdin = process.stdin; |
| 79 | + return new Promise<void>((res) => { |
| 80 | + const handler = (key: Buffer) => { |
| 81 | + if (key.toString() === '\r') { |
| 82 | + stdin.setRawMode(false); |
| 83 | + stdin.pause(); |
| 84 | + stdin.off('data', handler); |
| 85 | + res(); |
| 86 | + } |
| 87 | + }; |
| 88 | + stdin.setRawMode(true); |
| 89 | + stdin.resume(); |
| 90 | + stdin.setEncoding('utf8'); |
| 91 | + stdin.on('data', handler); |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments