diff --git a/src/app/api/mobile/event/route.ts b/src/app/api/mobile/event/route.ts new file mode 100644 index 00000000..e52819a6 --- /dev/null +++ b/src/app/api/mobile/event/route.ts @@ -0,0 +1,52 @@ +import _ from "lodash"; +import { NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +export { POST }; + +async function POST(request: Request) { + try { + const { data } = await request.json(); + + const create = await prisma.event.create({ + data: { + title: _.startCase(data.title), + lokasi: data.lokasi, + deskripsi: data.deskripsi, + eventMaster_TipeAcaraId: data.eventMaster_TipeAcaraId, + tanggal: data.tanggal, + tanggalSelesai: data.tanggalSelesai, + authorId: data.authorId, + }, + select: { + id: true, + title: true, + EventMaster_Status: { + select: { + name: true, + }, + }, + authorId: true, + }, + }); + + if (!create) return { status: 400, message: "Gagal disimpan" }; + + return NextResponse.json( + { + success: true, + message: "Berhasil disimpan", + data: create, + }, + { status: 201 } + ); + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Error create event", + }, + { status: 500 } + ); + } +} diff --git a/src/app/api/mobile/portofolio/[id]/route.ts b/src/app/api/mobile/portofolio/[id]/route.ts index c94e7bc2..1c4105a5 100644 --- a/src/app/api/mobile/portofolio/[id]/route.ts +++ b/src/app/api/mobile/portofolio/[id]/route.ts @@ -22,8 +22,9 @@ async function GET(request: Request, { params }: { params: { id: string } }) { masterBidangBisnisId: true, Profile: { select: { + id: true, userId: true, - } + }, }, MasterBidangBisnis: { select: { @@ -204,6 +205,25 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { let message; if (category === "detail") { + console.log("UPDATE PORTOFOLIO DETAIL >>"); + console.log("DATA >>", data); + + const checkData = await prisma.portofolio.findUnique({ + where: { id }, + include: { + Portofolio_BidangDanSubBidangBisnis: true, + }, + }); + + if (!checkData) { + return NextResponse.json({ + success: false, + message: "Data tidak ditemukan", + }); + } + + console.log("CHECK DATA >>", checkData); + const updateDetail = await prisma.portofolio.update({ where: { id }, data: { @@ -214,12 +234,75 @@ async function PUT(request: Request, { params }: { params: { id: string } }) { masterBidangBisnisId: data.masterBidangBisnisId, }, }); + if (!updateDetail) { return NextResponse.json({ success: false, message: "Gagal mengupdate detail portofolio", }); } + + const bidangBerubah = + checkData.masterBidangBisnisId !== data.masterBidangBisnisId; + + if (bidangBerubah) { + // Bidang berubah → hapus semua sub bidang lama + await prisma.portofolio_BidangDanSubBidangBisnis.deleteMany({ + where: { portofolioId: id }, + }); + + // Tambahkan sub bidang baru + for (const sub of data.subBidang) { + await prisma.portofolio_BidangDanSubBidangBisnis.create({ + data: { + portofolioId: id, + masterBidangBisnisId: data.masterBidangBisnisId, + masterSubBidangBisnisId: sub.MasterSubBidangBisnis.id, + }, + }); + } + } else { + // Bidang tidak berubah → sinkronisasi sub bidang + + const existingSub = checkData.Portofolio_BidangDanSubBidangBisnis; + + const incomingIds = data.subBidang.map( + (sub: any) => sub.MasterSubBidangBisnis.id + ); + + const existingIds = existingSub.map( + (item) => item.masterSubBidangBisnisId + ); + + // 1. Hapus sub bidang yang sudah tidak dipilih + const toDelete = existingSub.filter( + (item) => !incomingIds.includes(item.masterSubBidangBisnisId) + ); + + await prisma.portofolio_BidangDanSubBidangBisnis.deleteMany({ + where: { + id: { + in: toDelete.map((item) => item.id), + }, + }, + }); + + // 2. Tambahkan sub bidang baru yang belum ada di DB + const toCreate = data.subBidang.filter( + (sub: any) => !existingIds.includes(sub.MasterSubBidangBisnis.id) + ); + + for (const sub of toCreate) { + await prisma.portofolio_BidangDanSubBidangBisnis.create({ + data: { + portofolioId: id, + masterBidangBisnisId: data.masterBidangBisnisId, + masterSubBidangBisnisId: sub.MasterSubBidangBisnis.id, + }, + }); + } + } + message = "Berhasil mengupdate detail portofolio"; } else if (category === "medsos") { const updateMedsos = await prisma.portofolio_MediaSosial.update({ diff --git a/src/app/api/mobile/route.ts b/src/app/api/mobile/route.ts new file mode 100644 index 00000000..79fc8bcd --- /dev/null +++ b/src/app/api/mobile/route.ts @@ -0,0 +1,46 @@ +import { decrypt } from "@/app/(auth)/_lib/decrypt"; +import { prisma } from "@/lib"; +import { NextResponse } from "next/server"; + +export async function GET(request: Request) { + try { + const { searchParams } = new URL(request.url); + const token = searchParams.get("token"); + + const dataUser = await decrypt({ + token: token!, + encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, + }); + + const id = dataUser?.id; + const user = await prisma.user.findUnique({ + where: { + id: id as string, + }, + }); + + return NextResponse.json( + { + success: true, + message: "Berhasil mendapatkan data", + data: user, + }, + { + status: 200, + } + ); + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Gagal mendapatkan data", + reason: (error as Error).message, + }, + { + status: 500, + } + ); + } finally { + await prisma.$disconnect(); + } +} diff --git a/src/app/api/mobile/user/[id]/route.ts b/src/app/api/mobile/user/[id]/route.ts new file mode 100644 index 00000000..b151a570 --- /dev/null +++ b/src/app/api/mobile/user/[id]/route.ts @@ -0,0 +1,34 @@ +import { prisma } from "@/lib"; +import { NextResponse } from "next/server"; + +export async function GET( + request: Request, + { params }: { params: { id: string } } +) { + try { + const { id } = params; + + const data = await prisma.user.findUnique({ + where: { + id: id, + }, + include: { + Profile: true, + }, + }); + + return NextResponse.json( + { success: true, message: "Berhasil mendapatkan data", data: data }, + { status: 200 } + ); + } catch (error) { + return NextResponse.json( + { + success: false, + message: "Error get data from API ", + reason: (error as Error).message, + }, + { status: 500 } + ); + } +} diff --git a/src/app/api/mobile/user/route.ts b/src/app/api/mobile/user/route.ts index 79fc8bcd..3a414098 100644 --- a/src/app/api/mobile/user/route.ts +++ b/src/app/api/mobile/user/route.ts @@ -1,21 +1,45 @@ -import { decrypt } from "@/app/(auth)/_lib/decrypt"; import { prisma } from "@/lib"; import { NextResponse } from "next/server"; export async function GET(request: Request) { try { const { searchParams } = new URL(request.url); - const token = searchParams.get("token"); + const search = searchParams.get("search"); - const dataUser = await decrypt({ - token: token!, - encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, - }); - - const id = dataUser?.id; - const user = await prisma.user.findUnique({ + const data = await prisma.user.findMany({ + orderBy: { + username: "asc", + }, where: { - id: id as string, + username: { + contains: search || "", + mode: "insensitive", + }, + active: true, + NOT: { + Profile: null, + }, + OR: [ + { + MasterUserRole: { + name: "User", + }, + }, + { + MasterUserRole: { + name: "Admin", + }, + }, + ], + }, + include: { + Profile: { + select: { + id: true, + name: true, + imageId: true, + }, + }, }, }); @@ -23,7 +47,7 @@ export async function GET(request: Request) { { success: true, message: "Berhasil mendapatkan data", - data: user, + data: data, }, { status: 200, @@ -40,7 +64,46 @@ export async function GET(request: Request) { status: 500, } ); - } finally { - await prisma.$disconnect(); } + + // try { + // const { searchParams } = new URL(request.url); + // const token = searchParams.get("token"); + + // const dataUser = await decrypt({ + // token: token!, + // encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!, + // }); + + // const id = dataUser?.id; + // const user = await prisma.user.findUnique({ + // where: { + // id: id as string, + // }, + // }); + + // return NextResponse.json( + // { + // success: true, + // message: "Berhasil mendapatkan data", + // data: user, + // }, + // { + // status: 200, + // } + // ); + // } catch (error) { + // return NextResponse.json( + // { + // success: false, + // message: "Gagal mendapatkan data", + // reason: (error as Error).message, + // }, + // { + // status: 500, + // } + // ); + // } finally { + // await prisma.$disconnect(); + // } }