Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 1x 54x 54x 54x 54x 258x 258x 155x 103x 6x 6x 6x 6x 6x 97x 97x 3x 3x 3x 3x 94x 94x 54x 54x 88x 54x 54x 54x 54x 2x 54x 88x 88x 54x 54x 54x 85x 351x 351x 54x | import { parse as htmlParse, Node } from 'node-html-parser'; import { parse as dateParse} from 'date-format-parse'; import { inject, injectable } from 'inversify'; import ILogger from '../../core/contracts/ILogger'; import ITransactionDataProvider from '../../core/contracts/ITransactionDataProvider'; import { injectables } from '../../core/types/injectables'; import TransactionData from '../../core/types/transactionData'; import EntryType from '../../core/enums/entryType'; import { TRANSACTION_TYPES } from '../../core/types/transactionTypeString'; import transactionTypesByString from '../../core/types/transactionTypeByString' import TransactionType from '../../core/enums/transactionType'; @injectable() export default class GmailTransactionDataProvider implements ITransactionDataProvider { private readonly logger: ILogger; public constructor( @inject(injectables.ILogger) logger: ILogger ) { this.logger = logger; } public get(transactionDataRaw: string) { const transactionDataHtml = this.parseTransactionDataHtml(transactionDataRaw); const date = this.parseDate(transactionDataHtml); const reference = this.parseReference(transactionDataHtml); const valueDate = this.parseValueDate(transactionDataHtml); const sum = this.parseSum(transactionDataHtml); const entryType = this.parseEntryType(transactionDataHtml, reference); const { transactionType, paymentDetailsRaw } = this.parseTransactionType(transactionDataHtml, reference); const additionalDetailsRaw = this.parseAdditionalDetails(transactionDataHtml); const transactionData: TransactionData = { date, reference, valueDate, sum, entryType, transactionType, paymentDetailsRaw, additionalDetailsRaw: additionalDetailsRaw }; return transactionData; } private parseTransactionDataHtml(attachmentData: string) { const transactionData = htmlParse(attachmentData) .childNodes[1] // <html> ?.childNodes[3] // <body> ?.childNodes[12] // <table> ?.childNodes[5] // <tr> ?.childNodes; // <td>[] return transactionData; } private parseDate(transactionData: Node[]) { const date = dateParse(transactionData ?.[1] ?.childNodes[0] ?.rawText ?.padUTCTimezone(), 'DD.MM.YYYY HH:mm:ssZ') // Parse as UTC to avoid any client-side conversion side-effects ?.fromLocaltoUTC(); // Treat the parsed datetime as localised so convert it to UTC return date; } private parseReference(transactionData: Node[]) { const reference = transactionData ?.[3] ?.childNodes[1] ?.childNodes[0] ?.rawText; return reference; } private parseValueDate(transactionData: Node[]) { const valueDate = dateParse(transactionData ?.[5] ?.childNodes[0] ?.rawText ?.padTime() ?.padUTCTimezone(), 'DD.MM.YYYY HH:mm:ssZ'); // Value date is date-only, so treat it as midnight UTC to avoid any client-side conversion side-effects return valueDate; } private parseSum(transactionData: Node[]) { const sum = transactionData ?.[7] ?.childNodes[0] ?.rawText; return sum; } private parseEntryType(transactionData: Node[], transactionReference: string) { const entryTypeStr = transactionData?.[9] ?.childNodes[0] ?.rawText; const entryType: EntryType = (entryTypeStr == 'ДТ' || entryTypeStr == 'DR') ? EntryType.DEBIT : (entryTypeStr == 'КТ' || entryTypeStr == 'CR') ? EntryType.CREDIT : EntryType.INVALID; if (entryType === EntryType.INVALID) { this.logger.warn(`Unregonised entry type '${entryTypeStr}'`, { transactionReference: transactionReference }); } return entryType; } private parseTransactionType(transactionData: Node[], transactionReference: string) { const maxDataLineLength = 100; const dataElements = transactionData ?.[11] ?.childNodes; const dataRaw = dataElements ?.reduce((accumulator, current, i) => { const currentString = current.toString(); // Treat <br> variants as linebreaks if (currentString === '<br>' || currentString === '<br />') { return accumulator; } // Don't treat <wbr> as a linebreak if (accumulator.length > 0 && accumulator[accumulator.length-1] === '<wbr>') { accumulator.pop(); // pop the <wbr> const last = accumulator.pop(); // pop and collect the element preceeding <wbr> const updated = last?.concat(currentString) // combine the last element with the current one accumulator.push(updated ?? currentString); // push either the concatenated elements, or only the current one return accumulator; } // Don't add <wbr> if it's the last element (meaning it won't be removed by the previous clause) Iif (i == dataElements.length && currentString === '<wbr>') { return accumulator; } // If the current element is longer than the maximum data line length, append the last element to it if (accumulator.length > 0 && currentString.length >= maxDataLineLength) { const last = accumulator.pop()!; // pop and colect the last element const updated = currentString.concat(last); // combine the current element with the last one accumulator.push(updated); return accumulator; } accumulator.push(currentString); return accumulator; }, [] as string[]); const typeRegex = new RegExp(`([ ]?[\/]?(${TRANSACTION_TYPES.join('|')})[ ]?)`); const found = dataRaw ?.map(e => typeRegex.exec(e)?.[2]) ?.flat() ?.[0] ?? null; const typeByString = found as keyof typeof transactionTypesByString; const valid = found !== null && TRANSACTION_TYPES.includes(typeByString); const transactionType = valid ? transactionTypesByString[typeByString] : TransactionType.UNKNOWN; if (transactionType === TransactionType.UNKNOWN) { this.logger.warn(`Unknown transaction type '${found}'`, { transactionReference: transactionReference }); } const paymentDetailsRaw = dataRaw ?.map(e => e.replace(typeRegex, '')) ?.filter(e => e != ''); return { transactionType, paymentDetailsRaw }; } private parseAdditionalDetails(transactionData: Node[]) { const additionalDetailsNode = transactionData ?.[13] ?.childNodes ?.[1]; const additionalDetails = additionalDetailsNode ?.childNodes ?.map(e => e.rawText.split('\n')) ?.flat() ?.map(e => e.trim()) ?.filter(e => e != '') ?? []; return additionalDetails; } } |