9781cd9ed2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
2.8 KiB
TypeScript
83 lines
2.8 KiB
TypeScript
"use server";
|
||
|
||
import { revalidatePath } from "next/cache";
|
||
import { z } from "zod";
|
||
import { tr } from "@/i18n/tr";
|
||
import { fail, type ActionResult } from "@/lib/action-result";
|
||
import { getHouseholdActionContext } from "@/lib/dal";
|
||
import {
|
||
assetPriceFormSchema,
|
||
contributionSchema,
|
||
inKindFormSchema,
|
||
paymentFormSchema,
|
||
} from "@/shared/schemas/in-kind";
|
||
import {
|
||
addContribution,
|
||
createInKind,
|
||
deleteInKind,
|
||
recordPayment,
|
||
savePrice,
|
||
updateInKind,
|
||
} from "./service";
|
||
|
||
const idSchema = z.string().regex(/^[a-f0-9]{24}$/i);
|
||
|
||
function fieldErrors(error: z.ZodError) {
|
||
const out: Record<string, string> = {};
|
||
for (const issue of error.issues) {
|
||
const key = issue.path.join(".");
|
||
if (key && !out[key]) out[key] = issue.message;
|
||
}
|
||
return out;
|
||
}
|
||
|
||
const revalidate = () => revalidatePath("/", "layout");
|
||
|
||
async function run<T>(
|
||
schema: z.ZodType<T>,
|
||
input: unknown,
|
||
fn: (userId: string, householdId: string, data: T) => Promise<ActionResult<unknown>>,
|
||
): Promise<ActionResult<unknown>> {
|
||
const ctx = await getHouseholdActionContext("member");
|
||
if (!ctx) return fail(tr.common.unauthorized);
|
||
const parsed = schema.safeParse(input);
|
||
if (!parsed.success) return fail(tr.adminUsers.errors.checkFields, fieldErrors(parsed.error));
|
||
const result = await fn(ctx.user.id, ctx.household.id, parsed.data);
|
||
if (result.ok) revalidate();
|
||
return result;
|
||
}
|
||
|
||
export async function savePriceAction(input: unknown) {
|
||
return run(assetPriceFormSchema, input, savePrice);
|
||
}
|
||
|
||
/** Yeni aynî kayıt (debtId boş, contactId dolu) veya mevcut kaydı düzenle. */
|
||
export async function saveInKindAction(contactId: unknown, debtId: unknown, input: unknown) {
|
||
if (debtId) {
|
||
const id = idSchema.safeParse(debtId);
|
||
if (!id.success) return fail(tr.common.notFound);
|
||
return run(inKindFormSchema, input, (u, h, d) => updateInKind(u, h, id.data, d));
|
||
}
|
||
const cid = idSchema.safeParse(contactId);
|
||
if (!cid.success) return fail(tr.common.notFound);
|
||
return run(inKindFormSchema, input, (u, h, d) => createInKind(u, h, cid.data, d));
|
||
}
|
||
|
||
export async function deleteInKindAction(debtId: unknown): Promise<ActionResult<unknown>> {
|
||
const id = idSchema.safeParse(debtId);
|
||
if (!id.success) return fail(tr.common.notFound);
|
||
return run(z.unknown(), null, (u, h) => deleteInKind(u, h, id.data));
|
||
}
|
||
|
||
export async function addContributionAction(debtId: unknown, input: unknown) {
|
||
const id = idSchema.safeParse(debtId);
|
||
if (!id.success) return fail(tr.common.notFound);
|
||
return run(contributionSchema, input, (u, h, d) => addContribution(u, h, id.data, d));
|
||
}
|
||
|
||
export async function recordPaymentAction(debtId: unknown, input: unknown) {
|
||
const id = idSchema.safeParse(debtId);
|
||
if (!id.success) return fail(tr.common.notFound);
|
||
return run(paymentFormSchema, input, (u, h, d) => recordPayment(u, h, id.data, d));
|
||
}
|