147 lines
4.7 KiB
TypeScript
147 lines
4.7 KiB
TypeScript
import { prisma } from "@/module/_global";
|
|
import { createLogUserMobile } from "@/module/user";
|
|
import { NextResponse } from "next/server";
|
|
|
|
// GET ONE JABATAN
|
|
export async function GET(request: Request, context: { params: { id: string } }) {
|
|
try {
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const userMobile = searchParams.get("user")
|
|
const { id } = context.params;
|
|
|
|
if (userMobile == "null" || userMobile == undefined || userMobile == "") {
|
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
|
}
|
|
|
|
const data = await prisma.position.findUnique({
|
|
where: {
|
|
id: id,
|
|
},
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
idGroup: true,
|
|
},
|
|
});
|
|
if (!data) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: "Gagal mendapatkan jabatan, data tidak ditemukan",
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{
|
|
success: true,
|
|
message: "Berhasil mendapatkan jabatan",
|
|
data,
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// DELETE / ACTIVE & NON ACTIVE POSITION
|
|
export async function DELETE(request: Request, context: { params: { id: string } }) {
|
|
try {
|
|
const { id } = context.params;
|
|
const { isActive, user } = (await request.json());
|
|
|
|
if (user == "null" || user == undefined || user == "") {
|
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
|
}
|
|
|
|
const data = await prisma.position.count({
|
|
where: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
if (data == 0) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
message: "Gagal mengubah status jabatan, data tidak ditemukan",
|
|
},
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
|
|
const update = await prisma.position.update({
|
|
where: {
|
|
id: id,
|
|
},
|
|
data: {
|
|
isActive: !isActive,
|
|
},
|
|
});
|
|
|
|
// create log user
|
|
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate status data jabatan', table: 'position', data: id, user })
|
|
return NextResponse.json(
|
|
{ success: true, message: "Berhasil mengubah status jabatan" },
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ success: false, message: "Gagal mengubah status jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
|
|
// UPDATE POSITION
|
|
export async function PUT(request: Request, context: { params: { id: string } }) {
|
|
try {
|
|
const { id } = context.params;
|
|
const { name, idGroup, user } = await request.json();
|
|
|
|
if (user == "null" || user == undefined || user == "") {
|
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
|
|
}
|
|
|
|
const cek = await prisma.position.count({
|
|
where: {
|
|
name: {
|
|
equals: name,
|
|
mode: "insensitive"
|
|
},
|
|
idGroup: idGroup,
|
|
NOT: {
|
|
id: id
|
|
}
|
|
},
|
|
});
|
|
|
|
if (cek == 0) {
|
|
const positions = await prisma.position.update({
|
|
where: {
|
|
id: id,
|
|
},
|
|
data: {
|
|
name: name,
|
|
},
|
|
});
|
|
|
|
// create log user
|
|
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data jabatan', table: 'position', data: id, user })
|
|
return NextResponse.json({ success: true, message: "Berhasil mengedit jabatan", }, { status: 200 });
|
|
} else {
|
|
return NextResponse.json(
|
|
{ success: false, message: "Jabatan sudah ada" },
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ success: false, message: "Gagal mengedit jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
|
|
}
|
|
} |