feat : user
This commit is contained in:
@@ -4,6 +4,7 @@ import { getOneUser } from "./get/getOneUser";
|
|||||||
import { updateUser } from "./post/updateUser";
|
import { updateUser } from "./post/updateUser";
|
||||||
import { deleteUser } from "./post/deleteUser";
|
import { deleteUser } from "./post/deleteUser";
|
||||||
import { createUser } from "./post/createUser";
|
import { createUser } from "./post/createUser";
|
||||||
|
import { getRoleUser } from "./get/getRoleUser";
|
||||||
|
|
||||||
export const API_INDEX_USER = [
|
export const API_INDEX_USER = [
|
||||||
{
|
{
|
||||||
@@ -31,4 +32,9 @@ export const API_INDEX_USER = [
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
bin: deleteUser,
|
bin: deleteUser,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: "get-role-user",
|
||||||
|
method: "GET",
|
||||||
|
bin: getRoleUser,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
@@ -1,20 +1,17 @@
|
|||||||
import { prisma } from "@/module/_global";
|
import { prisma } from "@/module/_global";
|
||||||
|
import _ from "lodash";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
|
|
||||||
export async function getAllUser(req: NextRequest) {
|
export async function getAllUser(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const searchParams = req.nextUrl.searchParams;
|
const searchParams = req.nextUrl.searchParams;
|
||||||
const idGroup = "2";
|
const idGroup = "group1";
|
||||||
const idVillage = "121212";
|
const idVillage = "desa1";
|
||||||
const active = searchParams.get("active");
|
const active = searchParams.get("active");
|
||||||
const idPosition = searchParams.get("idPosition");
|
|
||||||
const idUserRole = searchParams.get("idUserRole");
|
|
||||||
|
|
||||||
const users = await prisma.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
where: {
|
where: {
|
||||||
isActive: active == "true" ? true : false,
|
isActive: active == "true" ? true : false,
|
||||||
idUserRole: String(idUserRole),
|
|
||||||
idPosition: String(idPosition),
|
|
||||||
idVillage: String(idVillage),
|
idVillage: String(idVillage),
|
||||||
idGroup: String(idGroup),
|
idGroup: String(idGroup),
|
||||||
},
|
},
|
||||||
@@ -26,6 +23,11 @@ export async function getAllUser(req: NextRequest) {
|
|||||||
phone: true,
|
phone: true,
|
||||||
email: true,
|
email: true,
|
||||||
gender: true,
|
gender: true,
|
||||||
|
Position: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
Group: {
|
Group: {
|
||||||
select: {
|
select: {
|
||||||
name: true,
|
name: true,
|
||||||
@@ -34,7 +36,14 @@ export async function getAllUser(req: NextRequest) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return Response.json(users);
|
const allData = users.map((v: any) => ({
|
||||||
|
..._.omit(v, ["Group", "Position"]),
|
||||||
|
group: v.Group.name,
|
||||||
|
position: v.Position.name,
|
||||||
|
}));
|
||||||
|
|
||||||
|
return Response.json(allData);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
|
|||||||
21
src/module/user/api/get/getRoleUser.ts
Normal file
21
src/module/user/api/get/getRoleUser.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { prisma } from "@/module/_global";
|
||||||
|
import { NextRequest } from "next/server";
|
||||||
|
|
||||||
|
export async function getRoleUser(req: NextRequest) {
|
||||||
|
try {
|
||||||
|
const res = await prisma.userRole.findMany({
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return Response.json(res);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return Response.json(
|
||||||
|
{ success: false, message: "Internal Server Error" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,33 +5,48 @@ import { NextRequest } from "next/server";
|
|||||||
export async function createUser(req: NextRequest) {
|
export async function createUser(req: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const data = await req.json();
|
const data = await req.json();
|
||||||
|
const village = "desa1"
|
||||||
|
|
||||||
const users = await prisma.user.create({
|
const cek = await prisma.user.count({
|
||||||
data: {
|
where: {
|
||||||
nik: data.nik,
|
nik: data.nik,
|
||||||
name: data.name,
|
|
||||||
phone: data.phone,
|
|
||||||
email: data.email,
|
email: data.email,
|
||||||
gender: data.gender,
|
phone: data.phone
|
||||||
idGroup: data.idGroup,
|
|
||||||
idVillage: data.idVillage,
|
|
||||||
idPosition: data.idPosition,
|
|
||||||
idUserRole: data.idUserRole,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
nik: true,
|
|
||||||
name: true,
|
|
||||||
phone: true,
|
|
||||||
email: true,
|
|
||||||
gender: true,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// create log user
|
if (cek == 0) {
|
||||||
const log = await createLogUser({ act: 'CREATE', desc: 'User membuat data user baru', table: 'user', data: users.id })
|
const users = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
nik: data.nik,
|
||||||
|
name: data.name,
|
||||||
|
phone: data.phone,
|
||||||
|
email: data.email,
|
||||||
|
gender: data.gender,
|
||||||
|
idGroup: data.idGroup,
|
||||||
|
idVillage: village,
|
||||||
|
idPosition: data.idPosition,
|
||||||
|
idUserRole: data.idUserRole,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
nik: true,
|
||||||
|
name: true,
|
||||||
|
phone: true,
|
||||||
|
email: true,
|
||||||
|
gender: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// create log user
|
||||||
|
const log = await createLogUser({ act: 'CREATE', desc: 'User membuat data user baru', table: 'user', data: users.id })
|
||||||
|
|
||||||
|
return Response.json({ success: true, message: 'Sukses membuat user' }, { status: 200 });
|
||||||
|
} else {
|
||||||
|
return Response.json({ success: false, message: "User sudah ada" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return Response.json({ success: true, message: 'Sukses membuat user' }, { status: 200 });
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@@ -1,13 +1,123 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import { WARNA } from "@/module/_global";
|
import { API_ADDRESS, WARNA } from "@/module/_global";
|
||||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||||
import { Box, Button, Select, Stack, TextInput } from "@mantine/core";
|
import { Box, Button, Select, Stack, TextInput } from "@mantine/core";
|
||||||
import { useState } from "react";
|
import { useRouter } from "next/navigation";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { HiUser } from "react-icons/hi2";
|
import { HiUser } from "react-icons/hi2";
|
||||||
|
|
||||||
|
type dataGroup = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
type dataPosition = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type dataROleUser = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
export default function CreateMember() {
|
export default function CreateMember() {
|
||||||
|
const router = useRouter()
|
||||||
const [isModal, setModal] = useState(false)
|
const [isModal, setModal] = useState(false)
|
||||||
|
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||||
|
const [listPosition, setListPosition] = useState<dataPosition[]>([])
|
||||||
|
const [listUserRole, setListUserRole] = useState<dataROleUser[]>([])
|
||||||
|
|
||||||
|
const [listData, setListData] = useState({
|
||||||
|
nik: "",
|
||||||
|
name: "",
|
||||||
|
phone: "",
|
||||||
|
email: "",
|
||||||
|
gender: "",
|
||||||
|
idGroup: "",
|
||||||
|
idPosition: "",
|
||||||
|
idUserRole: "",
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
async function getAllGroup() {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=desa1&active=true`)
|
||||||
|
const data = await res.json()
|
||||||
|
setListGorup(data)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAllPosition(val: any) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_ADDRESS.apiGetAllPosition}&groupId=${val}&active=true`)
|
||||||
|
const data = await res.json()
|
||||||
|
setListPosition(data)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAllUserRole() {
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_ADDRESS.apiGetRoleUser}`)
|
||||||
|
const data = await res.json()
|
||||||
|
setListUserRole(data)
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function changeGrup(val: any) {
|
||||||
|
|
||||||
|
console.log(val)
|
||||||
|
setListPosition([])
|
||||||
|
setListData({
|
||||||
|
...listData,
|
||||||
|
idGroup: val,
|
||||||
|
idPosition: ""
|
||||||
|
})
|
||||||
|
|
||||||
|
getAllPosition(val)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function onSubmit(val: boolean) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(API_ADDRESS.apiCreateUser, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
nik: listData.nik,
|
||||||
|
name: listData.name,
|
||||||
|
phone: listData.phone,
|
||||||
|
email: listData.email,
|
||||||
|
gender: listData.gender,
|
||||||
|
idGroup: listData.idGroup,
|
||||||
|
idPosition: listData.idPosition,
|
||||||
|
idUserRole: listData.idUserRole
|
||||||
|
})
|
||||||
|
})
|
||||||
|
toast.success("Sukses! Data tersimpan");
|
||||||
|
setModal(false)
|
||||||
|
router.push('/member')
|
||||||
|
} catch (error) {
|
||||||
|
toast.error('Error')
|
||||||
|
toast.error("Sukses! Data tersimpan");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getAllGroup()
|
||||||
|
getAllUserRole()
|
||||||
|
}, [])
|
||||||
|
|
||||||
function onTrue(val: boolean) {
|
function onTrue(val: boolean) {
|
||||||
if (val) {
|
if (val) {
|
||||||
@@ -40,7 +150,17 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
data={['Dinas', 'Adat', 'LPD', 'PKK']}
|
data={
|
||||||
|
listGroup
|
||||||
|
? listGroup.map((data) => ({
|
||||||
|
value: data.id,
|
||||||
|
label: data.name,
|
||||||
|
}))
|
||||||
|
: []
|
||||||
|
}
|
||||||
|
onChange={(val: any) => {
|
||||||
|
changeGrup(val)
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<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}
|
||||||
@@ -51,7 +171,41 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
data={['Kepala', 'Sekretaris', 'Bendahara', 'Anggota']}
|
data={
|
||||||
|
listPosition
|
||||||
|
? listPosition.map((data) => ({
|
||||||
|
value: data.id,
|
||||||
|
label: data.name,
|
||||||
|
}))
|
||||||
|
: []
|
||||||
|
}
|
||||||
|
onChange={(val: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
idPosition: val
|
||||||
|
})}
|
||||||
|
value={listData.idPosition}
|
||||||
|
/>
|
||||||
|
<Select
|
||||||
|
placeholder="Pilih Role" label="User Role" w={"100%"} size="md" required withAsterisk radius={30}
|
||||||
|
styles={{
|
||||||
|
input: {
|
||||||
|
color: WARNA.biruTua,
|
||||||
|
borderRadius: WARNA.biruTua,
|
||||||
|
borderColor: WARNA.biruTua,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
data={
|
||||||
|
listUserRole
|
||||||
|
? listUserRole.map((data) => ({
|
||||||
|
value: data.id,
|
||||||
|
label: data.name,
|
||||||
|
}))
|
||||||
|
: []
|
||||||
|
}
|
||||||
|
onChange={(val: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
idUserRole: val
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
size="md" type="number" radius={30} placeholder="NIK" withAsterisk label="NIK" w={"100%"}
|
size="md" type="number" radius={30} placeholder="NIK" withAsterisk label="NIK" w={"100%"}
|
||||||
@@ -62,6 +216,10 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(event: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
nik: event.target.value
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
size="md" type="text" radius={30} placeholder="Nama" withAsterisk label="Nama" w={"100%"}
|
size="md" type="text" radius={30} placeholder="Nama" withAsterisk label="Nama" w={"100%"}
|
||||||
@@ -72,6 +230,10 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(event: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
name: event.target.value
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
size="md" type="email" radius={30} placeholder="Email" withAsterisk label="Email" w={"100%"}
|
size="md" type="email" radius={30} placeholder="Email" withAsterisk label="Email" w={"100%"}
|
||||||
@@ -82,6 +244,10 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(event: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
email: event.target.value
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
size="md" type="number" radius={30} placeholder="+62...." withAsterisk label="Nomor Telepon" w={"100%"}
|
size="md" type="number" radius={30} placeholder="+62...." withAsterisk label="Nomor Telepon" w={"100%"}
|
||||||
@@ -92,6 +258,10 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
|
onChange={(event: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
phone: event.target.value
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
placeholder="Pilih Gender" label="Gender" w={"100%"} size="md" required withAsterisk radius={30}
|
placeholder="Pilih Gender" label="Gender" w={"100%"} size="md" required withAsterisk radius={30}
|
||||||
@@ -102,7 +272,14 @@ export default function CreateMember() {
|
|||||||
borderColor: WARNA.biruTua,
|
borderColor: WARNA.biruTua,
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
data={['Laki-laki', 'Perempuan']}
|
data={[
|
||||||
|
{ value: 'M', label: 'Laki-laki' },
|
||||||
|
{ value: 'F', label: 'Perempuan' },
|
||||||
|
]}
|
||||||
|
onChange={(val: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
gender: val
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box mt={30} mx={20} pb={20}>
|
<Box mt={30} mx={20} pb={20}>
|
||||||
@@ -119,7 +296,7 @@ export default function CreateMember() {
|
|||||||
</Box>
|
</Box>
|
||||||
<LayoutModal opened={isModal} onClose={() => setModal(false)}
|
<LayoutModal opened={isModal} onClose={() => setModal(false)}
|
||||||
description="Apakah Anda yakin ingin menambahkan data?"
|
description="Apakah Anda yakin ingin menambahkan data?"
|
||||||
onYes={(val) => { onTrue(val) }} />
|
onYes={(val) => { onSubmit(val) }} />
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -8,17 +8,14 @@ import { HiMiniUser } from "react-icons/hi2"
|
|||||||
|
|
||||||
type dataMember = {
|
type dataMember = {
|
||||||
id: string,
|
id: string,
|
||||||
idUserRole: string,
|
isActive: boolean
|
||||||
idVillage: string,
|
|
||||||
idGroup: string,
|
|
||||||
idPosition: string,
|
|
||||||
nik: string,
|
nik: string,
|
||||||
name: string,
|
name: string,
|
||||||
phone: string,
|
phone: string,
|
||||||
email: string,
|
email: string,
|
||||||
gender: string,
|
gender: string,
|
||||||
isActive: boolean
|
group: string,
|
||||||
|
position: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function TabListMember({ status }: { status: boolean }) {
|
export default function TabListMember({ status }: { status: boolean }) {
|
||||||
@@ -30,7 +27,7 @@ export default function TabListMember({ status }: { status: boolean }) {
|
|||||||
async function getAllUser() {
|
async function getAllUser() {
|
||||||
try {
|
try {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const res = await fetch(`${API_ADDRESS.apiGetAllUser}&active` + status)
|
const res = await fetch(`${API_ADDRESS.apiGetAllUser}&active=` + status)
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
setDataMember(data)
|
setDataMember(data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -42,7 +39,7 @@ export default function TabListMember({ status }: { status: boolean }) {
|
|||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
getAllUser()
|
getAllUser()
|
||||||
}, [])
|
}, [status])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -62,7 +59,7 @@ export default function TabListMember({ status }: { status: boolean }) {
|
|||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={'bold'} c={WARNA.biruTua}>{v.name}</Text>
|
<Text fw={'bold'} c={WARNA.biruTua}>{v.name}</Text>
|
||||||
{/* <Text fw={'lighter'} fz={12}>{v.grup + ' - ' + v.desc}</Text> */}
|
<Text fw={'lighter'} fz={12}>{v.group + ' - ' + v.position}</Text>
|
||||||
</Box>
|
</Box>
|
||||||
</Group>
|
</Group>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
Reference in New Issue
Block a user