From 0f575c4efc8f29c36b988579c83fdc3b85938ba0 Mon Sep 17 00:00:00 2001 From: amel Date: Tue, 29 Apr 2025 15:23:46 +0800 Subject: [PATCH 1/2] upd: api mobil.e Deskripsi: - update api mobile group crud - server action log user untuk mobile No Issues --- src/app/api/mobile/group/[id]/route.ts | 138 ++++++++++++++++++ .../api/mobile/group/get-division/route.ts | 53 +++++++ src/app/api/mobile/group/route.ts | 79 ++++++++++ src/module/user/index.ts | 10 +- .../user/log/fun/createLogUserMobile.ts | 21 +++ 5 files changed, 295 insertions(+), 6 deletions(-) create mode 100644 src/app/api/mobile/group/[id]/route.ts create mode 100644 src/app/api/mobile/group/get-division/route.ts create mode 100644 src/app/api/mobile/group/route.ts create mode 100644 src/module/user/log/fun/createLogUserMobile.ts diff --git a/src/app/api/mobile/group/[id]/route.ts b/src/app/api/mobile/group/[id]/route.ts new file mode 100644 index 0000000..5281961 --- /dev/null +++ b/src/app/api/mobile/group/[id]/route.ts @@ -0,0 +1,138 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import { NextResponse } from "next/server"; + +export const dynamic = 'force-dynamic' +export const revalidate = true + +export async function GET(request: Request, context: { params: { id: string } }) { + try { + const { searchParams } = new URL(request.url); + const userMobile = searchParams.get("user") + + if (userMobile == "null" || userMobile == undefined || userMobile == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const { id } = context.params; + const data = await prisma.group.findUnique({ + where: { + id: id, + }, + }); + + if (!data) { + return NextResponse.json( + { + success: false, + message: "Gagal mendapatkan grup, data tidak ditemukan", + }, + { status: 404 } + ); + } + + return NextResponse.json( + { + success: true, + message: "Berhasil mendapatkan grup", + data, + }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + +export async function DELETE(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { isActive, user } = (await request.json()); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + const userLogin = await funGetUserById({ id: user }) + + const data = await prisma.group.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Edit grup gagal, data tidak ditemukan", + }, + { status: 404 } + ); + } + + const update = await prisma.group.update({ + where: { + id: id, + }, + data: { + isActive: !isActive, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengedit status data grup', table: 'group', data: id, user }) + + return NextResponse.json({ success: true, message: "Grup berhasil diedit", data, }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengedit grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { name, user } = (await request.json()); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const data = await prisma.group.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Edit grup gagal, data tidak ditemukan", + }, + { status: 404 } + ); + } + + const update = await prisma.group.update({ + where: { + id: id, + }, + data: { + name: name, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengedit data grup', table: 'group', data: id, user }) + + return NextResponse.json({ success: true, message: "Grup berhasil diedit", data, }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengedit grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/group/get-division/route.ts b/src/app/api/mobile/group/get-division/route.ts new file mode 100644 index 0000000..c11a98b --- /dev/null +++ b/src/app/api/mobile/group/get-division/route.ts @@ -0,0 +1,53 @@ +import { prisma } from "@/module/_global"; +import { funGetUserByCookies } from "@/module/auth"; +import { NextResponse } from "next/server"; +export const dynamic = 'force-dynamic' +export const revalidate = true + + +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 role = user.idUserRole + const villaId = user.idVillage + const group = user.idGroup + let kondisi: any = { + isActive: true, + idVillage: String(villaId) + } + + if (role != "supadmin") { + kondisi = { + isActive: true, + idVillage: String(villaId), + id: String(group) + } + } + + const data = await prisma.group.findMany({ + where: kondisi, + select: { + id: true, + name: true, + Division: { + where: { + isActive: true + }, + select: { + id: true, + name: true + } + } + } + }); + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan grup", data, }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/group/route.ts b/src/app/api/mobile/group/route.ts new file mode 100644 index 0000000..cb271e0 --- /dev/null +++ b/src/app/api/mobile/group/route.ts @@ -0,0 +1,79 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUser } from "@/module/user"; +import { NextResponse } from "next/server"; + +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const isActive = searchParams.get("active"); + const name = searchParams.get('search'); + const userMobile = searchParams.get("user") + + if (userMobile == "null" || userMobile == undefined || userMobile == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const user = await funGetUserById({ id: userMobile }) + + const villaId = user.idVillage + + const data = await prisma.group.findMany({ + where: { + isActive: isActive == 'false' ? false : true, + idVillage: String(villaId), + name: { + contains: (name == undefined || name == null) ? "" : name, + mode: "insensitive" + } + }, + select: { + id: true, + name: true, + isActive: true + }, + orderBy: { + name: 'asc' + } + }); + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan grup", data, }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + +export async function POST(request: Request) { + try { + const { name, user } = (await request.json()); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const userMobile = await funGetUserById({ id: user }) + + const villaId = userMobile.idVillage + const data = await prisma.group.create({ + data: { + name, + idVillage: String(villaId) + }, + select: { + id: true + } + }); + + + // create log user + const log = await createLogUser({ act: 'CREATE', desc: 'User membuat data grup', table: 'group', data: data.id }) + + return NextResponse.json({ success: true, message: "Berhasil menambahkan grup", data, }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan grup, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/module/user/index.ts b/src/module/user/index.ts index 72ade9a..5f2ce84 100644 --- a/src/module/user/index.ts +++ b/src/module/user/index.ts @@ -1,11 +1,9 @@ import { TypeUser } from './lib/type_user'; import createLogUser from "./log/fun/createLogUser"; +import createLogUserMobile from './log/fun/createLogUserMobile'; import { funGetAllmember } from './member/lib/api_member'; -import Profile from './profile/ui/profile'; import EditProfile from './profile/ui/edit_profile'; +import Profile from './profile/ui/profile'; -export { createLogUser }; -export type { TypeUser } -export { funGetAllmember } -export { Profile } -export { EditProfile } \ No newline at end of file +export { createLogUser, createLogUserMobile, EditProfile, funGetAllmember, Profile }; +export type { TypeUser }; diff --git a/src/module/user/log/fun/createLogUserMobile.ts b/src/module/user/log/fun/createLogUserMobile.ts new file mode 100644 index 0000000..275c5c6 --- /dev/null +++ b/src/module/user/log/fun/createLogUserMobile.ts @@ -0,0 +1,21 @@ +import { prisma } from "@/module/_global"; + +export default async function createLogUserMobile({ act, desc, table, data, user }: { act: string, desc: string, table: string, data: string, user: string }) { + try { + await prisma.userLog.create({ + data: { + idUser: user, + action: act, + desc: desc, + idContent: data, + tbContent: table + } + }) + + return { success: true, message: "Success" } + + } catch (error) { + console.error(error) + return { success: false, message: "Internal Server Error" } + } +} \ No newline at end of file From e364c4901d0d3c6a9fde5521969b4385479524f7 Mon Sep 17 00:00:00 2001 From: amel Date: Tue, 29 Apr 2025 17:21:21 +0800 Subject: [PATCH 2/2] upd: mobile api Deskripsi: - api mobile jabatan crud No Issuesf --- src/app/api/mobile/position/[id]/route.ts | 144 ++++++++++++++++++++++ src/app/api/mobile/position/route.ts | 141 +++++++++++++++++++++ 2 files changed, 285 insertions(+) create mode 100644 src/app/api/mobile/position/[id]/route.ts create mode 100644 src/app/api/mobile/position/route.ts diff --git a/src/app/api/mobile/position/[id]/route.ts b/src/app/api/mobile/position/[id]/route.ts new file mode 100644 index 0000000..d13fa5d --- /dev/null +++ b/src/app/api/mobile/position/[id]/route.ts @@ -0,0 +1,144 @@ +import { prisma } from "@/module/_global"; +import { createLogUserMobile } from "@/module/user"; +import { NextResponse } from "next/server"; + +// GET ONE JABATAN +export async function GET(request: Request, context: { params: { id: string } }) { + try { + + const { searchParams } = new URL(request.url); + const userMobile = searchParams.get("user") + const { id } = context.params; + + if (userMobile == "null" || userMobile == undefined || userMobile == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const data = await prisma.position.findUnique({ + where: { + id: id, + }, + select: { + id: true, + name: true, + idGroup: true, + }, + }); + if (!data) { + return NextResponse.json( + { + success: false, + message: "Gagal mendapatkan jabatan, data tidak ditemukan", + }, + { status: 404 } + ); + } + + return NextResponse.json( + { + success: true, + message: "Berhasil mendapatkan jabatan", + data, + }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + +// DELETE / ACTIVE & NON ACTIVE POSITION +export async function DELETE(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { isActive, user } = (await request.json()); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const data = await prisma.position.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Gagal mengubah status jabatan, data tidak ditemukan", + }, + { status: 404 } + ); + } + + + const update = await prisma.position.update({ + where: { + id: id, + }, + data: { + isActive: !isActive, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status data jabatan', table: 'position', data: id, user }) + return NextResponse.json( + { success: true, message: "Berhasil mengubah status jabatan" }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengubah status jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + +// UPDATE POSITION +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params; + const { name, idGroup, user } = await request.json(); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const cek = await prisma.position.count({ + where: { + name: name, + idGroup: idGroup, + NOT: { + id: id + } + }, + }); + + if (cek == 0) { + const positions = await prisma.position.update({ + where: { + id: id, + }, + data: { + name: name, + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data jabatan', table: 'position', data: id, user }) + return NextResponse.json({ success: true, message: "Berhasil mengedit jabatan", }, { status: 200 }); + } else { + return NextResponse.json( + { success: false, message: "Jabatan sudah ada" }, + { status: 400 } + ); + } + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mengedit jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} \ No newline at end of file diff --git a/src/app/api/mobile/position/route.ts b/src/app/api/mobile/position/route.ts new file mode 100644 index 0000000..0b68326 --- /dev/null +++ b/src/app/api/mobile/position/route.ts @@ -0,0 +1,141 @@ +import { prisma } from "@/module/_global"; +import { funGetUserById } from "@/module/auth"; +import { createLogUserMobile } from "@/module/user"; +import _ from "lodash"; +import { NextResponse } from "next/server"; + + +// GET ALL POSITION +export async function GET(request: Request) { + try { + let grup + const { searchParams } = new URL(request.url); + const idGroup = searchParams.get("group"); + const active = searchParams.get('active'); + const name = searchParams.get('search') + const userMobile = searchParams.get("user") + + if (userMobile == "null" || userMobile == undefined || userMobile == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const user = await funGetUserById({ id: userMobile }) + + if (idGroup == "null" || idGroup == undefined || idGroup == "") { + grup = user.idGroup + } else { + grup = idGroup + } + + const cek = await prisma.group.count({ + where: { + id: grup, + isActive: true + } + }) + + if (cek == 0) { + return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, data tidak ditemukan", }, { status: 404 }); + } + + const filter = await prisma.group.findUnique({ + where: { + id: grup + }, + select: { + id: true, + name: true + } + }) + + const positions = await prisma.position.findMany({ + where: { + idGroup: grup, + isActive: active == 'false' ? false : true, + name: { + contains: (name == undefined || name == null) ? "" : name, + mode: "insensitive" + } + }, + select: { + id: true, + name: true, + isActive: true, + Group: { + select: { + name: true + } + } + }, + }); + + const allData = positions.map((v: any) => ({ + ..._.omit(v, ["Group", "name"]), + name: v.name, + group: v.Group.name + })) + + const dataFix = _.orderBy(allData, [data => data.name.toLowerCase()], ['asc']); + + return NextResponse.json({ success: true, message: "Berhasil mendapatkan jabatan", data: dataFix, filter }, { status: 200 }); + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + + + +// CREATE POSITION +export async function POST(request: Request) { + try { + const { name, idGroup, user } = await request.json(); + + if (user == "null" || user == undefined || user == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const userMobile = await funGetUserById({ id: user }) + + let groupFix = idGroup + + if (groupFix == null || groupFix == undefined || groupFix == "") { + groupFix = userMobile.idGroup + } + + + const cek = await prisma.position.count({ + where: { + name: name, + idGroup: groupFix, + }, + }); + if (cek == 0) { + const positions = await prisma.position.create({ + data: { + name: name, + idGroup: groupFix, + }, + select: { + id: true, + name: true, + idGroup: true + }, + }); + + // create log user + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data jabatan baru', table: 'position', data: positions.id, user }) + + return NextResponse.json({ success: true, message: "Berhasil menambahkan jabatan", positions, }, { status: 200 }); + } else { + return NextResponse.json( + { success: false, message: "Jabatan sudah ada" }, + { status: 400 } + ); + } + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +}