fiddy/apps/web/lib/client/entries.ts
2026-02-11 23:45:15 -08:00

77 lines
2.6 KiB
TypeScript

import { fetchJson } from "@/lib/client/fetch-json";
import type { Entry } from "@/lib/shared/types";
export async function entriesList() {
return fetchJson<{ entries: Entry[] }>("/api/entries", { method: "GET" });
}
export async function entriesCreate(input: {
entryType: "SPENDING" | "INCOME";
amountDollars: number;
occurredAt: string;
necessity: "NECESSARY" | "BOTH" | "UNNECESSARY";
purchaseType: string;
notes?: string;
tags?: string[];
bucketId?: number | null;
isRecurring?: boolean;
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/entries", {
method: "POST",
body: JSON.stringify(input)
});
}
export async function entriesUpdate(input: {
id: number;
entryType: "SPENDING" | "INCOME";
amountDollars: number;
occurredAt: string;
necessity: "NECESSARY" | "BOTH" | "UNNECESSARY";
purchaseType: string;
notes?: string;
tags?: string[];
bucketId?: number | null;
isRecurring?: boolean;
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/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,
isRecurring: input.isRecurring,
frequency: input.frequency,
intervalCount: input.intervalCount,
endCondition: input.endCondition,
endCount: input.endCount,
endDate: input.endDate,
nextRunAt: input.nextRunAt
})
});
}
export async function entriesDelete(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/entries/${numericId}`, { method: "DELETE" });
}