88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
import { prisma } from "@/lib";
|
|
import _ from "lodash";
|
|
import { NextResponse } from "next/server";
|
|
|
|
export { GET, PUT };
|
|
|
|
async function GET(request: Request, { params }: { params: { id: string } }) {
|
|
const { id } = params;
|
|
|
|
try {
|
|
const data = await prisma.user.findUnique({
|
|
where: {
|
|
id: id,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({
|
|
status: 200,
|
|
success: true,
|
|
message: "Success get data user access",
|
|
data: data,
|
|
});
|
|
} catch (error) {
|
|
console.log("[ERROR]", error);
|
|
return NextResponse.json({
|
|
status: 500,
|
|
success: false,
|
|
message: "Error get data user access",
|
|
reason: (error as Error).message,
|
|
});
|
|
}
|
|
}
|
|
|
|
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
|
const { id } = params;
|
|
const { data } = await request.json();
|
|
|
|
try {
|
|
if (data.active) {
|
|
const updateData = await prisma.user.update({
|
|
where: {
|
|
id: id,
|
|
},
|
|
data: {
|
|
active: data.active,
|
|
},
|
|
});
|
|
|
|
console.log("[Update Active Berhasil]", updateData);
|
|
} else if (data.role) {
|
|
const fixName = _.startCase(data.role.replace(/_/g, " "));
|
|
|
|
const checkRole = await prisma.masterUserRole.findFirst({
|
|
where: {
|
|
name: fixName,
|
|
},
|
|
});
|
|
|
|
console.log("[CHECK ROLE]", checkRole);
|
|
|
|
const updateData = await prisma.user.update({
|
|
where: {
|
|
id: id,
|
|
},
|
|
data: {
|
|
masterUserRoleId: checkRole?.id,
|
|
},
|
|
});
|
|
|
|
console.log("[Update Role Berhasil]", updateData);
|
|
}
|
|
|
|
return NextResponse.json({
|
|
status: 200,
|
|
success: true,
|
|
message: "Success update data user access",
|
|
});
|
|
} catch (error) {
|
|
console.log("[ERROR]", error);
|
|
return NextResponse.json({
|
|
status: 500,
|
|
success: false,
|
|
message: "Error update data user access",
|
|
reason: (error as Error).message,
|
|
});
|
|
}
|
|
}
|