- ✅
- ✅ - ✅ - ✅
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
import { createTripSchema, tripImageUrlsSchema } from "./schemas";
|
||||
import {
|
||||
createTripSchema,
|
||||
itineraryItemsSchema,
|
||||
tripImageUrlsSchema,
|
||||
} from "./schemas";
|
||||
import { tripService } from "@/server/services/trip.service";
|
||||
import { organizerService } from "@/server/services/organizer.service";
|
||||
import { revalidatePath } from "next/cache";
|
||||
@@ -21,7 +25,6 @@ export async function createTripAction(formData: FormData) {
|
||||
destination: formData.get("destination") as string,
|
||||
location: formData.get("location") as string,
|
||||
meetingPoint: formData.get("meetingPoint") as string,
|
||||
itinerary: formData.get("itinerary") as string,
|
||||
whatsIncluded: formData.get("whatsIncluded") as string,
|
||||
whatsExcluded: formData.get("whatsExcluded") as string,
|
||||
date: formData.get("date") as string,
|
||||
@@ -36,6 +39,22 @@ export async function createTripAction(formData: FormData) {
|
||||
return { error: result.error.issues[0].message };
|
||||
}
|
||||
|
||||
const itineraryJson = formData.get("itineraryItems");
|
||||
let itineraryItems: ReturnType<typeof itineraryItemsSchema.parse> = [];
|
||||
if (typeof itineraryJson === "string" && itineraryJson.trim().length > 0) {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(itineraryJson);
|
||||
} catch {
|
||||
return { error: "Format itinerary tidak valid" };
|
||||
}
|
||||
const itineraryParsed = itineraryItemsSchema.safeParse(parsed);
|
||||
if (!itineraryParsed.success) {
|
||||
return { error: itineraryParsed.error.issues[0].message };
|
||||
}
|
||||
itineraryItems = itineraryParsed.data;
|
||||
}
|
||||
|
||||
if (result.data.price > 0) {
|
||||
const approved = await organizerService.isApproved(session.user.id);
|
||||
if (!approved) {
|
||||
@@ -69,7 +88,6 @@ export async function createTripAction(formData: FormData) {
|
||||
try {
|
||||
const {
|
||||
meetingPoint,
|
||||
itinerary,
|
||||
whatsIncluded,
|
||||
whatsExcluded,
|
||||
...tripCore
|
||||
@@ -78,13 +96,13 @@ export async function createTripAction(formData: FormData) {
|
||||
const trip = await tripService.createTrip({
|
||||
...tripCore,
|
||||
meetingPoint,
|
||||
itinerary,
|
||||
whatsIncluded,
|
||||
whatsExcluded,
|
||||
date,
|
||||
endDate,
|
||||
organizerId: session.user.id,
|
||||
imageUrls: imageUrls.length > 0 ? imageUrls : undefined,
|
||||
itineraryItems: itineraryItems.length > 0 ? itineraryItems : undefined,
|
||||
});
|
||||
revalidatePath("/trips");
|
||||
revalidatePath("/");
|
||||
|
||||
Reference in New Issue
Block a user