diff --git a/src/app/api/mobile/discussion-general/[id]/comment/route.ts b/src/app/api/mobile/discussion-general/[id]/comment/route.ts index c748522..71b8dd2 100644 --- a/src/app/api/mobile/discussion-general/[id]/comment/route.ts +++ b/src/app/api/mobile/discussion-general/[id]/comment/route.ts @@ -147,4 +147,90 @@ export async function POST(request: Request, context: { params: { id: string } } console.error(error) return NextResponse.json({ success: false, message: "Gagal menambahkan komentar, coba lagi nanti (error: 500)" }) } +} + +// EDIT KOMENTAR +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params + const { desc, 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 cek = await prisma.discussionComment.count({ + where: { + id, + isActive: true + } + }) + + if (cek == 0) { + return NextResponse.json({ success: false, message: "Gagal mengedit komentar, data tidak ditemukan" }, { status: 200 }); + } + + + const data = await prisma.discussionComment.update({ + where: { + id + }, + data: { + comment: desc, + isEdited: true + } + }) + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengedit komentar pada diskusi umum', table: 'discussionComment', data: id, user: userMobile.id }) + return NextResponse.json({ success: true, message: "Berhasil mengedit komentar" }, { status: 200 }); + + } catch (error) { + console.error(error) + return NextResponse.json({ success: false, message: "Gagal mengedit komentar, coba lagi nanti (error: 500)" }) + } +} + + +// HAPUS KOMENTAR +export async function DELETE(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params + 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 cek = await prisma.discussionComment.count({ + where: { + id, + isActive: true + } + }) + + if (cek == 0) { + return NextResponse.json({ success: false, message: "Gagal mengedit komentar, data tidak ditemukan" }, { status: 200 }); + } + + + const data = await prisma.discussionComment.update({ + where: { + id + }, + data: { + isActive: false + } + }) + + // create log user + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus komentar pada diskusi umum', table: 'discussionComment', data: id, user: userMobile.id }) + return NextResponse.json({ success: true, message: "Berhasil mengedit komentar" }, { status: 200 }); + + } catch (error) { + console.error(error) + return NextResponse.json({ success: false, message: "Gagal mengedit komentar, coba lagi nanti (error: 500)" }) + } } \ No newline at end of file diff --git a/src/app/api/mobile/discussion-general/route.ts b/src/app/api/mobile/discussion-general/route.ts index bb46a00..b890761 100644 --- a/src/app/api/mobile/discussion-general/route.ts +++ b/src/app/api/mobile/discussion-general/route.ts @@ -75,6 +75,9 @@ export async function GET(request: Request) { DiscussionComment: { select: { id: true, + }, + where: { + isActive: true } } } diff --git a/src/app/api/mobile/discussion/[id]/comment/route.ts b/src/app/api/mobile/discussion/[id]/comment/route.ts index 34d86ec..57f7b2e 100644 --- a/src/app/api/mobile/discussion/[id]/comment/route.ts +++ b/src/app/api/mobile/discussion/[id]/comment/route.ts @@ -5,7 +5,7 @@ import _ from "lodash"; import { NextResponse } from "next/server"; import { sendFCMNotificationMany } from "../../../../../../../xsendMany"; -// CREATE COMENT BY ID KOMENTAR +// CREATE COMENT export async function POST(request: Request, context: { params: { id: string } }) { try { const { id } = context.params @@ -156,4 +156,103 @@ export async function POST(request: Request, context: { params: { id: string } } console.error(error); return NextResponse.json({ success: false, message: "Gagal menambah komentar, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); } +} + +// EDIT KOMENTAR +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params + const { comment, 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: "User tidak ditemukan" }, { status: 200 }); + } + + const cek = await prisma.divisionDisscussionComment.count({ + where: { + id, + isActive: true + } + }) + + if (cek == 0) { + return NextResponse.json( + { + success: false, + message: "Edit komentar gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const data = await prisma.divisionDisscussionComment.update({ + where: { + id: id + }, + data: { + comment: comment, + isEdited: true + } + }) + + // create log user + const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengedit komentar pada diskusi divisi', table: 'divisionDisscussionComment', data: id, user: userMobile.id }) + + return NextResponse.json({ success: true, message: "Berhasil mengedit komentar" }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menambah komentar, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 }); + } +} + +// HAPUS KOMENTAR +export async function DELETE(request: Request, context: { params: { id: string } }) { + try { + const { id } = context.params + 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: "User tidak ditemukan" }, { status: 200 }); + } + + const cek = await prisma.divisionDisscussionComment.count({ + where: { + id, + isActive: true + } + }) + + if (cek == 0) { + return NextResponse.json( + { + success: false, + message: "Hapus komentar gagal, data tidak ditemukan", + }, + { status: 200 } + ); + } + + const data = await prisma.divisionDisscussionComment.update({ + where: { + id: id + }, + data: { + isActive: false + } + }) + + // create log user + const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus komentar pada diskusi divisi', table: 'divisionDisscussionComment', data: id, user: userMobile.id }) + + return NextResponse.json({ success: true, message: "Berhasil menghapus komentar" }, { status: 200 }); + + } catch (error) { + console.error(error); + return NextResponse.json({ success: false, message: "Gagal menghapus komentar, 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/discussion/[id]/route.ts b/src/app/api/mobile/discussion/[id]/route.ts index 65930b1..14d676d 100644 --- a/src/app/api/mobile/discussion/[id]/route.ts +++ b/src/app/api/mobile/discussion/[id]/route.ts @@ -38,7 +38,8 @@ export async function GET(request: Request, context: { params: { id: string } }) if (cat == "comment") { const data = await prisma.divisionDisscussionComment.findMany({ where: { - idDisscussion: id + idDisscussion: id, + isActive: true }, select: { id: true, diff --git a/src/app/api/mobile/discussion/route.ts b/src/app/api/mobile/discussion/route.ts index 9dbdb43..3003002 100644 --- a/src/app/api/mobile/discussion/route.ts +++ b/src/app/api/mobile/discussion/route.ts @@ -67,6 +67,9 @@ export async function GET(request: Request) { DivisionDisscussionComment: { select: { id: true, + }, + where: { + isActive: true } } }