Simpl-Resultat/src/utils/sqlFlag.ts
le king fu f758282c2e fix(transactions): bind the manual-categorization flag as an integer (#390)
updateTransactionCategory bound the JS boolean `true`. tauri-plugin-sql
passes a bool to sqlx as a JSON value, so the column received the TEXT
'true', which the getting-started checklist's `is_manually_categorized = 1`
never matches: step 5 never ticked.

toSqlFlag turns a boolean, a number or a backup's string into 0/1. It is
used when a category is set by hand and when a JSON or CSV backup is
restored, since a backup exported while the bug stood carries "true"
(the CSV path read it as 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 20:35:00 -04:00

22 lines
938 B
TypeScript

/**
* Turn any truthy-looking value into the integer SQLite expects for a 0/1
* flag column (#390).
*
* Never bind a JS boolean through tauri-plugin-sql: the plugin only special-
* cases null, strings and numbers, so `true` reaches sqlx as a JSON value and
* is stored as the TEXT `'true'`. An INTEGER column keeps that text (it is not
* a number), and `WHERE flag = 1` never matches it.
*
* Accepts what a flag can look like on the way in: a boolean from the UI, a
* number from the database, or a string from a backup (`"1"`, or `"true"` in a
* backup exported while the bug stood).
*/
export function toSqlFlag(value: unknown): 0 | 1 {
if (value === true) return 1;
if (typeof value === "number") return Number.isFinite(value) && value !== 0 ? 1 : 0;
if (typeof value === "string") {
const normalized = value.trim().toLowerCase();
return normalized === "1" || normalized === "true" ? 1 : 0;
}
return 0;
}