56 lines
1.7 KiB
TypeScript
56 lines
1.7 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;
|
|
}) {
|
|
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;
|
|
}) {
|
|
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
|
|
})
|
|
});
|
|
}
|
|
|
|
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" });
|
|
}
|