31cffbe1bb
Add rules engine with CRUD API, condition/action evaluation, and apply-all endpoint. UI: rule builder form with field/operator/value conditions, tag multi-select, apply button with result stats.
434 lines
12 KiB
TypeScript
434 lines
12 KiB
TypeScript
"use client";
|
|
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import type { TransactionRow, StatementRow, TagRow } from "./queries";
|
|
import type { CurrentUser } from "./auth";
|
|
|
|
interface TransactionsResponse {
|
|
data: TransactionRow[];
|
|
total: number;
|
|
limit: number;
|
|
offset: number;
|
|
}
|
|
|
|
interface TransactionFilters {
|
|
from?: string;
|
|
to?: string;
|
|
category?: string;
|
|
bank_name?: string;
|
|
search?: string;
|
|
statement_id?: string;
|
|
tag_id?: string;
|
|
sort_by?: string;
|
|
sort_dir?: string;
|
|
limit?: number;
|
|
offset?: number;
|
|
}
|
|
|
|
function buildParams(filters: TransactionFilters): string {
|
|
const params = new URLSearchParams();
|
|
Object.entries(filters).forEach(([key, val]) => {
|
|
if (val !== undefined && val !== "") params.set(key, String(val));
|
|
});
|
|
return params.toString();
|
|
}
|
|
|
|
export function useTransactions(filters: TransactionFilters) {
|
|
return useQuery<TransactionsResponse>({
|
|
queryKey: ["transactions", filters],
|
|
queryFn: async () => {
|
|
const res = await fetch(`/api/transactions?${buildParams(filters)}`);
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTransaction(id: number) {
|
|
return useQuery<TransactionRow>({
|
|
queryKey: ["transaction", id],
|
|
queryFn: async () => {
|
|
const res = await fetch(`/api/transactions/${id}`);
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useStatements() {
|
|
return useQuery<StatementRow[]>({
|
|
queryKey: ["statements"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/statements");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useStatement(id: number) {
|
|
return useQuery<StatementRow>({
|
|
queryKey: ["statement", id],
|
|
queryFn: async () => {
|
|
const res = await fetch(`/api/statements/${id}`);
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useBanks() {
|
|
return useQuery<string[]>({
|
|
queryKey: ["banks"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/merchants?type=banks");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateTransaction() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
id,
|
|
...data
|
|
}: {
|
|
id: number;
|
|
category?: string;
|
|
merchant_normalized?: string;
|
|
notes?: string;
|
|
}) => {
|
|
const res = await fetch(`/api/transactions/${id}`, {
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
return res.json();
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["transactions"] });
|
|
qc.invalidateQueries({ queryKey: ["transaction"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useBulkAction() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body: {
|
|
action: string;
|
|
ids: number[];
|
|
category?: string;
|
|
merchant_normalized?: string;
|
|
splits?: { participant_id: number; share_percent: number }[];
|
|
tag_id?: number;
|
|
}) => {
|
|
const res = await fetch("/api/transactions/bulk", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
throw new Error(err.error || "Bulk action failed");
|
|
}
|
|
return res.json();
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
qc.invalidateQueries({ queryKey: ["transactions"] });
|
|
if (variables.action === "split") {
|
|
qc.invalidateQueries({ queryKey: ["splits"] });
|
|
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
|
|
qc.invalidateQueries({ queryKey: ["participant-balances"] });
|
|
}
|
|
if (variables.action === "tag" || variables.action === "untag") {
|
|
qc.invalidateQueries({ queryKey: ["tags"] });
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useParticipants() {
|
|
return useQuery<{ id: number; name: string; created_at: string }[]>({
|
|
queryKey: ["participants"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/participants");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useParticipantBalances() {
|
|
return useQuery<{ id: number; name: string; total_owed: number; unsettled_count: number }[]>({
|
|
queryKey: ["participant-balances"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/participants/balances");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSharedTransactions() {
|
|
return useQuery({
|
|
queryKey: ["shared-transactions"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/shared-transactions");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTransactionSplits(transactionId: number) {
|
|
return useQuery({
|
|
queryKey: ["splits", transactionId],
|
|
queryFn: async () => {
|
|
const res = await fetch(`/api/transactions/${transactionId}/splits`);
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSetSplits() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({
|
|
transactionId,
|
|
splits,
|
|
}: {
|
|
transactionId: number;
|
|
splits: { participant_id: number; share_percent: number }[];
|
|
}) => {
|
|
const res = await fetch(`/api/transactions/${transactionId}/splits`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ splits }),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
throw new Error(err.error || "Failed to set splits");
|
|
}
|
|
return res.json();
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["splits"] });
|
|
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
|
|
qc.invalidateQueries({ queryKey: ["participant-balances"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSettleSplits() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (body: { participant_id?: number; split_ids?: number[] }) => {
|
|
const res = await fetch("/api/splits/settle", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
return res.json();
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["shared-transactions"] });
|
|
qc.invalidateQueries({ queryKey: ["participant-balances"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCurrentUser() {
|
|
return useQuery<CurrentUser>({
|
|
queryKey: ["me"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/me");
|
|
if (!res.ok) throw new Error("Not authenticated");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateStatement() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ id, owner_id }: { id: number; owner_id: number }) => {
|
|
const res = await fetch(`/api/statements/${id}`, {
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ owner_id }),
|
|
});
|
|
return res.json();
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["statements"] });
|
|
qc.invalidateQueries({ queryKey: ["transactions"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTags() {
|
|
return useQuery<(TagRow & { transaction_count: number })[]>({
|
|
queryKey: ["tags"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/tags");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateTag() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ name, color }: { name: string; color?: string }) => {
|
|
const res = await fetch("/api/tags", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ name, color }),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
throw new Error(err.error || "Failed to create tag");
|
|
}
|
|
return res.json();
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["tags"] }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteTag() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (id: number) => {
|
|
await fetch(`/api/tags/${id}`, { method: "DELETE" });
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["tags"] });
|
|
qc.invalidateQueries({ queryKey: ["transactions"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAddTransactionTag() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ transactionId, tagId }: { transactionId: number; tagId: number }) => {
|
|
await fetch(`/api/transactions/${transactionId}/tags`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ tag_id: tagId }),
|
|
});
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["transactions"] }),
|
|
});
|
|
}
|
|
|
|
export function useRemoveTransactionTag() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ transactionId, tagId }: { transactionId: number; tagId: number }) => {
|
|
await fetch(`/api/transactions/${transactionId}/tags`, {
|
|
method: "DELETE",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ tag_id: tagId }),
|
|
});
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["transactions"] }),
|
|
});
|
|
}
|
|
|
|
export function useCreateParticipant() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ name, email }: { name: string; email?: string }) => {
|
|
const res = await fetch("/api/participants", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ name, email }),
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json();
|
|
throw new Error(err.error || "Failed to create participant");
|
|
}
|
|
return res.json();
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["participants"] });
|
|
qc.invalidateQueries({ queryKey: ["participant-balances"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
// --- Rules ---
|
|
|
|
export interface RuleRow {
|
|
id: number;
|
|
name: string;
|
|
conditions: { field: string; operator: string; value: string }[];
|
|
actions: { set_category?: string; add_tag_ids?: number[]; set_merchant?: string };
|
|
enabled: boolean;
|
|
priority: number;
|
|
created_at: string;
|
|
}
|
|
|
|
export function useRules() {
|
|
return useQuery<RuleRow[]>({
|
|
queryKey: ["rules"],
|
|
queryFn: async () => {
|
|
const res = await fetch("/api/rules");
|
|
return res.json();
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useCreateRule() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (data: Omit<RuleRow, "id" | "created_at">) => {
|
|
const res = await fetch("/api/rules", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
if (!res.ok) throw new Error("Failed to create rule");
|
|
return res.json();
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["rules"] }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateRule() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ id, ...data }: Partial<RuleRow> & { id: number }) => {
|
|
const res = await fetch(`/api/rules/${id}`, {
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
});
|
|
if (!res.ok) throw new Error("Failed to update rule");
|
|
return res.json();
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["rules"] }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteRule() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (id: number) => {
|
|
await fetch(`/api/rules/${id}`, { method: "DELETE" });
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["rules"] }),
|
|
});
|
|
}
|
|
|
|
export function useApplyRules() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
const res = await fetch("/api/rules/apply", { method: "POST" });
|
|
if (!res.ok) throw new Error("Failed to apply rules");
|
|
return res.json() as Promise<{ matched: number; transactions_affected: number }>;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["transactions"] });
|
|
qc.invalidateQueries({ queryKey: ["rules"] });
|
|
},
|
|
});
|
|
}
|