upd: mobile api

Deskripsi:
- api mobile jabatan crud

No Issuesf
This commit is contained in:
amel
2025-04-29 17:21:21 +08:00
parent 0f575c4efc
commit e364c4901d
2 changed files with 285 additions and 0 deletions

View File

@@ -0,0 +1,144 @@
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: 401 });
}
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: 404 }
);
}
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: 401 });
}
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: 401 });
}
const cek = await prisma.position.count({
where: {
name: name,
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: 400 }
);
}
} 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 });
}
}

View File

@@ -0,0 +1,141 @@
import { prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import _ from "lodash";
import { NextResponse } from "next/server";
// GET ALL POSITION
export async function GET(request: Request) {
try {
let grup
const { searchParams } = new URL(request.url);
const idGroup = searchParams.get("group");
const active = searchParams.get('active');
const name = searchParams.get('search')
const userMobile = searchParams.get("user")
if (userMobile == "null" || userMobile == undefined || userMobile == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const user = await funGetUserById({ id: userMobile })
if (idGroup == "null" || idGroup == undefined || idGroup == "") {
grup = user.idGroup
} else {
grup = idGroup
}
const cek = await prisma.group.count({
where: {
id: grup,
isActive: true
}
})
if (cek == 0) {
return NextResponse.json({ success: false, message: "Gagal mendapatkan jabatan, data tidak ditemukan", }, { status: 404 });
}
const filter = await prisma.group.findUnique({
where: {
id: grup
},
select: {
id: true,
name: true
}
})
const positions = await prisma.position.findMany({
where: {
idGroup: grup,
isActive: active == 'false' ? false : true,
name: {
contains: (name == undefined || name == null) ? "" : name,
mode: "insensitive"
}
},
select: {
id: true,
name: true,
isActive: true,
Group: {
select: {
name: true
}
}
},
});
const allData = positions.map((v: any) => ({
..._.omit(v, ["Group", "name"]),
name: v.name,
group: v.Group.name
}))
const dataFix = _.orderBy(allData, [data => data.name.toLowerCase()], ['asc']);
return NextResponse.json({ success: true, message: "Berhasil mendapatkan jabatan", data: dataFix, filter }, { 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 });
}
}
// CREATE POSITION
export async function POST(request: Request) {
try {
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: 401 });
}
const userMobile = await funGetUserById({ id: user })
let groupFix = idGroup
if (groupFix == null || groupFix == undefined || groupFix == "") {
groupFix = userMobile.idGroup
}
const cek = await prisma.position.count({
where: {
name: name,
idGroup: groupFix,
},
});
if (cek == 0) {
const positions = await prisma.position.create({
data: {
name: name,
idGroup: groupFix,
},
select: {
id: true,
name: true,
idGroup: true
},
});
// create log user
const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data jabatan baru', table: 'position', data: positions.id, user })
return NextResponse.json({ success: true, message: "Berhasil menambahkan jabatan", positions, }, { status: 200 });
} else {
return NextResponse.json(
{ success: false, message: "Jabatan sudah ada" },
{ status: 400 }
);
}
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menambahkan jabatan, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
}