feat: invitation deletion
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"use server"
|
||||
|
||||
import { revalidatePath } from "next/cache"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
import { isAdminEmail } from "@/lib/admin-auth"
|
||||
import { deleteInvitationGroup } from "@/lib/db/queries/invitations"
|
||||
import { deleteUploadObject } from "@/lib/storage/uploads"
|
||||
import { deleteInvitationSchema } from "@/lib/validation/invitation"
|
||||
|
||||
export interface DeleteInvitationActionState {
|
||||
error: string | null
|
||||
}
|
||||
|
||||
export async function deleteInvitationAction(
|
||||
_previousState: DeleteInvitationActionState,
|
||||
formData: FormData,
|
||||
): Promise<DeleteInvitationActionState> {
|
||||
const session = await auth()
|
||||
const adminEmail = session?.user?.email
|
||||
|
||||
if (!isAdminEmail(adminEmail)) {
|
||||
return { error: "You are not allowed to delete invitations." }
|
||||
}
|
||||
|
||||
const authorizedAdminEmail = adminEmail ?? ""
|
||||
|
||||
const parsed = deleteInvitationSchema.safeParse({
|
||||
invitationId: formData.get("invitationId"),
|
||||
})
|
||||
|
||||
if (!parsed.success) {
|
||||
return { error: "Could not delete this guest group." }
|
||||
}
|
||||
|
||||
let deleted: Awaited<ReturnType<typeof deleteInvitationGroup>>
|
||||
|
||||
try {
|
||||
deleted = await deleteInvitationGroup(parsed.data.invitationId, authorizedAdminEmail)
|
||||
} catch (error) {
|
||||
console.error("Failed to delete invitation group", error)
|
||||
return { error: "Could not delete this guest group. Please try again." }
|
||||
}
|
||||
|
||||
if (!deleted) {
|
||||
return { error: "This guest group was already removed." }
|
||||
}
|
||||
|
||||
if (deleted.photoObjectKey) {
|
||||
try {
|
||||
await deleteUploadObject(deleted.photoObjectKey)
|
||||
} catch (error) {
|
||||
console.error("Failed to delete uploaded photo object", {
|
||||
invitationId: deleted.id,
|
||||
objectKey: deleted.photoObjectKey,
|
||||
error,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath("/admin/guests")
|
||||
redirect("/admin/guests?deleted=1")
|
||||
}
|
||||
Reference in New Issue
Block a user