43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { fetchJson } from "@/lib/client/fetch-json";
|
|
import type { Schedule, ScheduleEndCondition, ScheduleFrequency } from "@/lib/shared/types";
|
|
|
|
type ScheduleInput = {
|
|
entryType: "SPENDING" | "INCOME";
|
|
amountDollars: number;
|
|
necessity: "NECESSARY" | "BOTH" | "UNNECESSARY";
|
|
purchaseType: string;
|
|
notes?: string;
|
|
tags?: string[];
|
|
startsOn: string;
|
|
frequency: ScheduleFrequency;
|
|
intervalCount?: number;
|
|
endCondition?: ScheduleEndCondition;
|
|
endCount?: number | null;
|
|
endDate?: string | null;
|
|
};
|
|
|
|
export async function schedulesList() {
|
|
return fetchJson<{ schedules: Schedule[] }>("/api/schedules", { method: "GET" });
|
|
}
|
|
|
|
export async function schedulesCreate(input: ScheduleInput & { createEntryNow?: boolean }) {
|
|
return fetchJson<{ schedule: Schedule }>("/api/schedules", {
|
|
method: "POST",
|
|
body: JSON.stringify(input)
|
|
});
|
|
}
|
|
|
|
export async function schedulesUpdate(input: ScheduleInput & { id: number; nextRunOn?: string; isActive?: boolean }) {
|
|
return fetchJson<{ schedule: Schedule }>(`/api/schedules/${input.id}`, {
|
|
method: "PATCH",
|
|
body: JSON.stringify(input)
|
|
});
|
|
}
|
|
|
|
export async function schedulesDelete(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/schedules/${numericId}`, { method: "DELETE" });
|
|
}
|