upd: user

Deskripsi:
- pembtasan role pada anggota
- tambah user api
- edit user api

No Issues
This commit is contained in:
amel
2024-09-05 11:25:49 +08:00
parent e4cc2e4f62
commit 0cc9f56ec8
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 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 });
}
}