Skip to content

Commit 5e233c8

Browse files
committed
401 error
1 parent 7e821e5 commit 5e233c8

File tree

2 files changed

+62
-65
lines changed

2 files changed

+62
-65
lines changed

src/app/api/v1/admin/notifications/[notificationId]/route.ts

+39-43
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ export async function GET(
2020
const params = await props.params;
2121
const session = await getServerSession();
2222

23-
if (isAdmin(session?.user?.email || "")) {
24-
// Signed in
25-
const notificationId = params.notificationId;
26-
try {
27-
const notificationItem =
28-
await getNotificationByNotificationId(notificationId);
29-
return NextResponse.json(notificationItem);
30-
} catch (e) {
31-
logger.error(e);
32-
return NextResponse.json({ success: false }, { status: 500 });
33-
}
34-
} else {
35-
return NextResponse.json({ success: false }, { status: 401 });
23+
if (!isAdmin(session?.user?.email || "")) {
24+
return NextResponse.json({ error: "Not an admin user" }, { status: 401 });
25+
}
26+
27+
const notificationId = params.notificationId;
28+
try {
29+
const notificationItem =
30+
await getNotificationByNotificationId(notificationId);
31+
return NextResponse.json(notificationItem);
32+
} catch (e) {
33+
logger.error(e);
34+
return NextResponse.json({ success: false }, { status: 500 });
3635
}
3736
}
3837

@@ -44,18 +43,17 @@ export async function DELETE(
4443
) {
4544
const params = await props.params;
4645
const session = await getServerSession();
47-
if (isAdmin(session?.user?.email || "")) {
48-
// Signed in
49-
const notificationId = params.notificationId;
50-
try {
51-
const notificationItem = await deleteNotification(notificationId);
52-
return NextResponse.json(notificationItem);
53-
} catch (e) {
54-
logger.error(e);
55-
return NextResponse.json({ success: false }, { status: 500 });
56-
}
57-
} else {
58-
return NextResponse.json({ success: false }, { status: 401 });
46+
if (!isAdmin(session?.user?.email || "")) {
47+
return NextResponse.json({ error: "Not an admin user" }, { status: 401 });
48+
}
49+
50+
const notificationId = params.notificationId;
51+
try {
52+
const notificationItem = await deleteNotification(notificationId);
53+
return NextResponse.json(notificationItem);
54+
} catch (e) {
55+
logger.error(e);
56+
return NextResponse.json({ success: false }, { status: 500 });
5957
}
6058
}
6159

@@ -66,24 +64,22 @@ export async function PUT(
6664
const params = await props.params;
6765
const { notificationId } = params;
6866
const session = await getServerSession();
67+
if (!isAdmin(session?.user?.email || "")) {
68+
return NextResponse.json({ error: "Not an admin user" }, { status: 401 });
69+
}
70+
try {
71+
const updatedData: NotificationRow = await req.json();
72+
const updatedNotification = await updateNotification(
73+
notificationId,
74+
updatedData,
75+
);
6976

70-
if (isAdmin(session?.user?.email || "")) {
71-
try {
72-
const updatedData: NotificationRow = await req.json();
73-
const updatedNotification = await updateNotification(
74-
notificationId,
75-
updatedData,
76-
);
77-
78-
return NextResponse.json(updatedNotification, { status: 200 });
79-
} catch (error) {
80-
console.error("Error updating notification:", error);
81-
return NextResponse.json(
82-
{ error: "Internal Server Error" },
83-
{ status: 500 },
84-
);
85-
}
86-
} else {
87-
return NextResponse.json({ success: false }, { status: 401 });
77+
return NextResponse.json(updatedNotification, { status: 200 });
78+
} catch (error) {
79+
console.error("Error updating notification:", error);
80+
return NextResponse.json(
81+
{ error: "Internal Server Error" },
82+
{ status: 500 },
83+
);
8884
}
8985
}

src/app/api/v1/admin/notifications/route.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import createDbConnection from "../../../../../db/connect";
88
import { getAllNotifications } from "../../../../../db/tables/notifications";
99
import { getServerSession } from "../../../../functions/server/getServerSession";
1010
import { isAdmin } from "../../../utils/auth";
11-
import { logger } from "../../../../functions/server/logging";
1211

1312
const knex = createDbConnection();
1413

@@ -17,7 +16,7 @@ export async function GET() {
1716
console.log("session from default route: ", session);
1817

1918
if (!isAdmin(session?.user?.email || "")) {
20-
logger.error("Not an admin user");
19+
return NextResponse.json({ error: "Not an admin user" }, { status: 401 });
2120
}
2221

2322
try {
@@ -34,28 +33,30 @@ export async function GET() {
3433

3534
export async function POST(req: NextRequest) {
3635
const session = await getServerSession();
37-
if (isAdmin(session?.user?.email || "")) {
38-
try {
39-
const newNotification: NotificationRow = await req.json();
40-
41-
const [addedNotification] = await knex("notifications")
42-
.insert(newNotification)
43-
.returning("*");
44-
45-
if (!addedNotification) {
46-
return NextResponse.json(
47-
{ error: "Failed to insert notification" },
48-
{ status: 400 },
49-
);
50-
}
51-
52-
return NextResponse.json(addedNotification, { status: 201 });
53-
} catch (error) {
54-
console.error("Error adding notification:", error);
36+
if (!isAdmin(session?.user?.email || "")) {
37+
return NextResponse.json({ error: "Not an admin user" }, { status: 401 });
38+
}
39+
40+
try {
41+
const newNotification: NotificationRow = await req.json();
42+
43+
const [addedNotification] = await knex("notifications")
44+
.insert(newNotification)
45+
.returning("*");
46+
47+
if (!addedNotification) {
5548
return NextResponse.json(
56-
{ error: "Internal Server Error" },
57-
{ status: 500 },
49+
{ error: "Failed to insert notification" },
50+
{ status: 400 },
5851
);
5952
}
53+
54+
return NextResponse.json(addedNotification, { status: 201 });
55+
} catch (error) {
56+
console.error("Error adding notification:", error);
57+
return NextResponse.json(
58+
{ error: "Internal Server Error" },
59+
{ status: 500 },
60+
);
6061
}
6162
}

0 commit comments

Comments
 (0)