import { fetchJson } from "@/lib/client/fetch-json"; import type { Entry } from "@/lib/shared/types"; export async function recurringEntriesList() { return fetchJson<{ entries: Entry[] }>("/api/recurring-entries", { method: "GET" }); } export async function recurringEntriesCreate(input: { entryType: "SPENDING" | "INCOME"; amountDollars: number; occurredAt: string; necessity: "NECESSARY" | "BOTH" | "UNNECESSARY"; purchaseType: string; notes?: string; tags?: string[]; bucketId?: number | null; frequency?: "DAILY" | "WEEKLY" | "BIWEEKLY" | "MONTHLY" | "QUARTERLY" | "YEARLY" | null; intervalCount?: number; endCondition?: "NEVER" | "AFTER_COUNT" | "BY_DATE" | null; endCount?: number | null; endDate?: string | null; nextRunAt?: string | null; }) { return fetchJson<{ entry: Entry }>("/api/recurring-entries", { method: "POST", body: JSON.stringify(input) }); } export async function recurringEntriesUpdate(input: { id: number; entryType: "SPENDING" | "INCOME"; amountDollars: number; occurredAt: string; necessity: "NECESSARY" | "BOTH" | "UNNECESSARY"; purchaseType: string; notes?: string; tags?: string[]; bucketId?: number | null; frequency?: "DAILY" | "WEEKLY" | "BIWEEKLY" | "MONTHLY" | "QUARTERLY" | "YEARLY" | null; intervalCount?: number; endCondition?: "NEVER" | "AFTER_COUNT" | "BY_DATE" | null; endCount?: number | null; endDate?: string | null; nextRunAt?: string | null; }) { return fetchJson<{ entry: Entry }>(`/api/recurring-entries/${input.id}`, { method: "PATCH", body: JSON.stringify({ entryType: input.entryType, amountDollars: input.amountDollars, occurredAt: input.occurredAt, necessity: input.necessity, purchaseType: input.purchaseType, notes: input.notes, tags: input.tags, bucketId: input.bucketId, frequency: input.frequency, intervalCount: input.intervalCount, endCondition: input.endCondition, endCount: input.endCount, endDate: input.endDate, nextRunAt: input.nextRunAt }) }); } export async function recurringEntriesDelete(input: { id: number | string }) { const numericId = Number(input.id); if (!Number.isFinite(numericId) || numericId <= 0) return { error: { code: "INVALID_ID", message: "Invalid id" } } as const; return fetchJson<{ ok: true }>(`/api/recurring-entries/${numericId}`, { method: "DELETE" }); }