feat: hapus file dari storage saat komentar dihapus atau diedit

- DELETE: soft-delete DiscussionCommentFile dan hapus file dari storage via funDeleteFile
- PUT: terima filesToRemove[], hapus file dari storage dan soft-delete record terkait
- Backward compatible: app lama tanpa filesToRemove tidak terpengaruh
This commit is contained in:
2026-06-09 17:35:46 +08:00
parent 0957e554a1
commit adee3f9e45

View File

@@ -1,4 +1,4 @@
import { prisma } from "@/module/_global"; import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth"; import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user"; import { createLogUserMobile } from "@/module/user";
import _ from "lodash"; import _ from "lodash";
@@ -10,7 +10,16 @@ import { sendFCMNotificationMany } from "../../../../../../../xsendMany";
export async function POST(request: Request, context: { params: { id: string } }) { export async function POST(request: Request, context: { params: { id: string } }) {
try { try {
const { id } = context.params const { id } = context.params
const { desc, user } = (await request.json()); const contentType = request.headers.get("content-type")
let desc, user, cekFile, body: FormData | undefined
if (contentType?.includes("multipart/form-data")) {
body = await request.formData()
const dataBody = body.get("data")
cekFile = body.has("file0")
;({ desc, user } = JSON.parse(dataBody as string))
} else {
;({ desc, user } = await request.json())
}
const userMobile = await funGetUserById({ id: String(user) }) const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
@@ -37,6 +46,28 @@ export async function POST(request: Request, context: { params: { id: string } }
} }
}) })
if (cekFile && body) {
body.delete("data")
for (const 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 = decodeURIComponent(file.name.replace("." + fExt, ""))
const upload = await funUploadFile({ file, dirId: DIR.discussion })
if (upload.success) {
await prisma.discussionCommentFile.create({
data: {
idComment: data.id,
name: fName,
extension: String(fExt),
idStorage: upload.data.id
}
})
}
}
}
}
const dataDiscussion = await prisma.discussion.findUnique({ const dataDiscussion = await prisma.discussion.findUnique({
where: { where: {
id id
@@ -153,7 +184,7 @@ export async function POST(request: Request, context: { params: { id: string } }
export async function PUT(request: Request, context: { params: { id: string } }) { export async function PUT(request: Request, context: { params: { id: string } }) {
try { try {
const { id } = context.params const { id } = context.params
const { desc, user } = (await request.json()); const { desc, user, filesToRemove = [] } = (await request.json());
const userMobile = await funGetUserById({ id: String(user) }) const userMobile = await funGetUserById({ id: String(user) })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") { if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
@@ -161,25 +192,30 @@ export async function PUT(request: Request, context: { params: { id: string } })
} }
const cek = await prisma.discussionComment.count({ const cek = await prisma.discussionComment.count({
where: { where: { id, isActive: true }
id,
isActive: true
}
}) })
if (cek == 0) { if (cek == 0) {
return NextResponse.json({ success: false, message: "Gagal mengedit komentar, data tidak ditemukan" }, { status: 200 }); return NextResponse.json({ success: false, message: "Gagal mengedit komentar, data tidak ditemukan" }, { status: 200 });
} }
if (filesToRemove.length > 0) {
const data = await prisma.discussionComment.update({ const files = await prisma.discussionCommentFile.findMany({
where: { where: { id: { in: filesToRemove }, idComment: id, isActive: true },
id select: { id: true, idStorage: true }
}, })
data: { for (const file of files) {
comment: desc, if (file.idStorage) await funDeleteFile({ fileId: file.idStorage })
isEdited: true
} }
await prisma.discussionCommentFile.updateMany({
where: { id: { in: filesToRemove }, idComment: id },
data: { isActive: false }
})
}
await prisma.discussionComment.update({
where: { id },
data: { comment: desc, isEdited: true }
}) })
// create log user // create log user
@@ -216,18 +252,30 @@ export async function DELETE(request: Request, context: { params: { id: string }
} }
const data = await prisma.discussionComment.update({ const commentFiles = await prisma.discussionCommentFile.findMany({
where: { where: { idComment: id, isActive: true },
id select: { id: true, idStorage: true }
}, })
data: {
isActive: false for (const file of commentFiles) {
if (file.idStorage) await funDeleteFile({ fileId: file.idStorage })
} }
if (commentFiles.length > 0) {
await prisma.discussionCommentFile.updateMany({
where: { idComment: id },
data: { isActive: false }
})
}
await prisma.discussionComment.update({
where: { id },
data: { isActive: false }
}) })
// create log user // create log user
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus komentar pada diskusi umum', table: 'discussionComment', data: id, user: userMobile.id }) 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 }); return NextResponse.json({ success: true, message: "Berhasil menghapus komentar" }, { status: 200 });
} catch (error) { } catch (error) {
console.error(error) console.error(error)