API Mobile

Fix:
- api/mobile/profile/route.tsx: hapus validasi email dan di buat khusus
- api/mobile/profile/[id]/route.ts: cat untuk memilih update apa yang di lakikan

Add:
- api/mobile/validate/: Validasi email

### No Issue
This commit is contained in:
2025-08-27 16:13:52 +08:00
parent b605177e0d
commit 6a614191e7
3 changed files with 103 additions and 40 deletions

View File

@@ -47,45 +47,82 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
} }
try { try {
let message;
const { id } = params; const { id } = params;
const body = await request.json(); const body = await request.json();
const { data } = body; const { data } = body;
const { searchParams } = new URL(request.url);
const category = searchParams.get("category");
const cekEmail = await prisma.profile.findUnique({ if (category === "profile") {
where: { const cekEmail = await prisma.profile.findUnique({
email: data.email, where: {
}, email: data.email,
}); },
});
if (cekEmail && cekEmail.id != id)
return NextResponse.json({
success: false,
message: "Email sudah digunakan",
});
const updateData = await prisma.profile.update({
where: {
id: id,
},
data: {
name: data.name,
email: data.email,
alamat: data.alamat,
jenisKelamin: data.jenisKelamin,
},
});
if (!updateData) {
return NextResponse.json({ success: false, message: "Gagal update" });
}
if (cekEmail && cekEmail.id != id) message = "Berhasil edit profile";
return NextResponse.json({ } else if (category === "photo") {
success: false, const updateData = await prisma.profile.update({
message: "Email sudah digunakan", where: {
id: id,
},
data: {
imageId: data.fileId,
},
}); });
const updateData = await prisma.profile.update({ if (!updateData) {
where: { return NextResponse.json({
id: id, success: false,
}, message: "Gagal update foto",
data: { });
name: data.name, }
email: data.email,
alamat: data.alamat,
jenisKelamin: data.jenisKelamin,
},
});
if (!updateData) { message = "Berhasil edit foto profile";
return NextResponse.json({ success: false, message: "Gagal update" }); } else if (category === "background") {
const updateData = await prisma.profile.update({
where: {
id: id,
},
data: {
imageBackgroundId: data.fileId,
},
});
if (!updateData) {
return NextResponse.json({
success: false,
message: "Gagal update background",
});
}
message = "Berhasil edit background profile";
} }
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
message: "Berhasil edit profile", message: message,
}); });
} catch (error) { } catch (error) {
backendLogger.error("Error edit profile", error);
return NextResponse.json( return NextResponse.json(
{ {
success: false, success: false,

View File

@@ -7,27 +7,15 @@ async function POST(request: Request) {
try { try {
const { data } = await request.json(); const { data } = await request.json();
const existingEmail = await prisma.profile.findUnique({ await prisma.profile.create({
where: {
email: data.email,
},
});
if (existingEmail) {
return NextResponse.json({
success: false,
message: "Email telah digunakan",
status: 400,
});
}
const create = await prisma.profile.create({
data: { data: {
userId: data.id, userId: data.id,
name: data.name, name: data.name,
email: data.email, email: data.email,
alamat: data.alamat, alamat: data.alamat,
jenisKelamin: data.jenisKelamin, jenisKelamin: data.jenisKelamin,
imageId: data.imageId || "",
imageBackgroundId: data.imageBackgroundId || "",
}, },
}); });

View File

@@ -0,0 +1,38 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib";
export async function POST(request: Request) {
const { data } = await request.json();
const email = data;
try {
const existingEmail = await prisma.profile.findUnique({
where: {
email: email,
},
});
if (existingEmail) {
return NextResponse.json({
success: false,
message: "Email telah digunakan",
status: 400,
});
}
return NextResponse.json({
success: true,
message: "Email tidak digunakan",
status: 200,
});
} catch (error) {
console.log("error", error);
return NextResponse.json(
{
success: false,
message: "Gagal validasi email",
},
{ status: 500 }
);
}
}