feat(shopping): mağazalar, zahmet skoru ve ürünler (marka/paket, dönüşüm, görseller)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
"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 { storeFormSchema } from "@/shared/schemas/store";
|
||||
import { createStore, setStoreArchived, updateStore } 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;
|
||||
}
|
||||
|
||||
async function run<T, R>(
|
||||
schema: z.ZodType<T>,
|
||||
input: unknown,
|
||||
fn: (userId: string, householdId: string, data: T) => Promise<ActionResult<R>>,
|
||||
): Promise<ActionResult<R>> {
|
||||
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) revalidatePath("/", "layout");
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Yeni (id boş) veya mevcut mağaza. */
|
||||
export async function saveStoreAction(storeId: unknown, input: unknown) {
|
||||
if (storeId) {
|
||||
const id = idSchema.safeParse(storeId);
|
||||
if (!id.success) return fail(tr.common.notFound);
|
||||
return run(storeFormSchema, input, (u, h, d) => updateStore(u, h, id.data, d));
|
||||
}
|
||||
return run(storeFormSchema, input, createStore);
|
||||
}
|
||||
|
||||
export async function setStoreArchivedAction(storeId: unknown, archived: unknown) {
|
||||
const id = idSchema.safeParse(storeId);
|
||||
if (!id.success) return fail(tr.common.notFound);
|
||||
return run(z.boolean(), archived, (u, h, a) => setStoreArchived(u, h, id.data, a));
|
||||
}
|
||||
Reference in New Issue
Block a user