Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update status api #75

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions suncityla/app/api/bookings/[bookingId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/prisma/prismaClient";


const updateBookingStatus = async (bookingId: string, newStatus: string) => {
const validStatuses = ["PENDING", "CONFIRMED", "CANCELLED", "VISITED"];

if (!validStatuses.includes(newStatus)) {
throw new Error("Invalid status");
}

const updatedBooking = await prisma.booking.update({
where: { id: bookingId },
data: { status: newStatus as "PENDING" | "CONFIRMED" | "CANCELLED" | "VISITED" },
});

return updatedBooking;
};

export async function PUT(req: NextRequest) {
const { pathname } = new URL(req.url);
const bookingId = pathname.split("/")[3];

if (!bookingId) {
return NextResponse.json({ message: "Booking id is required" }, { status: 400 });
}

try {
const body = await req.json();
const { status } = body;

if (!status || typeof status !== "string") {
return NextResponse.json({ message: "Invalid status" }, { status: 400 });
}

const updatedBooking = await updateBookingStatus(bookingId, status);
return NextResponse.json({
message: "Booking status updated successfully",
booking: updatedBooking,
});
} catch (error) {
console.error("Error updating booking status:", error);
return NextResponse.json(
{ message: error instanceof Error ? error.message : "Internal server error" },
{ status: 500 }
);
}
}
1 change: 1 addition & 0 deletions suncityla/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ enum BookingStatus {
PENDING
CONFIRMED
CANCELLED
VISITED
}