Initial commit: Cat Sitting Planner with PWA, SQLite, and Webhook Notifications
This commit is contained in:
62
app/actions/booking.ts
Normal file
62
app/actions/booking.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
"use server"
|
||||
|
||||
import prisma from "@/lib/prisma"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import { sendNotification } from "@/lib/notifications"
|
||||
|
||||
export async function createBooking(planId: string, date: Date, name: string, type: "SITTER" | "OWNER_HOME" = "SITTER") {
|
||||
// Simple check to ensure no double booking on server side
|
||||
const existing = await prisma.booking.findFirst({
|
||||
where: {
|
||||
planId,
|
||||
date: date,
|
||||
}
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
throw new Error("Day is already booked")
|
||||
}
|
||||
|
||||
const plan = await prisma.plan.findUnique({
|
||||
where: { id: planId }
|
||||
})
|
||||
|
||||
await prisma.booking.create({
|
||||
data: {
|
||||
planId,
|
||||
date,
|
||||
sitterName: name,
|
||||
type
|
||||
}
|
||||
})
|
||||
|
||||
if (plan?.webhookUrl && plan.notifyAll) {
|
||||
const dateStr = date.toLocaleDateString()
|
||||
const message = type === "OWNER_HOME"
|
||||
? `🏠 OWNER HOME: Marked for ${dateStr}.`
|
||||
: `✅ NEW BOOKING: ${name} is sitting on ${dateStr}.`
|
||||
await sendNotification(plan.webhookUrl, message)
|
||||
}
|
||||
|
||||
revalidatePath(`/dashboard/${planId}`)
|
||||
}
|
||||
|
||||
export async function deleteBooking(bookingId: number, planId: string) {
|
||||
const booking = await prisma.booking.findUnique({
|
||||
where: { id: bookingId },
|
||||
include: { plan: true }
|
||||
})
|
||||
|
||||
if (!booking) return
|
||||
|
||||
await prisma.booking.delete({
|
||||
where: { id: bookingId }
|
||||
})
|
||||
|
||||
if (booking.plan.webhookUrl) {
|
||||
const dateStr = booking.date.toLocaleDateString()
|
||||
await sendNotification(booking.plan.webhookUrl, `🚨 CANCELLATION: ${booking.sitterName} removed their booking for ${dateStr}.`)
|
||||
}
|
||||
|
||||
revalidatePath(`/dashboard/${planId}`)
|
||||
}
|
||||
Reference in New Issue
Block a user