upd: api mobile
Deskripsi: - update api mobile project -nb: blm selesai No Issues'
This commit is contained in:
@@ -1,17 +1,20 @@
|
|||||||
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
|
||||||
import { funGetUserByCookies } from "@/module/auth";
|
import { funGetUserById } from "@/module/auth";
|
||||||
import { createLogUser } from "@/module/user";
|
import { createLogUserMobile } from "@/module/user";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
// HAPUS FILE PROJECT BUKAN PAKE ISACTIVE
|
// HAPUS FILE PROJECT BUKAN PAKE ISACTIVE
|
||||||
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
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 { id } = context.params;
|
||||||
|
const { user } = (await request.json());
|
||||||
|
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 data = await prisma.projectFile.count({
|
const data = await prisma.projectFile.count({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
@@ -24,7 +27,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
success: false,
|
success: false,
|
||||||
message: "Hapus file gagal, data tidak ditemukan",
|
message: "Hapus file gagal, data tidak ditemukan",
|
||||||
},
|
},
|
||||||
{ status: 404 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,14 +46,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
});
|
});
|
||||||
|
|
||||||
// create log user
|
// create log user
|
||||||
const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus file kegiatan', table: 'project', data: String(dataRelasi?.idProject) })
|
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus file kegiatan', table: 'project', data: String(dataRelasi?.idProject), user: userMobile.id })
|
||||||
|
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
message: "File berhasil dihapus",
|
message: "File berhasil dihapus",
|
||||||
data,
|
|
||||||
},
|
},
|
||||||
{ status: 200 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
@@ -65,14 +67,15 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
|||||||
// CEK FILE PROJECT APAKAH PERNAH DIUPLOAD PADA PROJECT YG SAMA
|
// CEK FILE PROJECT APAKAH PERNAH DIUPLOAD PADA PROJECT YG SAMA
|
||||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
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 { id } = context.params;
|
||||||
const body = await request.formData()
|
const body = await request.formData()
|
||||||
|
const data = JSON.parse(body.get("data") as string)
|
||||||
|
const user = await funGetUserById({ id: data.user })
|
||||||
|
|
||||||
|
if (user.id == "null" || user.id == undefined || user.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
const file = body.get("file") as File
|
const file = body.get("file") as File
|
||||||
const fileName = file.name
|
const fileName = file.name
|
||||||
|
|
||||||
@@ -88,7 +91,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
success: false,
|
success: false,
|
||||||
message: "Upload file gagal, data kegiatan tidak ditemukan",
|
message: "Upload file gagal, data kegiatan tidak ditemukan",
|
||||||
},
|
},
|
||||||
{ status: 404 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +116,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
|
|
||||||
|
|
||||||
if (cek) {
|
if (cek) {
|
||||||
return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 400 });
|
return NextResponse.json({ success: false, message: "File sudah pernah diupload" }, { status: 200 });
|
||||||
} else {
|
} else {
|
||||||
return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Cek berhasil" }, { status: 200 });
|
||||||
}
|
}
|
||||||
@@ -128,14 +131,15 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
|||||||
// TAMBAH FILE PROJECT
|
// TAMBAH FILE PROJECT
|
||||||
export async function POST(request: Request, context: { params: { id: string } }) {
|
export async function POST(request: Request, context: { params: { id: string } }) {
|
||||||
try {
|
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 { id } = context.params;
|
||||||
const body = await request.formData()
|
const body = await request.formData()
|
||||||
|
const data = JSON.parse(body.get("data") as string)
|
||||||
|
const user = await funGetUserById({ id: data.user })
|
||||||
|
|
||||||
|
if (user.id == "null" || user.id == undefined || user.id == "") {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
const cekFile = body.has("file0")
|
const cekFile = body.has("file0")
|
||||||
|
|
||||||
const dataCek = await prisma.project.count({
|
const dataCek = await prisma.project.count({
|
||||||
@@ -150,46 +154,39 @@ export async function POST(request: Request, context: { params: { id: string } }
|
|||||||
success: false,
|
success: false,
|
||||||
message: "Tambah file kegiatan gagal, data tugas tidak ditemukan",
|
message: "Tambah file kegiatan gagal, data tugas tidak ditemukan",
|
||||||
},
|
},
|
||||||
{ status: 404 }
|
{ status: 200 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataProject = await prisma.project.findUnique({
|
|
||||||
where: {
|
|
||||||
id: id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (cekFile) {
|
if (cekFile) {
|
||||||
for (var pair of body.entries()) {
|
console.log('masuk file')
|
||||||
if (String(pair[0]).substring(0, 4) == "file") {
|
// for (var pair of body.entries()) {
|
||||||
const file = body.get(pair[0]) as File
|
// if (String(pair[0]).substring(0, 4) == "file") {
|
||||||
const fExt = file.name.split(".").pop()
|
// const file = body.get(pair[0]) as File
|
||||||
const fName = file.name.replace("." + fExt, "")
|
// const fExt = file.name.split(".").pop()
|
||||||
|
// const fName = file.name.replace("." + fExt, "")
|
||||||
|
|
||||||
const upload = await funUploadFile({ file: file, dirId: DIR.project })
|
// const upload = await funUploadFile({ file: file, dirId: DIR.project })
|
||||||
if (upload.success) {
|
// if (upload.success) {
|
||||||
const insertToTable = await prisma.projectFile.create({
|
// const insertToTable = await prisma.projectFile.create({
|
||||||
data: {
|
// data: {
|
||||||
idStorage: upload.data.id,
|
// idStorage: upload.data.id,
|
||||||
idProject: id,
|
// idProject: id,
|
||||||
name: fName,
|
// name: fName,
|
||||||
extension: String(fExt),
|
// extension: String(fExt),
|
||||||
|
|
||||||
},
|
// },
|
||||||
select: {
|
// select: {
|
||||||
id: true
|
// id: true
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// create log user
|
// // create log user
|
||||||
const log = await createLogUser({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id) })
|
// const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah file kegiatan', table: 'project', data: String(id), user: user.id })
|
||||||
return NextResponse.json({ success: true, message: "Berhasil mengupload file kegiatan" }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Berhasil mengupload file kegiatan" }, { status: 200 });
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user