upd: notif pengumuman setelah login

Deskripsi:
- mengupdate set cookies
- mengupdate get cookies'

No Issues
This commit is contained in:
amel
2025-04-08 15:04:28 +08:00
parent bba7ddaff3
commit 75b75ea5e8
5 changed files with 42 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
import { prisma } from "@/module/_global"; import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserByCookies, funSetCookies } from "@/module/auth";
import _ from "lodash"; import _ from "lodash";
import moment from "moment"; import moment from "moment";
import "moment/locale/id"; 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 }); 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, const allData = announcements.map((v: any) => ({
where: { ..._.omit(v, ["createdAt"]),
isActive: true, createdAt: moment(v.createdAt).format("ll")
idUserTo: user.id, }))
isRead: false,
category: 'announcement' await funSetCookies({ user: user.id, showNotification: false })
},
orderBy: [ if (announcements.length > 0) {
{ return NextResponse.json({ show: true, message: "Berhasil mendapatkan notifikasi", data: allData }, { status: 200 });
createdAt: 'desc' } 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) { } catch (error) {
console.error(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 }); return NextResponse.json({ success: false, message: "Gagal mendapatkan notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });

View File

@@ -6,13 +6,13 @@ import { cookies } from "next/headers";
export default async function funGetUserByCookies() { export default async function funGetUserByCookies() {
const sessionCookie = cookies().get("sessionCookieSDM"); const sessionCookie = cookies().get("sessionCookieSDM");
const userId = await unsealData(sessionCookie!.value, { const userId : any = await unsealData(sessionCookie!.value, {
password: pwd_key_config, password: pwd_key_config,
}); });
const user = await prisma.user.findUnique({ const user = await prisma.user.findUnique({
where: { where: {
id: String(userId), id: userId.user ? String(userId.user) : String(userId),
}, },
}); });
@@ -35,6 +35,7 @@ export default async function funGetUserByCookies() {
idVillage: user?.idVillage, idVillage: user?.idVillage,
idGroup: user?.idGroup, idGroup: user?.idGroup,
idPosition: user?.idPosition, idPosition: user?.idPosition,
theme: warna theme: warna,
showNotification: userId?.showNotification
}; };
} }

View File

@@ -4,9 +4,9 @@ import { cookies } from "next/headers";
import { prisma, pwd_key_config } from "@/module/_global"; import { prisma, pwd_key_config } from "@/module/_global";
import { createLogUser } from "@/module/user"; import { createLogUser } from "@/module/user";
export default async function funSetCookies({ user }: { user: string }) { export default async function funSetCookies({ user, showNotification }: { user: string, showNotification: boolean }) {
try { try {
const encryptedUserData = await sealData(user, { password: pwd_key_config }); const encryptedUserData = await sealData({user, showNotification}, { password: pwd_key_config });
// data user // data user
const dataUser = await prisma.user.findUnique({ const dataUser = await prisma.user.findUnique({

View File

@@ -37,7 +37,7 @@ export default function ViewVerification({ phone, otp, user }: IVerification) {
async function getVerification() { async function getVerification() {
setLoading(true) setLoading(true)
if (isOTP == inputOTP) { if (isOTP == inputOTP) {
const setCookies: any = await funSetCookies({ user: user }) const setCookies: any = await funSetCookies({ user: user, showNotification: true })
if (setCookies.success) { if (setCookies.success) {
toast.success(setCookies.message) toast.success(setCookies.message)

View File

@@ -15,15 +15,12 @@ export default function NotificationAnnouncement() {
async function fetchData() { async function fetchData() {
try { try {
const res = await funGetOneNotificationAnnouncement(); const res = await funGetOneNotificationAnnouncement();
if (res.success) { if (res.show) {
setTampilNotif(true) setTampilNotif(true)
setData(res.data); setData(res.data)
} else {
toast.error(res.message);
} }
} catch (error) { } catch (error) {
console.error(error); console.error(error);
toast.error("Gagal memuat data, coba lagi nanti");
} finally { } finally {
setTimeout(() => { setTimeout(() => {
setTampilNotif(false); setTampilNotif(false);