Compare commits
3 Commits
amalia/09-
...
amalia/10-
| Author | SHA1 | Date | |
|---|---|---|---|
| 67db39ccee | |||
| 076e7f564f | |||
| af504c95c0 |
@@ -0,0 +1,16 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "DivisionDiscussionCommentFile" (
|
||||
"id" TEXT NOT NULL,
|
||||
"idComment" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"extension" TEXT NOT NULL,
|
||||
"idStorage" TEXT,
|
||||
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "DivisionDiscussionCommentFile_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "DivisionDiscussionCommentFile" ADD CONSTRAINT "DivisionDiscussionCommentFile_idComment_fkey" FOREIGN KEY ("idComment") REFERENCES "DivisionDisscussionComment"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -461,16 +461,29 @@ model DivisionDisscussion {
|
||||
}
|
||||
|
||||
model DivisionDisscussionComment {
|
||||
id String @id @default(cuid())
|
||||
DivisionDisscussion DivisionDisscussion @relation(fields: [idDisscussion], references: [id])
|
||||
idDisscussion String
|
||||
comment String @db.Text
|
||||
isActive Boolean @default(true)
|
||||
User User @relation(fields: [createdBy], references: [id])
|
||||
createdBy String
|
||||
isEdited Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
id String @id @default(cuid())
|
||||
DivisionDisscussion DivisionDisscussion @relation(fields: [idDisscussion], references: [id])
|
||||
idDisscussion String
|
||||
comment String @db.Text
|
||||
isActive Boolean @default(true)
|
||||
User User @relation(fields: [createdBy], references: [id])
|
||||
createdBy String
|
||||
isEdited Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
DivisionDiscussionCommentFile DivisionDiscussionCommentFile[]
|
||||
}
|
||||
|
||||
model DivisionDiscussionCommentFile {
|
||||
id String @id @default(cuid())
|
||||
DivisionDisscussionComment DivisionDisscussionComment @relation(fields: [idComment], references: [id])
|
||||
idComment String
|
||||
name String
|
||||
extension String
|
||||
idStorage String?
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
model DivisionDiscussionFile {
|
||||
id String @id @default(cuid())
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
@@ -9,7 +9,18 @@ import { sendFCMNotificationMany } from "../../../../../../../xsendMany";
|
||||
export async function POST(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params
|
||||
const { comment, user } = (await request.json());
|
||||
const contentType = request.headers.get("content-type")
|
||||
|
||||
let comment: string, user: string, body: FormData | undefined, cekFile = false
|
||||
|
||||
if (contentType?.includes("multipart/form-data")) {
|
||||
body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
cekFile = body.has("file0");
|
||||
({ comment, user } = JSON.parse(dataBody as string))
|
||||
} else {
|
||||
({ comment, user } = await request.json())
|
||||
}
|
||||
|
||||
const userMobile = await funGetUserById({ id: String(user) })
|
||||
|
||||
@@ -44,6 +55,28 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
}
|
||||
})
|
||||
|
||||
if (cekFile && body) {
|
||||
body.delete("data")
|
||||
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 = decodeURIComponent(file.name.replace("." + fExt, ""))
|
||||
const upload = await funUploadFile({ file: file, dirId: DIR.discussionDivision })
|
||||
if (upload.success) {
|
||||
await prisma.divisionDiscussionCommentFile.create({
|
||||
data: {
|
||||
idStorage: upload.data.id,
|
||||
idComment: data.id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const dataDivision = await prisma.divisionDisscussion.findUnique({
|
||||
where: {
|
||||
id: id
|
||||
@@ -162,7 +195,7 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { id } = context.params
|
||||
const { comment, user } = (await request.json());
|
||||
const { comment, user, filesToRemove } = (await request.json());
|
||||
|
||||
const userMobile = await funGetUserById({ id: String(user) })
|
||||
|
||||
@@ -187,14 +220,17 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
);
|
||||
}
|
||||
|
||||
const data = await prisma.divisionDisscussionComment.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
comment: comment,
|
||||
isEdited: true
|
||||
if (filesToRemove && filesToRemove.length > 0) {
|
||||
for (const fileId of filesToRemove) {
|
||||
const file = await prisma.divisionDiscussionCommentFile.findUnique({ where: { id: fileId } })
|
||||
if (file?.idStorage) await funDeleteFile({ id: file.idStorage })
|
||||
await prisma.divisionDiscussionCommentFile.update({ where: { id: fileId }, data: { isActive: false } })
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.divisionDisscussionComment.update({
|
||||
where: { id: id },
|
||||
data: { comment: comment, isEdited: true }
|
||||
})
|
||||
|
||||
// create log user
|
||||
@@ -237,13 +273,17 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await prisma.divisionDisscussionComment.update({
|
||||
where: {
|
||||
id: id
|
||||
},
|
||||
data: {
|
||||
isActive: false
|
||||
}
|
||||
const commentFiles = await prisma.divisionDiscussionCommentFile.findMany({
|
||||
where: { idComment: id, isActive: true }
|
||||
})
|
||||
for (const file of commentFiles) {
|
||||
if (file.idStorage) await funDeleteFile({ id: file.idStorage })
|
||||
await prisma.divisionDiscussionCommentFile.update({ where: { id: file.id }, data: { isActive: false } })
|
||||
}
|
||||
|
||||
await prisma.divisionDisscussionComment.update({
|
||||
where: { id: id },
|
||||
data: { isActive: false }
|
||||
})
|
||||
|
||||
// create log user
|
||||
|
||||
@@ -53,6 +53,15 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
name: true,
|
||||
img: true
|
||||
}
|
||||
},
|
||||
DivisionDiscussionCommentFile: {
|
||||
where: { isActive: true },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
extension: true,
|
||||
idStorage: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
@@ -61,12 +70,13 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
})
|
||||
|
||||
const omitMember = data.map((v: any) => ({
|
||||
..._.omit(v, ["User", "createdBy", "createdAt", "updatedAt"]),
|
||||
..._.omit(v, ["User", "createdBy", "createdAt", "updatedAt", "DivisionDiscussionCommentFile"]),
|
||||
idUser: v.createdBy,
|
||||
username: v.User.name,
|
||||
img: v.User.img,
|
||||
createdAt: countTime(v.createdAt),
|
||||
updatedAt: moment(v.updatedAt).format("ll")
|
||||
updatedAt: moment(v.updatedAt).format("ll"),
|
||||
files: v.DivisionDiscussionCommentFile
|
||||
}))
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan komentar", data: omitMember }, { status: 200 });
|
||||
|
||||
Reference in New Issue
Block a user