upd: task divisi
Deskripsi: - upload file - view file No Issues
This commit is contained in:
@@ -90,7 +90,8 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
extension: true
|
||||
extension: true,
|
||||
idStorage: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,6 +102,7 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
nameInStorage: v.ContainerFileDivision.id,
|
||||
name: v.ContainerFileDivision.name,
|
||||
extension: v.ContainerFileDivision.extension,
|
||||
idStorage: v.ContainerFileDivision.idStorage,
|
||||
}))
|
||||
|
||||
allData = fix
|
||||
@@ -287,7 +289,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data tugas divisi', table: 'divisionProject', data: id })
|
||||
|
||||
return NextResponse.json( { success: true, message: "Tugas berhasil diedit", }, { status: 200 } );
|
||||
return NextResponse.json({ success: true, message: "Tugas berhasil diedit", }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengedit tugas, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
@@ -42,7 +42,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
}
|
||||
})
|
||||
|
||||
fs.unlink(`./public/file/task/${dataFile?.id}.${dataFile?.extension}`, (err) => { })
|
||||
await funDeleteFile({ fileId: String(dataFile?.idStorage) })
|
||||
|
||||
const deleteRelasi = await prisma.divisionProjectFile.delete({
|
||||
where: {
|
||||
@@ -57,7 +57,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghpus file divisi', table: 'divisionProject', data: String(dataRelasi?.idProject) })
|
||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus file tugas divisi', table: 'divisionProject', data: String(dataRelasi?.idProject) })
|
||||
|
||||
return NextResponse.json({ success: true, message: "File berhasil dihapus", data, }, { status: 200 });
|
||||
|
||||
@@ -115,34 +115,29 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
|
||||
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
data: {
|
||||
idDivision: String(dataProject?.idDivision),
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
const upload = await funUploadFile({ file: file, dirId: DIR.task })
|
||||
if (upload.success) {
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
data: {
|
||||
idDivision: String(dataProject?.idDivision),
|
||||
name: fName,
|
||||
extension: String(fExt),
|
||||
idStorage: upload.data.id
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
const dataFile = {
|
||||
idProject: id,
|
||||
idDivision: dataProject?.idDivision,
|
||||
idFile: insertToContainer.id,
|
||||
createdBy: user.id,
|
||||
}
|
||||
})
|
||||
|
||||
const nameFix = insertToContainer.id + '.' + fExt
|
||||
const filePath = path.join(root, nameFix)
|
||||
// Konversi ArrayBuffer ke Buffer
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
// Tulis file ke sistem
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
|
||||
|
||||
const dataFile = {
|
||||
idProject: id,
|
||||
idDivision: dataProject?.idDivision,
|
||||
idFile: insertToContainer.id,
|
||||
createdBy: user.id,
|
||||
fileFix.push(dataFile)
|
||||
}
|
||||
|
||||
|
||||
fileFix.push(dataFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,12 +147,12 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User meambahkan file tugas divisi baru', table: 'divisionProject', data: id })
|
||||
return NextResponse.json({ success: true, message: "Berhasil membuat tugas divisi" }, { status: 200 });
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User menambahkan file tugas divisi baru', table: 'divisionProject', data: id })
|
||||
return NextResponse.json({ success: true, message: "Berhasil menambahkan file" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal membuat tugas divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
return NextResponse.json({ success: false, message: "Gagal menambahkan filae, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _, { ceil } from "lodash";
|
||||
import { NextResponse } from "next/server";
|
||||
@@ -153,42 +153,35 @@ export async function POST(request: Request) {
|
||||
let fileFix: any[] = []
|
||||
|
||||
if (cekFile) {
|
||||
const root = path.join(process.cwd(), "./public/file/task/");
|
||||
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.task })
|
||||
if (upload.success) {
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
data: {
|
||||
idDivision: idDivision,
|
||||
name: fName,
|
||||
extension: String(fExt),
|
||||
idStorage: upload.data.id
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
data: {
|
||||
const dataFile = {
|
||||
idProject: data.id,
|
||||
idDivision: idDivision,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
idFile: insertToContainer.id,
|
||||
createdBy: user.id,
|
||||
}
|
||||
})
|
||||
|
||||
const nameFix = insertToContainer.id + '.' + fExt
|
||||
const filePath = path.join(root, nameFix)
|
||||
// Konversi ArrayBuffer ke Buffer
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
// Tulis file ke sistem
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
|
||||
|
||||
const dataFile = {
|
||||
idProject: data.id,
|
||||
idDivision: idDivision,
|
||||
idFile: insertToContainer.id,
|
||||
createdBy: user.id,
|
||||
fileFix.push(dataFile)
|
||||
}
|
||||
|
||||
|
||||
fileFix.push(dataFile)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user