feat(carbet): add lastBookedAt and endpoint
This commit is contained in:
parent
8d36f7008f
commit
fcc2749d1d
5 changed files with 63 additions and 2 deletions
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE "Carbet"
|
||||
ADD COLUMN "lastBookedAt" TIMESTAMP(3);
|
||||
|
|
@ -109,6 +109,7 @@ model Carbet {
|
|||
pirogueDurationMin Int
|
||||
capacity Int
|
||||
status CarbetStatus @default(DRAFT)
|
||||
lastBookedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
|
|
|
|||
25
src/app/api/carbets/[carbetId]/last-booked/route.ts
Normal file
25
src/app/api/carbets/[carbetId]/last-booked/route.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import { NextResponse } from "next/server";
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ carbetId: string }> },
|
||||
) {
|
||||
const { carbetId } = await params;
|
||||
|
||||
const carbet = await prisma.carbet.findUnique({
|
||||
where: { id: carbetId },
|
||||
select: { lastBookedAt: true },
|
||||
});
|
||||
|
||||
if (!carbet) {
|
||||
return NextResponse.json({ error: "Carbet introuvable." }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
lastBookedAt: carbet.lastBookedAt?.toISOString() ?? null,
|
||||
});
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import {
|
|||
PaymentStatus,
|
||||
SubscriptionStatus,
|
||||
} from "@/generated/prisma/enums";
|
||||
import { refreshCarbetLastBookedAt } from "@/lib/carbet-last-booked";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { fromStripeTimestamp, getStripeClient } from "@/lib/stripe";
|
||||
|
||||
|
|
@ -36,13 +37,17 @@ async function handleCheckoutCompleted(session: Stripe.Checkout.Session) {
|
|||
const type = session.metadata?.type;
|
||||
|
||||
if (type === "booking" && bookingId) {
|
||||
await prisma.booking.update({
|
||||
const booking = await prisma.booking.update({
|
||||
where: { id: bookingId },
|
||||
data: {
|
||||
paymentStatus: PaymentStatus.SUCCEEDED,
|
||||
status: BookingStatus.CONFIRMED,
|
||||
},
|
||||
select: {
|
||||
carbetId: true,
|
||||
},
|
||||
});
|
||||
await refreshCarbetLastBookedAt(booking.carbetId);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -78,13 +83,17 @@ async function handlePaymentIntentFailed(paymentIntent: Stripe.PaymentIntent) {
|
|||
return;
|
||||
}
|
||||
|
||||
await prisma.booking.update({
|
||||
const booking = await prisma.booking.update({
|
||||
where: { id: bookingId },
|
||||
data: {
|
||||
paymentStatus: PaymentStatus.FAILED,
|
||||
status: BookingStatus.CANCELLED,
|
||||
},
|
||||
select: {
|
||||
carbetId: true,
|
||||
},
|
||||
});
|
||||
await refreshCarbetLastBookedAt(booking.carbetId);
|
||||
}
|
||||
|
||||
async function handleSubscriptionUpdated(subscription: Stripe.Subscription) {
|
||||
|
|
|
|||
24
src/lib/carbet-last-booked.ts
Normal file
24
src/lib/carbet-last-booked.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { BookingStatus } from "@/generated/prisma/enums";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export async function refreshCarbetLastBookedAt(carbetId: string): Promise<void> {
|
||||
const latestConfirmedBooking = await prisma.booking.findFirst({
|
||||
where: {
|
||||
carbetId,
|
||||
status: BookingStatus.CONFIRMED,
|
||||
},
|
||||
orderBy: {
|
||||
updatedAt: "desc",
|
||||
},
|
||||
select: {
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.carbet.update({
|
||||
where: { id: carbetId },
|
||||
data: {
|
||||
lastBookedAt: latestConfirmedBooking?.updatedAt ?? null,
|
||||
},
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue