feature: notifikasi pengumuman setelah login
Deskripsi: - ui notification announcement - api findmany notification announcement - nb : masih setiap reload home tampil (seharusnya setelah login aja) No Issues
This commit is contained in:
45
src/app/api/notification/route.ts
Normal file
45
src/app/api/notification/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE NOTIFIKASI PENGUMUMAN
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const user = await funGetUserByCookies();
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
|
||||
const announcements = await prisma.notifications.findMany({
|
||||
skip: 0,
|
||||
take: 1,
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserTo: user.id,
|
||||
isRead: false,
|
||||
category: 'announcement'
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: 'desc'
|
||||
}
|
||||
]
|
||||
|
||||
});
|
||||
|
||||
const allData = announcements.map((v: any) => ({
|
||||
..._.omit(v, ["createdAt"]),
|
||||
createdAt: moment(v.createdAt).format("ll")
|
||||
}))
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan notifikasi", data: allData, }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user