announcement
This commit is contained in:
parent
79cd8172e3
commit
ee42151521
@ -3,6 +3,22 @@ import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
/**
|
||||
* Check if a user exists in the database
|
||||
*/
|
||||
async function userExists(userId: string): Promise<boolean> {
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true }
|
||||
});
|
||||
return !!user;
|
||||
} catch (error) {
|
||||
console.error(`Error checking if user exists:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// GET - Retrieve a specific announcement
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
@ -40,7 +56,7 @@ export async function GET(
|
||||
|
||||
const hasAccess =
|
||||
announcement.targetRoles.includes("all") ||
|
||||
announcement.targetRoles.some(role => roles.includes(role));
|
||||
announcement.targetRoles.some((role: string) => roles.includes(role));
|
||||
|
||||
if (!hasAccess) {
|
||||
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
@ -49,7 +65,11 @@ export async function GET(
|
||||
return NextResponse.json(announcement);
|
||||
} catch (error) {
|
||||
console.error("Error fetching announcement:", error);
|
||||
return NextResponse.json({ error: "Failed to fetch announcement" }, { status: 500 });
|
||||
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||
return NextResponse.json({
|
||||
error: "Failed to fetch announcement",
|
||||
details: errorMessage
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,6 +85,17 @@ export async function DELETE(
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Verify user exists in database
|
||||
const userExistsInDB = await userExists(session.user.id);
|
||||
|
||||
if (!userExistsInDB) {
|
||||
console.error("User not found in database:", session.user.id);
|
||||
return NextResponse.json({
|
||||
error: "User not found",
|
||||
details: `The user ID from your session (${session.user.id}) doesn't exist in the database.`
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if user has admin, entrepreneurship, or communication role
|
||||
const userRole = session.user.role || [];
|
||||
const roles = Array.isArray(userRole) ? userRole : [userRole];
|
||||
@ -95,6 +126,13 @@ export async function DELETE(
|
||||
return NextResponse.json({ message: "Announcement deleted successfully" });
|
||||
} catch (error) {
|
||||
console.error("Error deleting announcement:", error);
|
||||
return NextResponse.json({ error: "Failed to delete announcement" }, { status: 500 });
|
||||
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||
const errorCode = error.code || "UNKNOWN";
|
||||
|
||||
return NextResponse.json({
|
||||
error: "Failed to delete announcement",
|
||||
details: errorMessage,
|
||||
code: errorCode
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,22 @@ import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
/**
|
||||
* Check if a user exists in the database
|
||||
*/
|
||||
async function userExists(userId: string): Promise<boolean> {
|
||||
try {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { id: true }
|
||||
});
|
||||
return !!user;
|
||||
} catch (error) {
|
||||
console.error(`Error checking if user exists:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// GET - Retrieve all announcements (with role filtering)
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
@ -72,18 +88,16 @@ export async function POST(req: NextRequest) {
|
||||
return NextResponse.json({ error: "Missing required fields" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Verify user exists in database
|
||||
// Verify user exists in database (using session user id)
|
||||
console.log("Verifying user ID:", session.user.id);
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: session.user.id }
|
||||
});
|
||||
const userExistsInDB = await userExists(session.user.id);
|
||||
|
||||
if (!user) {
|
||||
console.error("Author not found in database:", session.user.id);
|
||||
if (!userExistsInDB) {
|
||||
console.error("User not found in database:", session.user.id);
|
||||
return NextResponse.json({
|
||||
error: "Author not found",
|
||||
details: "The user account does not exist in the database"
|
||||
error: "User not found",
|
||||
details: `The user ID from your session (${session.user.id}) doesn't exist in the database. This may be due to a session/database mismatch or the user hasn't been synced to the application database.`
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
@ -101,10 +115,13 @@ export async function POST(req: NextRequest) {
|
||||
} catch (error) {
|
||||
console.error("Error creating announcement:", error);
|
||||
// Return more detailed error information
|
||||
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
||||
const errorCode = error.code || "UNKNOWN";
|
||||
|
||||
return NextResponse.json({
|
||||
error: "Failed to create announcement",
|
||||
details: error.message || "Unknown error",
|
||||
code: error.code
|
||||
details: errorMessage,
|
||||
code: errorCode
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user