/** * Translates each check's own raw mismatch/issue data * (backend/services/checks_service.py's {index, ...} shape) into the * review screen's plain-language, four-part flag format - what's wrong, * where, why, what to do (docs/bankstatement.md section 5.1). A pure * frontend presentation layer; the backend's own checks are untouched - * still return their own terse `detail` text, still used elsewhere (the * Results dropdown's checks list). * * Only four of the seven checks ever flag an individual row - * checks_service.py's own mismatches/issues are index-based only for * running_balance/date_sanity/structure_sanity/narrative_direction; * statement_balance/page_coverage/engine_agreement/ocr_source are * whole-statement checks with no per-row data at all (confirmed by * reading checks_service.py directly - none of those four ever return a * `mismatches` or `issues` key). A transaction with no entry from any of * the four has nothing to show here, regardless of its own `isFlagged` * state (which can also come from engine-disagreement/OCR flags that * aren't row-specific). * * date_sanity/structure_sanity return a free-text `issue` string, not a * structured type - pattern-matched here against the backend's own known * message shapes. If that text ever changes, the fallback below still * shows the raw string as the "why" rather than silently dropping it - * never hiding information the check actually found, per this project's * own governing rule. */ import { fmtMoney } from './formatService' function runningBalanceFlag(item) { return { what: "The balance after this row doesn't follow from the row above.", why: `Expected the balance to be ${fmtMoney(item.expected)} here, but the statement shows ${fmtMoney(item.actual)}.`, whatToDo: "Check whether this row's debit and credit are the wrong way round, or whether a row above it is missing or wrong.", } } function dateSanityFlag(item) { const issue = item.issue || '' if (issue.startsWith('unparseable date:')) { return { what: "This row's date couldn't be read at all.", why: `The extracted text was ${issue.slice('unparseable date: '.length)}.`, whatToDo: 'Check the date against the original statement.', } } if (issue.includes('before the statement period')) { return { what: "This row's date falls before the statement's own period.", why: issue, whatToDo: 'Check the date against the original statement - a misread digit or a day/month swap are the usual causes.', } } if (issue.includes('after the statement period')) { return { what: "This row's date falls after the statement's own period.", why: issue, whatToDo: 'Check the date against the original statement - a misread digit or a day/month swap are the usual causes.', } } if (issue.includes('is out of order')) { return { what: "This row's date is out of order relative to the rows around it.", why: issue, whatToDo: 'Check the date against the original statement - a misread digit or a day/month swap are the usual causes.', } } return { what: "This row's date looks wrong.", why: issue, whatToDo: 'Check the date against the original statement.' } } function structureSanityFlag(item) { const issue = item.issue || '' if (issue === 'no amount at all') { return { what: 'This row has no debit, credit, or amount at all.', why: issue, whatToDo: 'Check the original statement for a value this extractor missed.', } } if (issue === 'blank description') { return { what: 'This row has no description text.', why: issue, whatToDo: 'Check the original statement - a wrapped description line may have been missed.', } } if (issue.startsWith('looks identical to row')) { return { what: 'This row looks identical to another row (same date, amount, and description).', why: issue, whatToDo: 'Confirm this is a real repeated transaction, not an accidental duplicate.', } } return { what: 'This row has a structural problem.', why: issue, whatToDo: 'Check this row against the original statement.' } } function narrativeDirectionFlag(item) { return { what: "This row's description attachment was a close geometric call.", why: item.issue || 'No clear label or geometric signal settled which transaction this wrapped text belongs to.', whatToDo: 'Confirm the description text actually belongs to this row, not the one before or after it.', } } const BUILDERS = { running_balance: runningBalanceFlag, date_sanity: dateSanityFlag, structure_sanity: structureSanityFlag, narrative_direction: narrativeDirectionFlag, } /** * Every flag for a given transaction index, across all four row-flagging * checks - a row can carry more than one (e.g. a bad date AND a broken * running balance), each returned as its own four-part explanation * rather than only the first one found. */ export function flagsForIndex(result, index) { const flags = [] for (const check of result?.checks || []) { const builder = BUILDERS[check.name] if (!builder) continue for (const item of check.mismatches || check.issues || []) { if (item.index === index) flags.push({ checkName: check.name, where: `Row ${index + 1}`, ...builder(item) }) } } return flags }