102 lines
2.6 KiB
TypeScript
102 lines
2.6 KiB
TypeScript
"use server"
|
|
|
|
import { revalidatePath } from "next/cache"
|
|
|
|
import { auth } from "@/auth"
|
|
import { isAdminEmail } from "@/lib/admin-auth"
|
|
import {
|
|
updateInvitationLanguage,
|
|
updateSaveTheDateSent,
|
|
} from "@/lib/db/queries/invitations"
|
|
import {
|
|
updateInvitationLanguageSchema,
|
|
updateSaveTheDateSentSchema,
|
|
} from "@/lib/validation/invitation"
|
|
|
|
export interface UpdateSaveTheDateSentResult {
|
|
ok: boolean
|
|
error: string | null
|
|
}
|
|
|
|
export interface UpdateInvitationLanguageResult {
|
|
ok: boolean
|
|
error: string | null
|
|
}
|
|
|
|
export async function updateSaveTheDateSentAction(input: {
|
|
invitationId: string
|
|
sent: boolean
|
|
}): Promise<UpdateSaveTheDateSentResult> {
|
|
const session = await auth()
|
|
const adminEmail = session?.user?.email
|
|
|
|
if (!isAdminEmail(adminEmail)) {
|
|
return { ok: false, error: "You are not allowed to update invitations." }
|
|
}
|
|
|
|
const parsed = updateSaveTheDateSentSchema.safeParse(input)
|
|
|
|
if (!parsed.success) {
|
|
return { ok: false, error: "Could not update save-the-date status." }
|
|
}
|
|
|
|
try {
|
|
const updated = await updateSaveTheDateSent({
|
|
invitationId: parsed.data.invitationId,
|
|
sent: parsed.data.sent,
|
|
adminEmail: adminEmail ?? "",
|
|
})
|
|
|
|
if (!updated) {
|
|
return { ok: false, error: "This guest group no longer exists." }
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to update save-the-date status", error)
|
|
return { ok: false, error: "Could not update save-the-date status." }
|
|
}
|
|
|
|
revalidatePath("/admin/guests")
|
|
revalidatePath("/admin")
|
|
|
|
return { ok: true, error: null }
|
|
}
|
|
|
|
export async function updateInvitationLanguageAction(input: {
|
|
invitationId: string
|
|
language: string
|
|
}): Promise<UpdateInvitationLanguageResult> {
|
|
const session = await auth()
|
|
const adminEmail = session?.user?.email
|
|
|
|
if (!isAdminEmail(adminEmail)) {
|
|
return { ok: false, error: "You are not allowed to update invitations." }
|
|
}
|
|
|
|
const parsed = updateInvitationLanguageSchema.safeParse(input)
|
|
|
|
if (!parsed.success) {
|
|
return { ok: false, error: "Select a supported language." }
|
|
}
|
|
|
|
try {
|
|
const updated = await updateInvitationLanguage({
|
|
invitationId: parsed.data.invitationId,
|
|
language: parsed.data.language,
|
|
adminEmail: adminEmail ?? "",
|
|
})
|
|
|
|
if (!updated) {
|
|
return { ok: false, error: "This guest group no longer exists." }
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to update invitation language", error)
|
|
return { ok: false, error: "Could not update the invitation language." }
|
|
}
|
|
|
|
revalidatePath(`/admin/guests/${parsed.data.invitationId}`)
|
|
revalidatePath("/admin/guests")
|
|
revalidatePath("/admin")
|
|
|
|
return { ok: true, error: null }
|
|
}
|