"use server"; import { getServerSession } from "next-auth"; import { revalidatePath } from "next/cache"; import { authOptions } from "@/lib/auth"; import { upsertReviewSchema } from "./schemas"; import { reviewService } from "@/server/services/review.service"; export async function upsertTripReviewAction(formData: FormData) { const session = await getServerSession(authOptions); if (!session?.user) { return { error: "Kamu harus login terlebih dahulu" }; } const raw = { tripId: formData.get("tripId") as string, rating: formData.get("rating") as string, comment: String(formData.get("comment") ?? ""), }; const parsed = upsertReviewSchema.safeParse(raw); if (!parsed.success) { return { error: parsed.error.issues[0]?.message ?? "Data tidak valid" }; } try { await reviewService.upsertReview( parsed.data.tripId, session.user.id, { rating: parsed.data.rating, comment: parsed.data.comment, } ); revalidatePath(`/trips/${parsed.data.tripId}`); revalidatePath("/profile"); return { success: true }; } catch (err) { return { error: (err as Error).message }; } }