upd: api mobile

Deskripsi:
- update api mobile register dan unregister token device

No Issues
This commit is contained in:
amel
2025-06-24 17:39:26 +08:00
parent cb020b7567
commit 466698f038
4 changed files with 81 additions and 23 deletions

View File

@@ -0,0 +1,61 @@
import { prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
try {
const { token, user } = (await request.json());
const userMobile = await funGetUserById({ id: user })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const cek = await prisma.tokenDeviceUser.count({
where: {
idUser: userMobile.id,
token
}
})
if (cek == 0) {
const data = await prisma.tokenDeviceUser.create({
data: {
token,
idUser: userMobile.id
}
});
return NextResponse.json({ success: true, message: "Berhasil mendaftarkan token" }, { status: 200 });
}
return NextResponse.json({ success: true, message: "Token sudah terdaftar" }, { status: 200 })
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menambahkan token, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
};
export async function PUT(request: Request) {
try {
const { token, user } = (await request.json());
const userMobile = await funGetUserById({ id: user })
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
}
const data = await prisma.tokenDeviceUser.deleteMany({
where: {
token,
idUser: userMobile.id
}
});
return NextResponse.json({ success: true, message: "Berhasil menghapus token", }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ success: false, message: "Gagal menghapus token, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
}
};