mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
Housing, Portfolio, Tags, the Recurring dialog, the subscription catalog and scan, and the setup wizard move onto the form kit from #602: a refused entry is said under the field that is wrong, in the same words in cloud and local-first. Both runtimes run one set of rules from shared/ for a tag, a housing expense, a holding and a recurring rule, and answer a refusal with 400 { error, fields }. An edit checks and writes only what it changes, so a row an older version stored can still be edited. - A tag name that is blank, over 50 characters, or another tag's in other case is refused at name. A colour must be #RRGGBB; a tag created without one takes the palette's next colour, and an edit without one keeps it. - A housing expense is refused at the field: a name up to 100 characters, six types, an amount to the cent, a real due month and a day that month has (29 February taken). One posted without a due month falls due this month. - A holding needs a ticker up to 20 characters, stored in capitals, shares and a price above zero and below one trillion, and a real date. The edit dialog sends back what is stored, and a buy merges only into the open profile's holding. - A recurring rule needs a description, an amount to the cent, a type, a frequency and a next date. Deleting one the profile does not have is a 404. A rule's period is added once, and a second add of it is a 409 in both runtimes. Local-first's upcoming list is the Worker's. - Another profile's tag, category or account is a 400 at the field it names. - A new tag and a new recurring rule answer 201 in both runtimes, as housing does. - Deleting a housing expense, holding, tag or recurring rule another tab already deleted says so and reloads the list. - MCP's upsert_tag_rule finds and creates tags by the tag rules. - The contract suite settles 12 pins (31 to 19). Slice 5 of 6 in docs/plans/2026-10-07-form-errors.md.
114 lines
3.9 KiB
TypeScript
114 lines
3.9 KiB
TypeScript
/**
|
|
* What the active recurring rules add in the next 30 days: GET /api/recurring/upcoming, worked out
|
|
* here so the Worker and local-first answer it alike. Local-first used to answer the active rules
|
|
* themselves, and the Worker every occurrence of the next 30 days (the contract's
|
|
* `recurring-upcoming`). Nothing in the app calls it yet.
|
|
*/
|
|
import { addDays, nextOccurrence } from './calendarMonths';
|
|
import { toCents } from './money';
|
|
|
|
/** A rule as the upcoming list reads it: its own fields and its category's name and colour. */
|
|
export interface UpcomingRule {
|
|
id: number;
|
|
description: string;
|
|
amount: number;
|
|
type: string;
|
|
frequency: string;
|
|
day_of_month: number | null;
|
|
next_date: string | null;
|
|
category_name?: string | null;
|
|
category_color?: string | null;
|
|
}
|
|
|
|
/** One occurrence: the rule, on the date it falls. */
|
|
export interface UpcomingItem extends UpcomingRule {
|
|
next_date: string;
|
|
}
|
|
|
|
export interface UpcomingCategory {
|
|
name: string;
|
|
color?: string | null;
|
|
total: number;
|
|
items: UpcomingItem[];
|
|
}
|
|
|
|
export interface UpcomingAnswer {
|
|
/** The first 20 occurrences, soonest first. */
|
|
transactions: UpcomingItem[];
|
|
/** Every occurrence by category, the largest total first. */
|
|
byCategory: UpcomingCategory[];
|
|
/** What every occurrence adds up to. */
|
|
totalMonthly: number;
|
|
currency: string;
|
|
}
|
|
|
|
/** How many days ahead the list looks. */
|
|
export const UPCOMING_DAYS = 30;
|
|
|
|
/**
|
|
* Each occurrence of `rules` from `today` (the person's, YYYY-MM-DD) to 30 days on. The rules are
|
|
* the active ones; `currency` is the profile's base currency.
|
|
*/
|
|
export function upcomingRecurring(
|
|
rules: readonly UpcomingRule[],
|
|
today: string,
|
|
currency: string
|
|
): UpcomingAnswer {
|
|
const end = addDays(today, UPCOMING_DAYS);
|
|
const upcoming: UpcomingItem[] = [];
|
|
for (const r of rules) {
|
|
// From the rule's next date. A rule whose next date has passed is listed once, on today, for
|
|
// all it has missed: populate writes each missed period on its own date, one press each (a
|
|
// monthly rule due 15 August, run on 10 October, writes 15 August, then 15 September), so
|
|
// this one entry stands for them and is not one of their dates. After today the rule is
|
|
// listed on its own dates, stepped from its next date and not from today's day, with the step
|
|
// populate takes, so those are the dates populate will write. The monthly step here used to
|
|
// be setMonth() and then the day, and setMonth() overflows first: a rule on the 31st went
|
|
// from January to March, and February was never listed.
|
|
const dates: string[] = [];
|
|
let cursor = r.next_date || today;
|
|
if (cursor <= today) {
|
|
dates.push(today);
|
|
while (cursor && cursor <= today) {
|
|
cursor = nextOccurrence(cursor, r.frequency, r.day_of_month);
|
|
}
|
|
}
|
|
while (cursor && cursor <= end) {
|
|
dates.push(cursor);
|
|
cursor = nextOccurrence(cursor, r.frequency, r.day_of_month);
|
|
}
|
|
for (const date of dates) {
|
|
upcoming.push({
|
|
id: r.id,
|
|
description: r.description,
|
|
amount: r.amount,
|
|
type: r.type,
|
|
frequency: r.frequency,
|
|
day_of_month: r.day_of_month,
|
|
next_date: date,
|
|
category_name: r.category_name,
|
|
category_color: r.category_color,
|
|
});
|
|
}
|
|
}
|
|
|
|
upcoming.sort((a, b) => a.next_date.localeCompare(b.next_date));
|
|
|
|
const byCategory: Record<string, UpcomingCategory> = {};
|
|
let total = 0;
|
|
for (const item of upcoming) {
|
|
const name = item.category_name || 'Uncategorized';
|
|
byCategory[name] ??= { name, color: item.category_color, total: 0, items: [] };
|
|
byCategory[name].total = toCents(byCategory[name].total + item.amount);
|
|
byCategory[name].items.push(item);
|
|
total = toCents(total + item.amount);
|
|
}
|
|
|
|
return {
|
|
transactions: upcoming.slice(0, 20),
|
|
byCategory: Object.values(byCategory).sort((a, b) => b.total - a.total),
|
|
totalMonthly: total,
|
|
currency,
|
|
};
|
|
}
|