upd: user
Deskripsi: - pembtasan role pada anggota - tambah user api - edit user api No Issues
This commit is contained in:
@@ -55,10 +55,13 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
const group = users?.Group.name
|
||||
const position = users?.Position.name
|
||||
const idUserRole = users?.UserRole.id
|
||||
const phone = users?.phone.substr(2)
|
||||
|
||||
const result = { ...userData, group, position, idUserRole, phone };
|
||||
|
||||
const omitData = _.omit(result, ["Group", "Position", "UserRole"]);
|
||||
|
||||
const result = { ...userData, group, position, idUserRole };
|
||||
|
||||
const omitData = _.omit(result, ["Group", "Position", "UserRole"])
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
@@ -110,10 +113,13 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
},
|
||||
});
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate status anggota', table: 'user', data: id })
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan anggota",
|
||||
message: "Berhasil mengupdate status anggota",
|
||||
result,
|
||||
},
|
||||
{ status: 200 }
|
||||
@@ -121,7 +127,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
return NextResponse.json({ success: false, message: "Gagal mengupdate status anggota, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +141,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
}
|
||||
const { id } = context.params;
|
||||
console.log(id)
|
||||
|
||||
|
||||
const body = await request.formData()
|
||||
const file = body.get("file") as File
|
||||
const data = body.get("data")
|
||||
@@ -149,19 +155,35 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
idPosition,
|
||||
idUserRole
|
||||
} = JSON.parse(data as string)
|
||||
// const data = await request.json();
|
||||
const cek = await prisma.user.count({
|
||||
|
||||
const cekNIK = await prisma.user.count({
|
||||
where: {
|
||||
nik: nik,
|
||||
email: email,
|
||||
phone: phone,
|
||||
NOT: {
|
||||
id: id
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (cek == 0) {
|
||||
const cekEmail = await prisma.user.count({
|
||||
where: {
|
||||
email: email,
|
||||
NOT: {
|
||||
id: id
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const cekPhone = await prisma.user.count({
|
||||
where: {
|
||||
phone: "62" + phone,
|
||||
NOT: {
|
||||
id: id
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (cekNIK == 0 && cekEmail == 0 && cekPhone == 0) {
|
||||
const updates = await prisma.user.update({
|
||||
where: {
|
||||
id: id
|
||||
@@ -175,7 +197,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
idGroup: idGroup,
|
||||
idPosition: idPosition,
|
||||
idUserRole: idUserRole,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
img: true
|
||||
}
|
||||
@@ -187,7 +209,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fileName = id + '.' + fExt;
|
||||
const filePath = path.join(root, fileName);
|
||||
|
||||
|
||||
// Konversi ArrayBuffer ke Buffer
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
fs.writeFileSync(filePath, buffer);
|
||||
@@ -203,17 +225,17 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data user', table: 'user', data: user.id })
|
||||
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data anggota', table: 'user', data: user.id })
|
||||
|
||||
return Response.json(
|
||||
{ success: true, message: "Sukses Update User" },
|
||||
{ success: true, message: "Sukses update anggota" },
|
||||
{ status: 200 }
|
||||
);
|
||||
} else {
|
||||
return Response.json({ success: false, message: "User sudah ada" }, { status: 400 });
|
||||
return Response.json({ success: false, message: "Anggota sudah ada" }, { status: 400 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
return NextResponse.json({ success: false, message: "Gagal mengupdate data anggota, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
'use client'
|
||||
import { WARNA } from "@/module/_global";
|
||||
import { globalRole, WARNA } from "@/module/_global";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
import { funGetAllPosition } from "@/module/position/lib/api_position";
|
||||
@@ -14,6 +14,8 @@ import { funEditMember, funGetOneMember, funGetRoleUser } from "../lib/api_membe
|
||||
import _ from "lodash";
|
||||
import { Dropzone } from "@mantine/dropzone";
|
||||
import { FaCamera } from "react-icons/fa6";
|
||||
import { useHookstate } from "@hookstate/core";
|
||||
import { valueRoleUser } from "../../lib/val_user";
|
||||
|
||||
|
||||
export default function EditMember({ id }: { id: string }) {
|
||||
@@ -26,6 +28,7 @@ export default function EditMember({ id }: { id: string }) {
|
||||
const openRef = useRef<() => void>(null)
|
||||
const [img, setIMG] = useState<any | null>()
|
||||
const [loading, setLoading] = useState(true)
|
||||
const roleLogin = useHookstate(globalRole)
|
||||
const [touched, setTouched] = useState({
|
||||
nik: false,
|
||||
name: false,
|
||||
@@ -91,8 +94,7 @@ export default function EditMember({ id }: { id: string }) {
|
||||
|
||||
async function getAllUserRole() {
|
||||
try {
|
||||
const res = await funGetRoleUser();
|
||||
setListUserRole(res.data)
|
||||
setListUserRole(valueRoleUser.filter((v) => v.login == roleLogin.get())[0]?.data);
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
@@ -191,7 +193,7 @@ export default function EditMember({ id }: { id: string }) {
|
||||
:
|
||||
<>
|
||||
|
||||
<Select
|
||||
{/* <Select
|
||||
placeholder="Pilih Grup" label="Grup" w={"100%"} size="md" required withAsterisk radius={30}
|
||||
styles={{
|
||||
input: {
|
||||
@@ -219,7 +221,7 @@ export default function EditMember({ id }: { id: string }) {
|
||||
data.idGroup == "" ? "Grup Tidak Boleh Kosong" : null
|
||||
)
|
||||
}
|
||||
/>
|
||||
/> */}
|
||||
<Select
|
||||
placeholder="Pilih Jabatan" label="Jabatan" w={"100%"} size="md" required withAsterisk radius={30}
|
||||
styles={{
|
||||
|
||||
@@ -13,6 +13,7 @@ import { funGetOneMember } from "../lib/api_member";
|
||||
import toast from "react-hot-toast";
|
||||
import { IListMember, IMember } from "../lib/type_member";
|
||||
import { useHookstate } from "@hookstate/core";
|
||||
import { valueRoleUser } from "../../lib/val_user";
|
||||
|
||||
|
||||
export default function NavbarDetailMember({ id }: IMember) {
|
||||
@@ -21,6 +22,7 @@ export default function NavbarDetailMember({ id }: IMember) {
|
||||
const [selectId, setSelectId] = useState<string>('');
|
||||
const [active, setActive] = useState<boolean>(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [isEdit, setEdit] = useState(false)
|
||||
const roleLogin = useHookstate(globalRole)
|
||||
|
||||
useShallowEffect(() => {
|
||||
@@ -36,6 +38,7 @@ export default function NavbarDetailMember({ id }: IMember) {
|
||||
setDataOne(respose.data)
|
||||
setActive(respose.data?.isActive)
|
||||
setSelectId(respose.data?.id)
|
||||
setEdit(valueRoleUser.filter((v) => v.login == roleLogin.get())[0]?.data.some((i: any) => i.id == respose.data.idUserRole))
|
||||
} else {
|
||||
toast.error(respose.message)
|
||||
}
|
||||
@@ -57,7 +60,7 @@ export default function NavbarDetailMember({ id }: IMember) {
|
||||
<Group justify="space-between">
|
||||
<LayoutIconBack />
|
||||
{
|
||||
(roleLogin.get() != "user") &&
|
||||
(roleLogin.get() != "user") && isEdit &&
|
||||
<ActionIcon onClick={() => setOpen(true)} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
|
||||
<HiMenu size={20} color='white' />
|
||||
</ActionIcon>
|
||||
@@ -78,7 +81,7 @@ export default function NavbarDetailMember({ id }: IMember) {
|
||||
</>
|
||||
:
|
||||
<>
|
||||
<Text c={'white'} fw={'bold'} fz={25}>{dataOne?.name}</Text>
|
||||
<Text c={'white'} fw={'bold'} fz={25} ta={"center"}>{dataOne?.name}</Text>
|
||||
<Text c={'white'} fw={'lighter'} fz={15}>{dataOne?.group} - {dataOne?.position}</Text>
|
||||
</>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user