From 9b2cb5baa595a30ab82d6394dfca794b383ed411 Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 19 May 2025 12:15:45 +0800 Subject: [PATCH 1/3] upd: api mobile Deskripsi: - api mobile banner create - api mobile banner delete - api mobile banner edit - api mobile banner get one NO Issues --- src/app/api/mobile/banner/[id]/route.ts | 37 ++++++++++++++----------- src/app/api/mobile/banner/route.ts | 36 ++++++++++-------------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/app/api/mobile/banner/[id]/route.ts b/src/app/api/mobile/banner/[id]/route.ts index 31e2136..e91ea4f 100644 --- a/src/app/api/mobile/banner/[id]/route.ts +++ b/src/app/api/mobile/banner/[id]/route.ts @@ -1,6 +1,6 @@ import { DIR, funDeleteFile, funUploadFile, 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 { NextResponse } from "next/server"; @@ -8,9 +8,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 userLogin = await funGetUserById({ id: String(user) }) + + if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } const data = await prisma.bannerImage.findUnique({ @@ -31,9 +34,10 @@ export async function GET(request: Request, context: { params: { id: string } }) export async function DELETE(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 { user } = (await request.json()); + const userLogin = await funGetUserById({ id: user }) + if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } const upd = await prisma.bannerImage.update({ @@ -47,7 +51,7 @@ export async function DELETE(request: Request, context: { params: { id: string } // create log user - const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus banner', table: 'bannerImage', data: id }) + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus banner', table: 'bannerImage', data: id, user: String(userLogin.id) }) return NextResponse.json({ success: true, message: "Berhasil menghapus banner" }, { status: 200 }); @@ -62,17 +66,18 @@ export async function DELETE(request: Request, context: { params: { id: string } export async function PUT(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 body = await request.formData() const file = body.get("file") as File const data = body.get("data") - const { title } = JSON.parse(data as string) + const { title, user } = JSON.parse(data as string) + const userLogin = await funGetUserById({ id: user }) + + if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + const upd = await prisma.bannerImage.update({ where: { id: String(id) @@ -102,7 +107,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) } // create log user - const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data banner', table: 'bannerImage', data: user.id }) + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data banner', table: 'bannerImage', data: id, user: String(userLogin.id) }) return NextResponse.json({ success: true, message: "Berhasil mengupdate banner" }, { status: 200 }); diff --git a/src/app/api/mobile/banner/route.ts b/src/app/api/mobile/banner/route.ts index 9737ac1..160d4b6 100644 --- a/src/app/api/mobile/banner/route.ts +++ b/src/app/api/mobile/banner/route.ts @@ -1,6 +1,6 @@ -import { DIR, funUploadFile, funViewDir, prisma } from "@/module/_global"; +import { DIR, funUploadFile, prisma } from "@/module/_global"; import { funGetUserById } from "@/module/auth"; -import { createLogUser } from "@/module/user"; +import { createLogUserMobile } from "@/module/user"; import { NextResponse } from "next/server"; @@ -38,32 +38,22 @@ export async function GET(request: Request) { // CREATE BANNER export async function POST(request: Request) { try { - console.log('masuklas') - console.log(request) const body = await request.formData() - const fileAwal = body.get("file"); - const file = JSON.parse(fileAwal as string) - const fileFix = file as File - const fileCoba = new File([fileFix], fileFix.name, { type: fileFix.type}); + const file = body.get("file") as File; const data = body.get("data"); - console.log(body, fileCoba) - const { title, user } = JSON.parse(data as string) - - 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 }) + if (userLogin.id == "null" || userLogin.id == undefined || userLogin.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } - const fExt = fileCoba.name.split(".").pop() - const fName = fileCoba.name.replace("." + fExt, "") - const newFile = new File([fileCoba], fileCoba.name, { type: fileCoba.type }); - const ini = funViewDir({ dirId: DIR.user }) - const upload = await funUploadFile({ file: fileCoba, dirId: DIR.banner }) + const fExt = file.name.split(".").pop() + const newFile = new File([file], file.name, { type: file.type }); + + const upload = await funUploadFile({ file: newFile, dirId: DIR.banner }) if (upload.success) { const create = await prisma.bannerImage.create({ data: { @@ -71,12 +61,14 @@ export async function POST(request: Request) { idVillage: userLogin.idVillage, extension: String(fExt), image: upload.data.id + }, + select: { + id: true, } }) // create log user - const log = await createLogUser({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: String(userLogin.id) }) - + const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah data banner baru', table: 'bannerImage', data: String(create.id), user: String(userLogin.id) }) return Response.json({ success: true, message: 'Sukses menambah data banner' }, { status: 200 }); } else { return Response.json({ success: false, message: 'Gagal menambah data banner' }, { status: 200 }); From d690e9c6497240a0e63385ccad5461c8b42c1516 Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 19 May 2025 15:38:43 +0800 Subject: [PATCH 2/3] upd: api mobile Deskripsi: - upload gambar pada tambah member - upload gambar pada edit member No Issues --- src/app/api/mobile/user/route.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app/api/mobile/user/route.ts b/src/app/api/mobile/user/route.ts index 1776b51..1729227 100644 --- a/src/app/api/mobile/user/route.ts +++ b/src/app/api/mobile/user/route.ts @@ -151,14 +151,13 @@ export async function POST(request: Request) { const body = await request.formData() const data = JSON.parse(body.get("data") as string) const file = body.get("file") as File + const user = await funGetUserById({ id: data.user }) - if (data.user == "null" || data.user == undefined || data.user == "") { + if (user.id == "null" || user.id == undefined || user.id == "") { return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); } - const user = await funGetUserById({ id: data.user }) const village = String(user.idVillage) - let groupFix = data.idGroup if (groupFix == null || groupFix == undefined || groupFix == "") { From ec9810a1bf6c3eacbb95821086abcfa1d84df60b Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 19 May 2025 17:50:08 +0800 Subject: [PATCH 3/3] upd: api mobile Deskripsi: - update api mobile project -nb: blm selesai No Issues' --- src/app/api/mobile/project/file/[id]/route.ts | 107 +++++++++--------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/src/app/api/mobile/project/file/[id]/route.ts b/src/app/api/mobile/project/file/[id]/route.ts index 30f3965..106d916 100644 --- a/src/app/api/mobile/project/file/[id]/route.ts +++ b/src/app/api/mobile/project/file/[id]/route.ts @@ -1,17 +1,20 @@ import { DIR, funDeleteFile, funUploadFile, 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 { NextResponse } from "next/server"; // HAPUS FILE PROJECT BUKAN PAKE ISACTIVE 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 { id } = context.params; + const { user } = (await request.json()); + const userMobile = await funGetUserById({ id: 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.projectFile.count({ where: { id: id, @@ -24,7 +27,7 @@ export async function DELETE(request: Request, context: { params: { id: string } success: false, message: "Hapus file gagal, data tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -43,14 +46,13 @@ export async function DELETE(request: Request, context: { params: { id: string } }); // create log user - const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus file kegiatan', table: 'project', data: String(dataRelasi?.idProject) }) + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus file kegiatan', table: 'project', data: String(dataRelasi?.idProject), user: userMobile.id }) return NextResponse.json( { success: true, message: "File berhasil dihapus", - data, }, { status: 200 } ); @@ -65,14 +67,15 @@ export async function DELETE(request: Request, context: { params: { id: string } // CEK FILE PROJECT APAKAH PERNAH DIUPLOAD PADA PROJECT YG SAMA 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 } = context.params; const body = await request.formData() + const data = JSON.parse(body.get("data") as string) + const user = await funGetUserById({ id: data.user }) + + if (user.id == "null" || user.id == undefined || user.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + const file = body.get("file") as File const fileName = file.name @@ -88,7 +91,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) success: false, message: "Upload file gagal, data kegiatan tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } @@ -113,7 +116,7 @@ export async function PUT(request: Request, context: { params: { id: string } }) if (cek) { - return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 400 }); + return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 200 }); } else { return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 }); } @@ -128,14 +131,15 @@ export async function PUT(request: Request, context: { params: { id: string } }) // TAMBAH FILE PROJECT 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 { id } = context.params; const body = await request.formData() + const data = JSON.parse(body.get("data") as string) + const user = await funGetUserById({ id: data.user }) + + if (user.id == "null" || user.id == undefined || user.id == "") { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 }); + } + const cekFile = body.has("file0") const dataCek = await prisma.project.count({ @@ -150,46 +154,39 @@ export async function POST(request: Request, context: { params: { id: string } } success: false, message: "Tambah file kegiatan gagal, data tugas tidak ditemukan", }, - { status: 404 } + { status: 200 } ); } - const dataProject = await prisma.project.findUnique({ - where: { - id: id, - }, - }); - - - if (cekFile) { - for (var pair of body.entries()) { - if (String(pair[0]).substring(0, 4) == "file") { - const file = body.get(pair[0]) as File - const fExt = file.name.split(".").pop() - const fName = file.name.replace("." + fExt, "") + console.log('masuk file') + // for (var pair of body.entries()) { + // if (String(pair[0]).substring(0, 4) == "file") { + // const file = body.get(pair[0]) as File + // const fExt = file.name.split(".").pop() + // const fName = file.name.replace("." + fExt, "") - const upload = await funUploadFile({ file: file, dirId: DIR.project }) - if (upload.success) { - const insertToTable = await prisma.projectFile.create({ - data: { - idStorage: upload.data.id, - idProject: id, - name: fName, - extension: String(fExt), + // const upload = await funUploadFile({ file: file, dirId: DIR.project }) + // if (upload.success) { + // const insertToTable = await prisma.projectFile.create({ + // data: { + // idStorage: upload.data.id, + // idProject: id, + // name: fName, + // extension: String(fExt), - }, - select: { - id: true - } - }) - } - } - } + // }, + // select: { + // id: true + // } + // }) + // } + // } + // } } - // create log user - const log = await createLogUser({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id) }) + // // create log user + // const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id), user: user.id }) return NextResponse.json({ success: true, message: "Berhasil mengupload file kegiatan" }, { status: 200 }); } catch (error) {