upd: divisi

Deskripsi:
- info divisi

No Issues
This commit is contained in:
amel
2024-08-12 14:09:44 +08:00
parent 655df62dc6
commit 3bf3eed9aa
16 changed files with 400 additions and 146 deletions

View File

@@ -154,4 +154,105 @@ export async function GET(request: Request, context: { params: { id: string } })
console.log(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
}
// MENGELUARKAN ANGGOTA DARI DIVISI
export async function DELETE(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 idDivision = context.params.id;
const { id } = (await request.json());
const data = await prisma.division.count({
where: {
id: idDivision,
isActive: true
},
});
if (data == 0) {
return NextResponse.json(
{
success: false,
message: "Hapus anggota divisi gagal, data tidak ditemukan",
},
{ status: 404 }
);
}
const update = await prisma.divisionMember.delete({
where: {
id: id,
},
});
return NextResponse.json(
{
success: true,
message: "Anggota divisi berhasil dihapus",
},
{ status: 200 }
);
} catch (error) {
console.log(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
}
// MENGGANTIS STATUS ADMIN DIVISI
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 idDivision = context.params.id;
const { id, isAdmin } = (await request.json());
const data = await prisma.division.count({
where: {
id: idDivision,
isActive: true
},
});
if (data == 0) {
return NextResponse.json(
{
success: false,
message: "Perubahan status admin gagal, data tidak ditemukan",
},
{ status: 404 }
);
}
const update = await prisma.divisionMember.update({
where: {
id: id,
},
data: {
isAdmin: !isAdmin
}
});
return NextResponse.json(
{
success: true,
message: "Status admin berhasil diupdate",
},
{ status: 200 }
);
} catch (error) {
console.log(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
}

View File

@@ -1,9 +1,10 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash";
import { NextResponse } from "next/server";
// GET ONE DATA DIVISI :: UNTUK TAMPIL DATA DI HALAMAN EDIT
// GET ONE DATA DIVISI :: UNTUK TAMPIL DATA DI HALAMAN EDIT DAN INFO
export async function GET(request: Request, context: { params: { id: string } }) {
try {
const { id } = context.params;
@@ -20,11 +21,43 @@ export async function GET(request: Request, context: { params: { id: string } })
});
if (!data) {
return NextResponse.json( { success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 } );
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan", }, { status: 404 });
}
return NextResponse.json( { success: true, message: "Berhasil mendapatkan divisi", data, }, { status: 200 } );
const member = await prisma.divisionMember.findMany({
where: {
idDivision: String(id),
isActive: true
},
select: {
id: true,
isAdmin: true,
isLeader: true,
idUser: true,
User: {
select: {
name: true
}
}
},
orderBy: {
isAdmin: 'desc',
}
})
const fixMember = member.map((v: any) => ({
..._.omit(v, ["User"]),
name: v.User.name
}))
const dataFix = {
division: data,
member: fixMember
}
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: dataFix, }, { status: 200 });
} catch (error) {
console.log(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });

View File

@@ -7,7 +7,6 @@ import { NextResponse } from "next/server";
// GET ALL MEMBER / USER
export async function GET(request: Request) {
try {
let fixGroup
const { searchParams } = new URL(request.url);
const name = searchParams.get('search')
@@ -23,6 +22,7 @@ export async function GET(request: Request) {
fixGroup = idGroup
}
const users = await prisma.user.findMany({
where: {
isActive: active == "true" ? true : false,