Fix notifikasi API for mobile

Fix:
modified:   src/app/api/mobile/notification/[id]/route.ts
modified:   src/app/api/mobile/notification/[id]/unread-count/route.ts
modified:   src/app/api/mobile/notification/route.ts

### No Issue
This commit is contained in:
2025-12-24 17:47:00 +08:00
parent d3d4912a5f
commit d50fda90e0
3 changed files with 47 additions and 224 deletions

View File

@@ -1,4 +1,5 @@
import { prisma } from "@/lib";
import _ from "lodash";
import { NextRequest, NextResponse } from "next/server";
export async function GET(
@@ -8,33 +9,22 @@ export async function GET(
const { id } = params;
const { searchParams } = new URL(request.url);
const category = searchParams.get("category");
let fixData;
const fixCategory = _.upperCase(category || "");
try {
let fixData;
const data = await prisma.notifikasi.findMany({
orderBy: {
createdAt: "desc",
},
where: {
recipientId: id,
kategoriApp: fixCategory,
},
});
if (category === "count-as-unread") {
const data = await prisma.notifikasi.findMany({
where: {
userId: id,
isRead: false,
},
});
fixData = data.length;
} else if (category === "all") {
const data = await prisma.notifikasi.findMany({
where: {
userId: id,
},
});
fixData = data;
} else {
return NextResponse.json({
success: false,
message: "Invalid category",
});
}
fixData = data;
return NextResponse.json({
success: true,
@@ -47,3 +37,31 @@ export async function GET(
);
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const { id } = params;
try {
await prisma.notifikasi.update({
where: {
id: id,
},
data: {
isRead: true,
},
});
return NextResponse.json({
success: true,
message: "Notifications marked as read",
});
} catch (error) {
return NextResponse.json(
{ error: (error as Error).message },
{ status: 500 }
);
}
}