From 4e4fc7d1cf0a3829c816205dd7392b37c119737b Mon Sep 17 00:00:00 2001 From: amel Date: Thu, 26 Jun 2025 14:34:07 +0800 Subject: [PATCH] upd: api mobile deskripsi: - list notifikasi - read notifikasi No Issues --- src/app/api/mobile/home/notification/route.ts | 47 ++++++++++--------- src/app/api/mobile/route.ts | 5 +- src/app/api/mobile/user/[id]/route.ts | 10 +++- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/app/api/mobile/home/notification/route.ts b/src/app/api/mobile/home/notification/route.ts index 31e7116..445c078 100644 --- a/src/app/api/mobile/home/notification/route.ts +++ b/src/app/api/mobile/home/notification/route.ts @@ -1,6 +1,6 @@ import { prisma } from "@/module/_global"; -import { funGetUserByCookies } from "@/module/auth"; -import { createLogUser } from "@/module/user"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; import _ from "lodash"; import moment from "moment"; import "moment/locale/id"; @@ -9,21 +9,22 @@ import { NextResponse } from "next/server"; // GET ALL NOTIFIKASI 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 { searchParams } = new URL(request.url); + const user = searchParams.get('user'); const page = searchParams.get('page'); const dataSkip = Number(page) * 10 - 10; + const userMobile = await funGetUserById({ id: String(user) }); + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + const announcements = await prisma.notifications.findMany({ skip: dataSkip, take: 10, where: { isActive: true, - idUserTo: user.id + idUserTo: userMobile.id }, orderBy: [ { @@ -53,11 +54,13 @@ export async function GET(request: Request) { // UPDATE READ NOTIFIKASI export async function PUT(request: Request, context: { params: { id: string } }) { try { - const user = await funGetUserByCookies() - if (user.id == undefined) { - return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + const { id, user } = await request.json(); + const userMobile = await funGetUserById({ id: String(user) }); + + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } - const { id } = await request.json(); + const data = await prisma.notifications.count({ where: { id: id, @@ -70,7 +73,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) success: false, message: "Gagal mendapatkan data, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -84,13 +87,13 @@ export async function PUT(request: Request, context: { params: { id: string } }) }); // create log user - const log = await createLogUser({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id }) + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User membaca notifikasi', table: 'notifications', data: id, user: userMobile.id }) - return NextResponse.json({ success: true, message: "Berhasil mendapatkan notifikasi", }, { status: 200 }); + return NextResponse.json({ success: true, message: "Berhasil membaca notifikasi", }, { 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 }); + return NextResponse.json({ success: false, message: "Gagal membaca notifikasi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); } } @@ -98,14 +101,16 @@ export async function PUT(request: Request, context: { params: { id: string } }) // UPDATE READ ALL NOTIFICATION export async function POST(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 { user } = await request.json(); + const userMobile = await funGetUserById({ id: String(user) }); + + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } const upd = await prisma.notifications.updateMany({ where: { - idUserTo: user.id, + idUserTo: userMobile.id, isRead: false }, data: { @@ -114,7 +119,7 @@ export async function POST(request: Request) { }) // create log user - const log = await createLogUser({ act: 'UPDATE', desc: 'User menandai semua notifikasi', table: 'notifications', data: '' }) + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User menandai semua notifikasi', table: 'notifications', data: '', user: userMobile.id }) return NextResponse.json({ success: true, message: "Berhasil mengupdate notifikasi", }, { status: 200 }); diff --git a/src/app/api/mobile/route.ts b/src/app/api/mobile/route.ts index 124dca8..3a5f199 100644 --- a/src/app/api/mobile/route.ts +++ b/src/app/api/mobile/route.ts @@ -24,8 +24,9 @@ export async function GET(request: Request) { body: 'Check out the latest features in our app.', }, data: { - category: 'value1', - content: 'value2', + id: 'cm1eqwkwj00067j2bzejyh9u7', + category: 'division', + content: 'cm1eqwkvu00017j2b8qnisp9g', }, }; diff --git a/src/app/api/mobile/user/[id]/route.ts b/src/app/api/mobile/user/[id]/route.ts index 29dada5..0fffb77 100644 --- a/src/app/api/mobile/user/[id]/route.ts +++ b/src/app/api/mobile/user/[id]/route.ts @@ -42,6 +42,11 @@ export async function GET(request: Request, context: { params: { id: string } }) id: true }, }, + Village:{ + select:{ + name:true + } + } }, }); @@ -51,10 +56,11 @@ export async function GET(request: Request, context: { params: { id: string } }) const idUserRole = users?.UserRole.id const phone = users?.phone.substr(2) const role = users?.UserRole.name + const village = users?.Village.name - const result = { ...userData, group, position, idUserRole, phone, role }; + const result = { ...userData, group, position, idUserRole, phone, role, village }; - const omitData = _.omit(result, ["Group", "Position", "UserRole"]); + const omitData = _.omit(result, ["Group", "Position", "UserRole", "Village"]);