From 589fa01bf6a99ad840b72183b95cf2c483dd88a3 Mon Sep 17 00:00:00 2001 From: amel Date: Wed, 21 May 2025 15:58:27 +0800 Subject: [PATCH] upd: api mobile Deskripsi: - update load divisi - tambah anggota - hapus anggota - update status admin divisi - info divisi - detail divisi No Issues --- .../api/mobile/division/[id]/detail/route.ts | 64 +++++++++---------- src/app/api/mobile/division/[id]/route.ts | 26 +++++--- src/app/api/mobile/division/report/route.ts | 16 +++-- 3 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/app/api/mobile/division/[id]/detail/route.ts b/src/app/api/mobile/division/[id]/detail/route.ts index c65c4fc..65b268d 100644 --- a/src/app/api/mobile/division/[id]/detail/route.ts +++ b/src/app/api/mobile/division/[id]/detail/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"; @@ -12,12 +12,13 @@ export async function GET(request: Request, context: { params: { id: string } }) try { let allData const { id } = context.params; - const user = await funGetUserByCookies() const { searchParams } = new URL(request.url); const kategori = searchParams.get("cat"); + const user = searchParams.get("user"); + const userMobile = await funGetUserById({ id: String(user) }) - if (user.id == undefined) { - return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } const data = await prisma.division.findUnique({ @@ -28,7 +29,7 @@ export async function GET(request: Request, context: { params: { id: string } }) }); if (!data) { - return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 }); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 }); } if (kategori == "jumlah") { @@ -244,18 +245,17 @@ export async function GET(request: Request, context: { params: { id: string } }) } - - // MENGELUARKAN ANGGOTA DARI DIVISI export async function DELETE(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 idDivision = context.params.id + const { id, user } = (await request.json()) - const idDivision = context.params.id; - const { id } = (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 data = await prisma.division.count({ where: { @@ -270,7 +270,7 @@ export async function DELETE(request: Request, context: { params: { id: string } success: false, message: "Hapus anggota divisi gagal, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -281,7 +281,7 @@ export async function DELETE(request: Request, context: { params: { id: string } }); // create log user - const log = await createLogUser({ act: 'DELETE', desc: 'User mengeluarkan anggota divisi', table: 'division', data: idDivision }) + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengeluarkan anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) return NextResponse.json( { @@ -300,13 +300,13 @@ export async function DELETE(request: Request, context: { params: { id: string } // MENGGANTI STATUS ADMIN DIVISI 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 idDivision = context.params.id; - const { id, isAdmin } = (await request.json()); + const { id, isAdmin, 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 data = await prisma.division.count({ where: { @@ -321,7 +321,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) success: false, message: "Perubahan status admin gagal, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -335,7 +335,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) }); // create log user - const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate status anggota divisi', table: 'division', data: idDivision }) + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) return NextResponse.json( { @@ -354,13 +354,13 @@ export async function PUT(request: Request, context: { params: { id: string } }) // TAMBAH ANGGOTA DIVISI export async function POST(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 { member, user } = await request.json() + const idDivision = context.params.id + const userMobile = await funGetUserById({ id: String(user) }) - const member = await request.json(); - const idDivision = context.params.id; + if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } const data = await prisma.division.count({ @@ -376,7 +376,7 @@ export async function POST(request: Request, context: { params: { id: string } } success: false, message: "Tambah anggota divisi gagal, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -392,7 +392,7 @@ export async function POST(request: Request, context: { params: { id: string } } }) // create log user - const log = await createLogUser({ act: 'CREATE', desc: 'User menambah anggota divisi', table: 'division', data: idDivision }) + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah anggota divisi', table: 'division', data: idDivision, user: userMobile.id }) return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota divisi" }, { status: 200 }); } catch (error) { diff --git a/src/app/api/mobile/division/[id]/route.ts b/src/app/api/mobile/division/[id]/route.ts index efe6316..546d602 100644 --- a/src/app/api/mobile/division/[id]/route.ts +++ b/src/app/api/mobile/division/[id]/route.ts @@ -1,5 +1,5 @@ import { prisma } from "@/module/_global"; -import { funGetUserByCookies } from "@/module/auth"; +import { funGetUserByCookies, funGetUserById } from "@/module/auth"; import { createLogUser } from "@/module/user"; import _ from "lodash"; import { NextResponse } from "next/server"; @@ -9,9 +9,12 @@ import { NextResponse } from "next/server"; export async function GET(request: Request, context: { params: { id: string } }) { try { const { id } = context.params; - 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 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 data = await prisma.division.findUnique({ @@ -22,7 +25,7 @@ export async function GET(request: Request, context: { params: { id: string } }) }); if (!data) { - return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 }); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 200 }); } const member = await prisma.divisionMember.findMany({ @@ -42,9 +45,16 @@ export async function GET(request: Request, context: { params: { id: string } }) } } }, - orderBy: { - isAdmin: 'desc', - } + orderBy: [ + { + isAdmin: 'desc', + }, + { + User: { + name: 'asc' + } + } + ] }) const fixMember = member.map((v: any) => ({ diff --git a/src/app/api/mobile/division/report/route.ts b/src/app/api/mobile/division/report/route.ts index 6c28b1e..efc4517 100644 --- a/src/app/api/mobile/division/report/route.ts +++ b/src/app/api/mobile/division/report/route.ts @@ -1,26 +1,28 @@ import { prisma } from "@/module/_global"; -import { funGetUserByCookies } from "@/module/auth"; +import { funGetUserById } from "@/module/auth"; import _, { ceil } from "lodash"; -import moment from "moment"; import { NextResponse } from "next/server"; export async function GET(request: Request) { try { - const user = await funGetUserByCookies() const { searchParams } = new URL(request.url) const idGroup = searchParams.get("group") const division = searchParams.get("division") const date = searchParams.get("date") const dateAkhir = searchParams.get("date-end") const kat = searchParams.get("cat") + const user = searchParams.get("user") let grup - if (user.id == undefined) { - return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }) + 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 }); } - if (idGroup == "null" || idGroup == undefined || idGroup == "") { - grup = user.idGroup + + if (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == null || idGroup == "undefined") { + grup = userMobile.idGroup } else { grup = idGroup }