Files

51 lines
1.5 KiB
TypeScript

"use server"
import prisma from "@/lib/prisma"
import { revalidatePath } from "next/cache"
import { headers } from "next/headers"
import { sendPlanNotification } from "@/lib/notifications"
import { getDictionary } from "@/get-dictionary"
export async function updatePlan(
planId: string,
data: {
instructions?: string;
webhookUrl?: string;
notifyAll?: boolean;
title?: string;
feedingPerDay?: number;
feedingInterval?: number;
litterInterval?: number;
},
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,
title: data.title,
feedingPerDay: data.feedingPerDay,
feedingInterval: data.feedingInterval,
litterInterval: data.litterInterval,
}
})
if (data.instructions && plan.notifyAll) {
const host = (await headers()).get("host")
const protocol = host?.includes("localhost") ? "http" : "https"
const planUrl = `${protocol}://${host}/${lang}/dashboard/${planId}`
await sendPlanNotification(
planId,
dict.notifications.instructionsUpdated.replace("{url}", planUrl),
plan.webhookUrl
)
}
revalidatePath(`/${lang}/dashboard/${planId}`)
}