Merge branch 'join' into lukman/2-september-2024
This commit is contained in:
@@ -70,7 +70,7 @@ export async function GET(request: Request) {
|
||||
allData = data.map((v: any) => ({
|
||||
..._.omit(v, ["ProjectTask", "createdAt"]),
|
||||
progress: ceil((v.ProjectTask.filter((i: any) => i.status == 1).length * 100) / v.ProjectTask.length),
|
||||
createdAt: moment(v.dateStart).format("LL")
|
||||
createdAt: moment(v.createdAt).format("LL")
|
||||
}))
|
||||
|
||||
} else if (kategori == "division") {
|
||||
|
||||
@@ -109,7 +109,12 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
img: true
|
||||
img: true,
|
||||
Position: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -119,7 +124,8 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
..._.omit(v, ["User"]),
|
||||
name: v.User.name,
|
||||
email: v.User.email,
|
||||
img: v.User.img
|
||||
img: v.User.img,
|
||||
position: v.User.Position.name
|
||||
}))
|
||||
|
||||
allData = fix
|
||||
|
||||
203
src/app/api/project/file/[id]/route.ts
Normal file
203
src/app/api/project/file/[id]/route.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import _ from "lodash";
|
||||
|
||||
// HAPUS FILE PROJECT BUKAN PAKE ISACTIVE
|
||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
const { id } = context.params;
|
||||
const data = await prisma.projectFile.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Hapus file gagal, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const dataRelasi = await prisma.projectFile.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
fs.unlink(`./public/file/project/${dataRelasi?.id}.${dataRelasi?.extension}`, (err) => { })
|
||||
|
||||
const deleteRelasi = await prisma.projectFile.delete({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "File berhasil dihapus",
|
||||
data,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal menghapus file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CEK FILE PROJECT APAKAH PERNAH DIUPLOAD PADA PROJECT YG SAMA
|
||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
|
||||
const { id } = context.params;
|
||||
const body = await request.formData()
|
||||
const file = body.get("file") as File
|
||||
const fileName = file.name
|
||||
|
||||
const dataCek = await prisma.project.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (dataCek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Upload file gagal, data kegiatan tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const dataTaskFile = await prisma.projectFile.findMany({
|
||||
where: {
|
||||
idProject: id,
|
||||
isActive: true
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
extension: true
|
||||
}
|
||||
})
|
||||
|
||||
const dataOmit = dataTaskFile.map((v: any) => ({
|
||||
..._.omit(v, [""]),
|
||||
file: v.name + '.' + v.extension,
|
||||
}))
|
||||
|
||||
const cek = dataOmit.some((i: any) => i.file == fileName)
|
||||
|
||||
|
||||
if (cek) {
|
||||
return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 400 });
|
||||
} else {
|
||||
return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 });
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return NextResponse.json({ success: false, message: "Upload file gagal, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TAMBAH FILE PROJECT
|
||||
export async function POST(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
|
||||
const { id } = context.params;
|
||||
const body = await request.formData()
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
const dataCek = await prisma.project.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (dataCek == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Tambah file kegiatan gagal, data tugas tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const dataProject = await prisma.project.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (cekFile) {
|
||||
let a = 0
|
||||
const root = path.join(process.cwd(), "./public/file/project/");
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]) == "file" + a) {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
|
||||
|
||||
const insertToTable = await prisma.projectFile.create({
|
||||
data: {
|
||||
idProject: id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
const nameFix = insertToTable.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);
|
||||
}
|
||||
a++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mengupload file kegiatan" }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengupload file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
|
||||
// GET ALL DATA PROJECT
|
||||
@@ -80,7 +81,11 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { idGroup, title, task, member, file } = (await request.json())
|
||||
const body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
const { idGroup, title, task, member } = JSON.parse(dataBody as string)
|
||||
const userId = user.id
|
||||
|
||||
|
||||
@@ -112,7 +117,7 @@ export async function POST(request: Request) {
|
||||
|
||||
if (member.length > 0) {
|
||||
const dataMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "name"]),
|
||||
..._.omit(v, ["idUser", "name", "img"]),
|
||||
idProject: data.id,
|
||||
idUser: v.idUser,
|
||||
}))
|
||||
@@ -122,27 +127,36 @@ export async function POST(request: Request) {
|
||||
})
|
||||
}
|
||||
|
||||
let fileFix: any[] = []
|
||||
if (cekFile) {
|
||||
let a = 0
|
||||
const root = path.join(process.cwd(), "./public/file/project/");
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]) == "file" + a) {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
|
||||
if (file.length > 0) {
|
||||
file.map((v: any, index: any) => {
|
||||
const f: any = file[index].get('file')
|
||||
const fName = f.name
|
||||
const fExt = fName.split(".").pop()
|
||||
|
||||
const dataFile = {
|
||||
name: fName,
|
||||
extension: fExt,
|
||||
idProject: data.id,
|
||||
const insertToTable = await prisma.projectFile.create({
|
||||
data: {
|
||||
idProject: data.id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
const nameFix = insertToTable.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);
|
||||
}
|
||||
|
||||
fileFix.push(dataFile)
|
||||
})
|
||||
|
||||
const insertFile = await prisma.divisionProjectFile.createMany({
|
||||
data: fileFix
|
||||
})
|
||||
|
||||
a++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -113,7 +113,12 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
select: {
|
||||
name: true,
|
||||
email: true,
|
||||
img: true
|
||||
img: true,
|
||||
Position: {
|
||||
select: {
|
||||
name: true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,7 +129,8 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
..._.omit(v, ["User"]),
|
||||
name: v.User.name,
|
||||
email: v.User.email,
|
||||
img: v.User.img
|
||||
img: v.User.img,
|
||||
position: v.User.Position.name
|
||||
}))
|
||||
|
||||
allData = fix
|
||||
|
||||
@@ -116,8 +116,8 @@ export async function POST(request: Request, context: { params: { id: string } }
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]) == "file" + a) {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fName = file.name.split(".")[0]
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
|
||||
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
@@ -218,14 +218,13 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
file: v.ContainerFileDivision.name + '.' + v.ContainerFileDivision.extension,
|
||||
}))
|
||||
|
||||
|
||||
const cek = dataOmit.some((i: any) => i.file == fileName)
|
||||
|
||||
|
||||
if (cek) {
|
||||
return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 });
|
||||
} else {
|
||||
return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 400 });
|
||||
} else {
|
||||
return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 });
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
@@ -157,8 +157,8 @@ export async function POST(request: Request) {
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]) == "file" + a) {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fName = file.name.split(".")[0]
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
|
||||
|
||||
const insertToContainer = await prisma.containerFileDivision.create({
|
||||
|
||||
Reference in New Issue
Block a user