Files
cat-sitting-planner/app/actions/plan.ts

38 lines
1.1 KiB
TypeScript

"use server"
import prisma from "@/lib/prisma"
import { revalidatePath } from "next/cache"
import { headers } from "next/headers"
import { sendNotification } from "@/lib/notifications"
import { getDictionary } from "@/get-dictionary"
export async function updatePlan(
planId: string,
data: { instructions?: string; webhookUrl?: string; notifyAll?: boolean },
lang: string = "en"
) {
const dict = await getDictionary(lang as any)
const plan = await prisma.plan.update({
where: { id: planId },
data: {
instructions: data.instructions,
webhookUrl: data.webhookUrl,
notifyAll: data.notifyAll,
}
})
if (data.instructions && plan.webhookUrl && plan.notifyAll) {
const host = (await headers()).get("host")
const protocol = host?.includes("localhost") ? "http" : "https"
const planUrl = `${protocol}://${host}/${lang}/dashboard/${planId}`
await sendNotification(
plan.webhookUrl,
dict.notifications.instructionsUpdated.replace("{url}", planUrl)
)
}
revalidatePath(`/${lang}/dashboard/${planId}`)
}