upd: notif pengumuman setelah login
Deskripsi: - mengupdate set cookies - mengupdate get cookies' No Issues
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { funGetUserByCookies, funSetCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import "moment/locale/id";
|
||||
@@ -14,30 +14,42 @@ export async function GET(request: Request) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (user.showNotification == true) {
|
||||
const announcements = await prisma.notifications.findMany({
|
||||
skip: 0,
|
||||
take: 1,
|
||||
where: {
|
||||
isActive: true,
|
||||
idUserTo: user.id,
|
||||
isRead: false,
|
||||
category: 'announcement'
|
||||
},
|
||||
orderBy: [
|
||||
{
|
||||
createdAt: 'desc'
|
||||
}
|
||||
]
|
||||
|
||||
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")
|
||||
}))
|
||||
|
||||
await funSetCookies({ user: user.id, showNotification: false })
|
||||
|
||||
if (announcements.length > 0) {
|
||||
return NextResponse.json({ show: true, message: "Berhasil mendapatkan notifikasi", data: allData }, { status: 200 });
|
||||
} else {
|
||||
return NextResponse.json({ show: false, message: "Tidak ada notifikasi pengumuman" }, { status: 200 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ show: false, message: "Notifikasi telah tampil" }, { status: 200 });
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
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