5d370a062e
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.0 KiB
TypeScript
89 lines
3.0 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 { contactEntrySchema, contactFormSchema } from "@/shared/schemas/contact";
|
|
import {
|
|
createContact,
|
|
createContactEntry,
|
|
setContactArchived,
|
|
updateContact,
|
|
updateContactEntry,
|
|
} 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;
|
|
}
|
|
|
|
function revalidate() {
|
|
revalidatePath("/", "layout");
|
|
}
|
|
|
|
export async function saveContactAction(
|
|
contactId: unknown,
|
|
input: unknown,
|
|
): Promise<ActionResult<unknown>> {
|
|
const ctx = await getHouseholdActionContext("member");
|
|
if (!ctx) return fail(tr.common.unauthorized);
|
|
const parsed = contactFormSchema.safeParse(input);
|
|
if (!parsed.success) return fail(tr.adminUsers.errors.checkFields, fieldErrors(parsed.error));
|
|
let result: ActionResult<unknown>;
|
|
if (contactId === null || contactId === undefined) {
|
|
result = await createContact(ctx.user.id, ctx.household.id, parsed.data);
|
|
} else {
|
|
const id = idSchema.safeParse(contactId);
|
|
if (!id.success) return fail(tr.common.notFound);
|
|
result = await updateContact(ctx.user.id, ctx.household.id, id.data, parsed.data);
|
|
}
|
|
if (result.ok) revalidate();
|
|
return result;
|
|
}
|
|
|
|
export async function setContactArchivedAction(
|
|
contactId: unknown,
|
|
archived: unknown,
|
|
): Promise<ActionResult> {
|
|
const ctx = await getHouseholdActionContext("member");
|
|
if (!ctx) return fail(tr.common.unauthorized);
|
|
const id = idSchema.safeParse(contactId);
|
|
const a = z.boolean().safeParse(archived);
|
|
if (!id.success || !a.success) return fail(tr.common.notFound);
|
|
const result = await setContactArchived(ctx.user.id, ctx.household.id, id.data, a.data);
|
|
if (result.ok) revalidate();
|
|
return result;
|
|
}
|
|
|
|
/** Yeni hareket (transactionId boş) veya mevcut hareketi düzenle. */
|
|
export async function saveContactEntryAction(
|
|
contactId: unknown,
|
|
transactionId: unknown,
|
|
input: unknown,
|
|
): Promise<ActionResult<unknown>> {
|
|
const ctx = await getHouseholdActionContext("member");
|
|
if (!ctx) return fail(tr.common.unauthorized);
|
|
const parsed = contactEntrySchema.safeParse(input);
|
|
if (!parsed.success) return fail(tr.adminUsers.errors.checkFields, fieldErrors(parsed.error));
|
|
let result: ActionResult<unknown>;
|
|
if (transactionId) {
|
|
const tid = idSchema.safeParse(transactionId);
|
|
if (!tid.success) return fail(tr.common.notFound);
|
|
result = await updateContactEntry(ctx.user.id, ctx.household.id, tid.data, parsed.data);
|
|
} else {
|
|
const cid = idSchema.safeParse(contactId);
|
|
if (!cid.success) return fail(tr.common.notFound);
|
|
result = await createContactEntry(ctx.user.id, ctx.household.id, cid.data, parsed.data);
|
|
}
|
|
if (result.ok) revalidate();
|
|
return result;
|
|
}
|