feat : update profile

This commit is contained in:
lukman
2024-08-28 14:32:00 +08:00
parent 0d16adbd2f
commit d88e42e8a8
17 changed files with 660 additions and 179 deletions

View File

@@ -0,0 +1,59 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { NextResponse } from "next/server";
// UPDATE PROFILE BY COOKIES
export async function PUT(request: Request, context: { params: { id: string } }) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const { id } = context.params;
const data = await request.json();
const cek = await prisma.user.count({
where: {
id: id,
nik: data.nik,
email: data.email,
phone: data.phone
}
})
if (cek == 0) {
return NextResponse.json(
{
success: false,
message: "Gagal mendapatkan profile, data tidak ditemukan",
},
{ status: 404 }
);
}
const result = await prisma.user.update({
where: {
id: id,
},
data: {
nik: data.nik,
name: data.name,
email: data.email,
phone: data.phone,
gender: data.gender,
},
});
return NextResponse.json(
{
success: true,
message: "Berhasil mendapatkan profile",
result,
},
{ status: 200 }
);
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal mendapatkan anggota, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
}
}

View File

@@ -0,0 +1,82 @@
import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash";
import { NextResponse } from "next/server";
// GET PROFILE BY COOKIES
export async function GET(request: Request) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const data = await prisma.user.findUnique({
where: {
id: user.id
},
select: {
id: true,
name: true,
email: true,
phone: true,
nik: true,
gender: true,
idGroup: true,
idPosition: true,
Group: {
select: {
name: true
}
},
Position: {
select: {
name: true
}
}
}
})
const { ...userData } = data;
const group = data?.Group.name
const position = data?.Position.name
const result = { ...userData, group, position };
const omitData = _.omit(result, ["Group", "Position",])
return NextResponse.json({ success: true, data: omitData });
} catch (error) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
}
// UPDATE PROFILE BY COOKIES
export async function PUT(request: Request) {
try {
const user = await funGetUserByCookies()
if (user.id == undefined) {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
}
const body = await request.json();
const { name, email, phone, nik, gender } = body;
const data = await prisma.user.update({
where: {
id: user.id
},
data: {
name: name,
email: email,
phone: phone,
nik: nik,
gender: gender
}
})
return NextResponse.json({ success: true, message: "Berhasil ubah profile", data: data });
} catch (error) {
return NextResponse.json({ success: false, message: "Gagal ubah profile" }, { status: 401 });
}
}