feat : update project
This commit is contained in:
@@ -61,7 +61,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
const { id } = context.params
|
||||
const { idUser } = (await request.json());
|
||||
|
||||
const data = await prisma.divisionProject.count({
|
||||
const data = await prisma.project.count({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
@@ -91,4 +91,94 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mengeluarkan anggota project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
//GET MEMBER ALL USER ID GROUP
|
||||
export async function GET(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 groupId = user.idGroup
|
||||
const userId = user.id
|
||||
|
||||
const data = await prisma.project.findUnique({
|
||||
where: {
|
||||
id: String(id),
|
||||
isActive: true
|
||||
}
|
||||
})
|
||||
|
||||
if (data == null) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal, data tugas tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// const member = await prisma.projectMember.findMany({
|
||||
// where: {
|
||||
// idProject: String(id),
|
||||
// isActive: true
|
||||
// },
|
||||
// select: {
|
||||
// id: true,
|
||||
// isLeader: true,
|
||||
// idUser: true,
|
||||
// User: {
|
||||
// select: {
|
||||
// name: true
|
||||
// }
|
||||
// }
|
||||
// },
|
||||
// orderBy: {
|
||||
// isLeader: 'desc',
|
||||
// }
|
||||
// })
|
||||
|
||||
// const fixMember = member.map((v: any) => ({
|
||||
// ..._.omit(v, ["User"]),
|
||||
// name: v.User.name
|
||||
// }))
|
||||
|
||||
const member = await prisma.user.findMany({
|
||||
where: {
|
||||
idGroup: String(groupId),
|
||||
id: {
|
||||
not: String(userId)
|
||||
},
|
||||
isActive: true
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
}
|
||||
})
|
||||
|
||||
const fixMember = member.map((v: any) => ({
|
||||
idUser: v.id,
|
||||
name: v.name,
|
||||
email: v.email,
|
||||
|
||||
}))
|
||||
|
||||
const dataFix = {
|
||||
project: data,
|
||||
member: fixMember
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan project", data: dataFix, }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,6 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
}
|
||||
})
|
||||
|
||||
console.log(dataProgress)
|
||||
const semua = dataProgress.length
|
||||
const selesai = _.filter(dataProgress, { status: 1 }).length
|
||||
const progress = Math.ceil((selesai / semua) * 100)
|
||||
@@ -185,10 +184,10 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
}
|
||||
|
||||
const { id } = context.params
|
||||
const { idTask } = await request.json()
|
||||
const data = await prisma.projectTask.count({
|
||||
const { reason } = await request.json()
|
||||
const data = await prisma.project.count({
|
||||
where: {
|
||||
id: String(idTask)
|
||||
id: id
|
||||
}
|
||||
})
|
||||
|
||||
@@ -207,7 +206,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
},
|
||||
data: {
|
||||
status: 3,
|
||||
reason: idTask
|
||||
reason: reason
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
@@ -52,3 +53,137 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
|
||||
|
||||
// EDIT STATUS DETAIL PROJECT
|
||||
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 { status, idProject } = (await request.json());
|
||||
|
||||
const data = await prisma.projectTask.count({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
})
|
||||
|
||||
if (data == 0) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false, message: "Gagal mendapatkan project, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const dataCreate = await prisma.projectTask.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: {
|
||||
status: status
|
||||
}
|
||||
})
|
||||
|
||||
const dataTask = await prisma.projectTask.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
idProject: idProject,
|
||||
}
|
||||
})
|
||||
|
||||
const semua = dataTask.length
|
||||
const selesai = dataTask.filter((item) => item.status == 1).length
|
||||
const prosess = Math.ceil((selesai / semua) * 100)
|
||||
let statusProject = 1
|
||||
|
||||
if (prosess == 100) {
|
||||
statusProject = 2
|
||||
} else if (prosess == 0) {
|
||||
statusProject = 0
|
||||
}
|
||||
|
||||
|
||||
const update = await prisma.project.update({
|
||||
where: {
|
||||
id: idProject
|
||||
},
|
||||
data: {
|
||||
status: statusProject
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Status detail Project berhasil diupdate", data }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal membatalkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// GET ONE DETAIL PROJECT
|
||||
export async function GET(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.projectTask.findUnique({
|
||||
where: {
|
||||
id: String(id),
|
||||
isActive: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!data) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false, message: "Gagal mendapatkan project, data tidak ditemukan",
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, message: "Detail project berhasil ditemukan", data }, { status: 200 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// EDIT DETAIL 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 { name, dateStart, dateEnd } = (await request.json());
|
||||
|
||||
const data = await prisma.projectTask.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: {
|
||||
name: name,
|
||||
dateStart: new Date(moment(dateStart).format('YYYY-MM-DD')),
|
||||
dateEnd: new Date(moment(dateEnd).format('YYYY-MM-DD')),
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, message: "Detail project berhasil diupdate", data }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal membatalkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import _ from "lodash";
|
||||
import moment from "moment";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
|
||||
@@ -62,4 +63,89 @@ export async function GET(request: Request) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan project, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// CREATE PROJECT
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
if (user.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { idVillage, idGroup, name, task, member, file } = (await request.json())
|
||||
const userId = user.id
|
||||
|
||||
|
||||
const data = await prisma.project.create({
|
||||
data: {
|
||||
idVillage: String(idVillage),
|
||||
idGroup: String(idGroup),
|
||||
name: name,
|
||||
desc: "",
|
||||
createdBy: String(userId)
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
})
|
||||
|
||||
if (task.length > 0) {
|
||||
const dataProject = task.map((v: any) => ({
|
||||
..._.omit(v, ["dateStart", "dateEnd", "name"]),
|
||||
idProject: data.id,
|
||||
name: v.name,
|
||||
dateStart: new Date(moment(v.dateStart).format('YYYY-MM-DD')),
|
||||
dateEnd: new Date(moment(v.dateEnd).format('YYYY-MM-DD')),
|
||||
}))
|
||||
|
||||
const insertTask = await prisma.projectTask.createMany({
|
||||
data: dataProject
|
||||
})
|
||||
}
|
||||
|
||||
if (member.length > 0) {
|
||||
const dataMember = member.map((v: any) => ({
|
||||
..._.omit(v, ["idUser", "name"]),
|
||||
idProject: data.id,
|
||||
idUser: v.idUser,
|
||||
name: v.name
|
||||
}))
|
||||
|
||||
const insertMember = await prisma.projectMember.createMany({
|
||||
data: dataMember
|
||||
})
|
||||
}
|
||||
|
||||
let fileFix: any[] = []
|
||||
|
||||
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()
|
||||
// funUploadFile(fName, f)
|
||||
|
||||
const dataFile = {
|
||||
name: fName,
|
||||
extension: fExt,
|
||||
idProject: data.id,
|
||||
}
|
||||
|
||||
fileFix.push(dataFile)
|
||||
})
|
||||
|
||||
const insertFile = await prisma.divisionProjectFile.createMany({
|
||||
data: fileFix
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: data, }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user