Merge pull request #198 from bipproduction/amalia/05-september-24

upd: user
This commit is contained in:
Amalia
2024-09-05 11:27:51 +08:00
committed by GitHub
3 changed files with 50 additions and 23 deletions

View File

@@ -55,10 +55,13 @@ export async function GET(request: Request, context: { params: { id: string } })
const group = users?.Group.name const group = users?.Group.name
const position = users?.Position.name const position = users?.Position.name
const idUserRole = users?.UserRole.id 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( 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( return NextResponse.json(
{ {
success: true, success: true,
message: "Berhasil mendapatkan anggota", message: "Berhasil mengupdate status anggota",
result, result,
}, },
{ status: 200 } { status: 200 }
@@ -121,7 +127,7 @@ export async function DELETE(request: Request, context: { params: { id: string }
} catch (error) { } catch (error) {
console.error(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; const { id } = context.params;
console.log(id) console.log(id)
const body = await request.formData() const body = await request.formData()
const file = body.get("file") as File const file = body.get("file") as File
const data = body.get("data") const data = body.get("data")
@@ -149,19 +155,35 @@ export async function PUT(request: Request, context: { params: { id: string } })
idPosition, idPosition,
idUserRole idUserRole
} = JSON.parse(data as string) } = JSON.parse(data as string)
// const data = await request.json();
const cek = await prisma.user.count({ const cekNIK = await prisma.user.count({
where: { where: {
nik: nik, nik: nik,
email: email,
phone: phone,
NOT: { NOT: {
id: id 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({ const updates = await prisma.user.update({
where: { where: {
id: id id: id
@@ -175,7 +197,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
idGroup: idGroup, idGroup: idGroup,
idPosition: idPosition, idPosition: idPosition,
idUserRole: idUserRole, idUserRole: idUserRole,
}, },
select: { select: {
img: true img: true
} }
@@ -187,7 +209,7 @@ export async function PUT(request: Request, context: { params: { id: string } })
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fileName = id + '.' + fExt; const fileName = id + '.' + fExt;
const filePath = path.join(root, fileName); const filePath = path.join(root, fileName);
// Konversi ArrayBuffer ke Buffer // Konversi ArrayBuffer ke Buffer
const buffer = Buffer.from(await file.arrayBuffer()); const buffer = Buffer.from(await file.arrayBuffer());
fs.writeFileSync(filePath, buffer); fs.writeFileSync(filePath, buffer);
@@ -203,17 +225,17 @@ export async function PUT(request: Request, context: { params: { id: string } })
} }
// create log user // 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( return Response.json(
{ success: true, message: "Sukses Update User" }, { success: true, message: "Sukses update anggota" },
{ status: 200 } { status: 200 }
); );
} else { } 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) { } catch (error) {
console.error(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 });
} }
} }

View File

@@ -1,5 +1,5 @@
'use client' 'use client'
import { WARNA } from "@/module/_global"; import { globalRole, WARNA } from "@/module/_global";
import LayoutModal from "@/module/_global/layout/layout_modal"; import LayoutModal from "@/module/_global/layout/layout_modal";
import { funGetAllGroup, IDataGroup } from "@/module/group"; import { funGetAllGroup, IDataGroup } from "@/module/group";
import { funGetAllPosition } from "@/module/position/lib/api_position"; import { funGetAllPosition } from "@/module/position/lib/api_position";
@@ -14,6 +14,8 @@ import { funEditMember, funGetOneMember, funGetRoleUser } from "../lib/api_membe
import _ from "lodash"; import _ from "lodash";
import { Dropzone } from "@mantine/dropzone"; import { Dropzone } from "@mantine/dropzone";
import { FaCamera } from "react-icons/fa6"; import { FaCamera } from "react-icons/fa6";
import { useHookstate } from "@hookstate/core";
import { valueRoleUser } from "../../lib/val_user";
export default function EditMember({ id }: { id: string }) { export default function EditMember({ id }: { id: string }) {
@@ -26,6 +28,7 @@ export default function EditMember({ id }: { id: string }) {
const openRef = useRef<() => void>(null) const openRef = useRef<() => void>(null)
const [img, setIMG] = useState<any | null>() const [img, setIMG] = useState<any | null>()
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const roleLogin = useHookstate(globalRole)
const [touched, setTouched] = useState({ const [touched, setTouched] = useState({
nik: false, nik: false,
name: false, name: false,
@@ -91,8 +94,7 @@ export default function EditMember({ id }: { id: string }) {
async function getAllUserRole() { async function getAllUserRole() {
try { try {
const res = await funGetRoleUser(); setListUserRole(valueRoleUser.filter((v) => v.login == roleLogin.get())[0]?.data);
setListUserRole(res.data)
} catch (error) { } catch (error) {
console.error(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} placeholder="Pilih Grup" label="Grup" w={"100%"} size="md" required withAsterisk radius={30}
styles={{ styles={{
input: { input: {
@@ -219,7 +221,7 @@ export default function EditMember({ id }: { id: string }) {
data.idGroup == "" ? "Grup Tidak Boleh Kosong" : null data.idGroup == "" ? "Grup Tidak Boleh Kosong" : null
) )
} }
/> /> */}
<Select <Select
placeholder="Pilih Jabatan" label="Jabatan" w={"100%"} size="md" required withAsterisk radius={30} placeholder="Pilih Jabatan" label="Jabatan" w={"100%"} size="md" required withAsterisk radius={30}
styles={{ styles={{

View File

@@ -13,6 +13,7 @@ import { funGetOneMember } from "../lib/api_member";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { IListMember, IMember } from "../lib/type_member"; import { IListMember, IMember } from "../lib/type_member";
import { useHookstate } from "@hookstate/core"; import { useHookstate } from "@hookstate/core";
import { valueRoleUser } from "../../lib/val_user";
export default function NavbarDetailMember({ id }: IMember) { export default function NavbarDetailMember({ id }: IMember) {
@@ -21,6 +22,7 @@ export default function NavbarDetailMember({ id }: IMember) {
const [selectId, setSelectId] = useState<string>(''); const [selectId, setSelectId] = useState<string>('');
const [active, setActive] = useState<boolean>(false) const [active, setActive] = useState<boolean>(false)
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [isEdit, setEdit] = useState(false)
const roleLogin = useHookstate(globalRole) const roleLogin = useHookstate(globalRole)
useShallowEffect(() => { useShallowEffect(() => {
@@ -36,6 +38,7 @@ export default function NavbarDetailMember({ id }: IMember) {
setDataOne(respose.data) setDataOne(respose.data)
setActive(respose.data?.isActive) setActive(respose.data?.isActive)
setSelectId(respose.data?.id) setSelectId(respose.data?.id)
setEdit(valueRoleUser.filter((v) => v.login == roleLogin.get())[0]?.data.some((i: any) => i.id == respose.data.idUserRole))
} else { } else {
toast.error(respose.message) toast.error(respose.message)
} }
@@ -57,7 +60,7 @@ export default function NavbarDetailMember({ id }: IMember) {
<Group justify="space-between"> <Group justify="space-between">
<LayoutIconBack /> <LayoutIconBack />
{ {
(roleLogin.get() != "user") && (roleLogin.get() != "user") && isEdit &&
<ActionIcon onClick={() => setOpen(true)} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info"> <ActionIcon onClick={() => setOpen(true)} variant="light" bg={WARNA.bgIcon} size="lg" radius="lg" aria-label="Info">
<HiMenu size={20} color='white' /> <HiMenu size={20} color='white' />
</ActionIcon> </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> <Text c={'white'} fw={'lighter'} fz={15}>{dataOne?.group} - {dataOne?.position}</Text>
</> </>
} }