announcement

This commit is contained in:
alma 2025-05-04 21:45:24 +02:00
parent 4f63014500
commit 79cd8172e3
2 changed files with 44 additions and 4 deletions

View File

@ -72,6 +72,21 @@ export async function POST(req: NextRequest) {
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
}
// Verify user exists in database
console.log("Verifying user ID:", session.user.id);
const user = await prisma.user.findUnique({
where: { id: session.user.id }
});
if (!user) {
console.error("Author not found in database:", session.user.id);
return NextResponse.json({
error: "Author not found",
details: "The user account does not exist in the database"
}, { status: 400 });
}
// Create new announcement
const announcement = await prisma.announcement.create({
data: {
@ -85,6 +100,11 @@ export async function POST(req: NextRequest) {
return NextResponse.json(announcement, { status: 201 });
} catch (error) {
console.error("Error creating announcement:", error);
return NextResponse.json({ error: "Failed to create announcement" }, { status: 500 });
// Return more detailed error information
return NextResponse.json({
error: "Failed to create announcement",
details: error.message || "Unknown error",
code: error.code
}, { status: 500 });
}
}

View File

@ -30,9 +30,10 @@ import {
CardHeader,
CardTitle
} from "@/components/ui/card";
import { CheckIcon, Loader2 } from "lucide-react";
import { CheckIcon, Loader2, AlertCircle } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { useToast } from "@/components/ui/use-toast";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
// Form schema
const formSchema = z.object({
@ -49,6 +50,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
const [selectedRoles, setSelectedRoles] = useState<string[]>([]);
const [isSubmitting, setIsSubmitting] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
const [errorMsg, setErrorMsg] = useState<string | null>(null);
const { toast } = useToast();
// Initialize form
@ -99,6 +101,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
// Form submission
const onSubmit = async (data: z.infer<typeof formSchema>) => {
setIsSubmitting(true);
setErrorMsg(null);
try {
// Send the data to the API
@ -110,8 +113,17 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
body: JSON.stringify(data),
});
// Parse response, even if it's an error
const result = await response.json();
if (!response.ok) {
throw new Error('Failed to create announcement');
let errorMessage = result.error || 'Failed to create announcement';
if (result.details) {
errorMessage += `: ${result.details}`;
}
console.error("API Error:", result);
setErrorMsg(errorMessage);
throw new Error(errorMessage);
}
// Reset form and show success message
@ -130,7 +142,7 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
console.error("Error submitting announcement:", error);
toast({
title: "Error",
description: "Failed to create the announcement. Please try again.",
description: error instanceof Error ? error.message : "Failed to create the announcement. Please try again.",
variant: "destructive",
});
} finally {
@ -147,6 +159,14 @@ export function AnnouncementForm({ userRole }: AnnouncementFormProps) {
</CardDescription>
</CardHeader>
<CardContent>
{errorMsg && (
<Alert variant="destructive" className="mb-6">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Error</AlertTitle>
<AlertDescription>{errorMsg}</AlertDescription>
</Alert>
)}
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField