Files
sistem-desa-mandiri/src/app/api/mobile/task/detail/[id]/route.ts
amal d000910b9f upd : api mobile detail tugas task divisi
Deskripsi:
- api get list detail tugas task divisi
- api tambah detail tugas task divisi
- api edit detail tugas task divisi
- api tambah data task divisi > detail tugas

No Issues
2025-08-20 17:13:07 +08:00

288 lines
9.2 KiB
TypeScript

import { prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import _ from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";
// HAPUS DETAIL TASK
export async function DELETE(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params;
const { idProject, user } = (await request.json());
const userMobile = await funGetUserById({ id: String(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.divisionProjectTask.count({
where: {
id: id,
},
});
if (data == 0) {
return NextResponse.json(
{
success: false,
message: "Hapus tugas gagal, data tidak ditemukan",
},
{ status: 200 }
);
}
const update = await prisma.divisionProjectTask.update({
where: {
id: id,
},
data: {
isActive: false,
},
});
// const cek progress
const dataTask = await prisma.divisionProjectTask.findMany({
where: {
isActive: true,
idProject: idProject
}
})
const semua = dataTask.length
const selesai = _.filter(dataTask, { status: 1 }).length
const progress = Math.ceil((selesai / semua) * 100)
let statusProject = 1
if (progress == 100) {
statusProject = 2
} else if (progress == 0) {
statusProject = 0
}
const updProject = await prisma.divisionProject.update({
where: {
id: idProject
},
data: {
status: statusProject
}
})
// create log user
const log = await createLogUserMobile({ act: 'DELETE', desc: 'User menghapus detail task divisi', table: 'divisionProjectTask', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Tugas berhasil dihapus", }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menghapus tugas divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// EDIT STATUS DETAIL TASK
export async function PUT(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params;
const { status, idProject, user } = (await request.json());
const userMobile = await funGetUserById({ id: String(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.divisionProjectTask.count({
where: {
id: id,
},
});
if (data == 0) {
return NextResponse.json(
{
success: false,
message: "Update status detail tugas gagal, data tidak ditemukan",
},
{ status: 200 }
);
}
const update = await prisma.divisionProjectTask.update({
where: {
id: id,
},
data: {
status: status,
},
});
// const cek progress
const dataTask = await prisma.divisionProjectTask.findMany({
where: {
isActive: true,
idProject: idProject
}
})
const semua = dataTask.length
const selesai = _.filter(dataTask, { status: 1 }).length
const progress = Math.ceil((selesai / semua) * 100)
let statusProject = 1
if (progress == 100) {
statusProject = 2
} else if (progress == 0) {
statusProject = 0
}
const updProject = await prisma.divisionProject.update({
where: {
id: idProject
},
data: {
status: statusProject
}
})
// create log user
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status detail task divisi', table: 'divisionProjectTask', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Status detail tugas berhasil diupdate", data, }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mengupdate status detail tugas, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// GET ONE DETAIL TASK
export async function GET(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params;
const { searchParams } = new URL(request.url);
const user = searchParams.get("user");
const kategori = searchParams.get("cat");
const userMobile = await funGetUserById({ id: String(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.divisionProjectTask.findUnique({
where: {
id: String(id),
isActive: true
}
});
if (!data) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan detail tugas, data tidak ditemukan" }, { status: 200 });
}
let dataFix
if (kategori == "detailTask") {
const dataDetail = await prisma.divisionProjectTaskDetail.findMany({
where: {
idTask: String(id)
},
orderBy: {
date: "asc"
}
})
const dataDetailFix = dataDetail.map((data: any) => ({
...data,
date: moment(data?.date).format('DD-MM-YYYY'),
timeStart: data.timeStart == null ? "" : moment.utc(data.timeStart).format("HH:mm"),
timeEnd: data.timeEnd == null ? "" : moment.utc(data.timeEnd).format("HH:mm")
}))
dataFix = dataDetailFix
} else {
dataFix = data
}
return NextResponse.json({ success: true, message: "Berhasil mendapatkan detail tugas divisi", data: dataFix }, { status: 200 });
}
catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan detail tugas divisi, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}
// EDIT DETAIL TASK
export async function POST(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params;
const { title, dateStart, dateEnd, user, dataDetail } = (await request.json());
const userMobile = await funGetUserById({ id: String(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.divisionProjectTask.count({
where: {
id: id,
},
});
if (data == 0) {
return NextResponse.json(
{
success: false,
message: "Edit detail tugas gagal, data tidak ditemukan",
},
{ status: 200 }
);
}
const update = await prisma.divisionProjectTask.update({
where: {
id: id,
},
data: {
title,
dateStart: new Date(dateStart),
dateEnd: new Date(dateEnd),
},
});
const dataDetailDelete = await prisma.divisionProjectTaskDetail.deleteMany({
where: {
idTask: id
}
})
if (dataDetail.length > 0) {
const dataDetailFix = dataDetail.map((v: any) => ({
...v,
idTask: id,
date: new Date(v.date),
timeStart: v.timeStart == null ? null : new Date(new Date('1970-01-01 ' + v.timeStart).getTime() - (new Date('1970-01-01 ' + v.timeStart).getTimezoneOffset() * 60000)).toISOString(),
timeEnd: v.timeEnd == null ? null : new Date(new Date('1970-01-01 ' + v.timeEnd).getTime() - (new Date('1970-01-01 ' + v.timeEnd).getTimezoneOffset() * 60000)).toISOString(),
}))
const dataDetailCreate = await prisma.divisionProjectTaskDetail.createMany({
data: dataDetailFix
})
}
// create log user
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data detail task divisi', table: 'divisionProjectTask', data: id, user: userMobile.id })
return NextResponse.json({ success: true, message: "Detail tugas berhasil diedit", data, }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mengedit detail tugas, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}