mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Three rows of a 5,194-row sheet were rejected for "Could not read amount_local"
— values like 754.312, left by a currency conversion that multiplies and never
rounds. Losing a transaction over a tenth of a cent is the wrong trade.
Separator rules, now in shared/importNumber.ts:
1,234.56 both separators -> the LAST one is the decimal point
1.234.567 one char, repeated -> grouping, an integer
1.234 exactly one -> a decimal point, always
1234 none -> an integer
Rule 3 is the change. A lone separator used to be rejected as ambiguous; it now
reads as money, because prices carry cents. It is also the rule that can be
wrong — `1.000` as European thousands means 1000 and this reads it as 1.00 — so
that exact shape is flagged and surfaced rather than decided silently. Grouping
structure is still validated strictly: relaxing which separator means "decimal"
is deliberate, accepting `1,2,3` is not.
Fractions longer than two digits round to cents, and the flag for that fires on
the amount actually moving by half a cent or more rather than on the digit
count. A sheet that multiplies to convert a currency stores binary-float residue
in hundreds of rows; on the reported sheet 250 rows carry residue, 7 carry real
extra precision, and only those 7 are flagged.
A row with no date imports dated today and warns, in BOTH runtimes, instead of
being rejected locally and silently stamped today by the Worker. A date the user
can correct beats a transaction that never arrived. Its dedup behaviour is
pinned: the sync runs daily, and a row failing to match its stored copy would
pile up one duplicate per day.
Warnings are a channel of their own alongside skipped_items and never deselect a
row — the row imports either way; the note is how the user knows to look at it.
Rejections are numbered as the spreadsheet numbers them. Every parser consumes
the first record as the header, so rows[0] is the second line of the source and
`index + 1` named the line above the one the user had to open.
Both runtimes now share importNumber.ts and importRowChecks.ts. The two number
parsers were byte-identical apart from a doc comment, and the row checks are
exactly where the runtimes drifted last time, costing 291 rows.
74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
/**
|
|
* Per-row validation for imports — the numeric fields and the date.
|
|
*
|
|
* Shared because the two runtimes disagreeing here is not hypothetical: the IndexedDB path used to
|
|
* reject rows with no description while the Worker accepted them, so the same sheet imported clean
|
|
* in the cloud and silently lost 291 rows locally. One implementation, both runtimes.
|
|
*
|
|
* A row is REJECTED only when a value cannot be read at all. Anything that can be read but might
|
|
* not be what the user meant — a rounded amount, a date we had to invent — imports and raises a
|
|
* warning, so the row lands in the ledger and the user is told what to look at.
|
|
*/
|
|
import { describeImportNumberFlags, parseImportNumberDetailed } from './importNumber';
|
|
|
|
/** A non-fatal note about a row that was imported. */
|
|
export interface ImportRowWarning {
|
|
index: number;
|
|
reason: string;
|
|
label?: string;
|
|
}
|
|
|
|
export interface ImportRowNumbers {
|
|
amount: number | null;
|
|
amountLocal: number | null;
|
|
exchangeRate: number | null;
|
|
/** Field names that could not be read at all — a non-empty list means reject the row. */
|
|
invalidFields: string[];
|
|
/** Sentences to show the user for a row that still imports. */
|
|
warnings: string[];
|
|
}
|
|
|
|
const isBlank = (value: unknown): boolean =>
|
|
value === null || value === undefined || String(value).trim() === '';
|
|
|
|
/**
|
|
* Read a row's three financial fields.
|
|
*
|
|
* `amount_local` falls back to `amount` and `exchange_rate` to 1 when blank — a sheet that keeps
|
|
* one currency leaves both empty on nearly every row.
|
|
*/
|
|
export function checkImportRowNumbers(raw: {
|
|
amount: unknown;
|
|
amountLocal: unknown;
|
|
exchangeRate: unknown;
|
|
}): ImportRowNumbers {
|
|
const invalidFields: string[] = [];
|
|
const warnings: string[] = [];
|
|
|
|
const collect = (field: string, source: unknown) => {
|
|
const result = parseImportNumberDetailed(source);
|
|
const note = describeImportNumberFlags(field, String(source), result.flags);
|
|
if (note) warnings.push(note);
|
|
return result.value;
|
|
};
|
|
|
|
const amount = collect('amount', raw.amount);
|
|
const amountLocal = isBlank(raw.amountLocal) ? amount : collect('amount_local', raw.amountLocal);
|
|
const exchangeRate = isBlank(raw.exchangeRate) ? 1 : collect('exchange_rate', raw.exchangeRate);
|
|
|
|
if (amount === null) invalidFields.push('amount');
|
|
if (amountLocal === null) invalidFields.push('amount_local');
|
|
if (exchangeRate === null || exchangeRate <= 0) invalidFields.push('exchange_rate');
|
|
|
|
return { amount, amountLocal, exchangeRate, invalidFields, warnings };
|
|
}
|
|
|
|
/** The message shown for a row whose date the sheet did not supply. */
|
|
export const MISSING_DATE_WARNING =
|
|
"No date in this row — imported with today's date. Open the transaction to set the real one.";
|
|
|
|
/** The reason string for a row whose numbers could not be read. */
|
|
export function unreadableNumbersReason(invalidFields: string[]): string {
|
|
return `Could not read ${invalidFields.join(', ')} — check the number format`;
|
|
}
|