upd: api diskusi umum
Deskripsi: - tambah file pada data diskusi umum - detail file pada get one diskusi umum No Issues
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { countTime, prisma } from "@/module/_global";
|
||||
import { countTime, DIR, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
@@ -8,7 +8,7 @@ import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
// GET ONE DETAIL DISKUSI UMUM
|
||||
export async function GET(request : Request, context: { params: { id: string } }) {
|
||||
export async function GET(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
let dataFix
|
||||
const { id } = context.params
|
||||
@@ -127,8 +127,21 @@ export async function GET(request : Request, context: { params: { id: string } }
|
||||
} else {
|
||||
dataFix = false
|
||||
}
|
||||
}
|
||||
} else if (kategori == "file") {
|
||||
const data = await prisma.discussionFile.findMany({
|
||||
where: {
|
||||
idDiscussion: id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idStorage: true,
|
||||
name: true,
|
||||
extension: true
|
||||
}
|
||||
})
|
||||
|
||||
dataFix = data
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan diskusi", data: dataFix }, { status: 200 });
|
||||
|
||||
@@ -247,7 +260,12 @@ 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 { title, desc, user } = (await request.json());
|
||||
const body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
// const { title, desc, user } = (await request.json());
|
||||
const { title, desc, user, oldFile } = JSON.parse(dataBody as string)
|
||||
|
||||
const userMobile = await funGetUserById({ id: String(user) })
|
||||
|
||||
@@ -275,6 +293,41 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
}
|
||||
});
|
||||
|
||||
if (oldFile.length > 0) {
|
||||
for (let index = 0; index < oldFile.length; index++) {
|
||||
const element = oldFile[index];
|
||||
if (element.delete) {
|
||||
await prisma.discussionFile.delete({
|
||||
where: {
|
||||
id: element.id
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cekFile) {
|
||||
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.discussion })
|
||||
if (upload.success) {
|
||||
await prisma.discussionFile.create({
|
||||
data: {
|
||||
idStorage: upload.data.id,
|
||||
idDiscussion: id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data diskusi umum', table: 'discussion', data: id, user: userMobile.id })
|
||||
return NextResponse.json({ success: true, message: "Berhasil mengedit diskusi umum" }, { status: 200 });
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
@@ -112,13 +112,20 @@ export async function GET(request: Request) {
|
||||
// CREATE DISCUSSION GENERALE
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { idGroup, user, title, desc, member } = await request.json();
|
||||
|
||||
if (user == "null" || user == undefined || user == "") {
|
||||
const body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
// const { idGroup, user, title, desc, member } = await request.json();
|
||||
const { idGroup, user, title, desc, member } = JSON.parse(dataBody as string)
|
||||
|
||||
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 userMobile = await funGetUserById({ id: user })
|
||||
const userId = user
|
||||
const userRoleLogin = userMobile.idUserRole
|
||||
|
||||
@@ -145,6 +152,29 @@ export async function POST(request: Request) {
|
||||
data: dataMember
|
||||
})
|
||||
|
||||
|
||||
if (cekFile) {
|
||||
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.discussion })
|
||||
if (upload.success) {
|
||||
await prisma.discussionFile.create({
|
||||
data: {
|
||||
idStorage: upload.data.id,
|
||||
idDiscussion: data.id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const memberNotifMobile = await prisma.discussionMember.findMany({
|
||||
where: {
|
||||
idDiscussion: data.id
|
||||
|
||||
Reference in New Issue
Block a user