Core: per-group default category (group.updated defaultCategory)
SPEC listed defaultCategory on group.updated but nothing implemented it. Replay accepts one of CATEGORIES and ignores the whole event otherwise, validating before any field is applied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gVKYyo9BHLHPW4qr4x
This commit is contained in:
parent
769b76857a
commit
c12fd64f09
3 changed files with 18 additions and 1 deletions
|
|
@ -4,7 +4,9 @@
|
|||
// after its target event; nothing is ever erased from the log. See SPEC.md §6.
|
||||
import { validateExpense } from './expense.ts';
|
||||
import { isCurrencyCode } from './money.ts';
|
||||
import type { Activity, GroupEvent, GroupState, Recurring } from './types.ts';
|
||||
import { CATEGORIES, type Activity, type GroupEvent, type GroupState, type Recurring } from './types.ts';
|
||||
|
||||
const isCategory = (c: unknown): c is (typeof CATEGORIES)[number] => (CATEGORIES as readonly unknown[]).includes(c);
|
||||
|
||||
export function emptyState(): GroupState {
|
||||
return { group: null, members: [], expenses: {}, comments: {}, recurring: {} };
|
||||
|
|
@ -40,18 +42,22 @@ function apply(s: GroupState, ev: GroupEvent, snapshots: Map<string, GroupState>
|
|||
const d = ev.data;
|
||||
if (s.group || !str(d?.name, 100) || !isCurrencyCode(d.baseCurrency)) return ignore();
|
||||
s.group = { name: d.name, baseCurrency: d.baseCurrency, icon: typeof d.icon === 'string' ? d.icon.slice(0, 16) : '' };
|
||||
if (isCategory(d.defaultCategory)) s.group.defaultCategory = d.defaultCategory;
|
||||
act.after = { ...s.group };
|
||||
break;
|
||||
}
|
||||
case 'group.updated': {
|
||||
if (!s.group) return ignore();
|
||||
const d = ev.data ?? {};
|
||||
// Validate before changing anything, so an invalid event has no effect.
|
||||
if (d.defaultCategory !== undefined && !isCategory(d.defaultCategory)) return ignore();
|
||||
act.before = { ...s.group };
|
||||
if (d.name !== undefined) {
|
||||
if (!str(d.name, 100)) return ignore();
|
||||
s.group.name = d.name;
|
||||
}
|
||||
if (typeof d.icon === 'string') s.group.icon = d.icon.slice(0, 16);
|
||||
if (d.defaultCategory !== undefined) s.group.defaultCategory = d.defaultCategory;
|
||||
if (d.baseCurrency !== undefined && d.baseCurrency !== s.group.baseCurrency) {
|
||||
// Locked once the group has any expense (SPEC §7.1).
|
||||
if (hasExpenses || !isCurrencyCode(d.baseCurrency)) return ignore();
|
||||
|
|
|
|||
|
|
@ -81,6 +81,8 @@ export interface GroupInfo {
|
|||
name: string;
|
||||
baseCurrency: string;
|
||||
icon: string;
|
||||
/** category new expenses start with; absent = 'other' */
|
||||
defaultCategory?: (typeof CATEGORIES)[number];
|
||||
}
|
||||
|
||||
export type EventBody =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue