From 02b25ffc84a940d1499104065a4a33535ac8a496 Mon Sep 17 00:00:00 2001 From: bagasbanuna Date: Wed, 17 Dec 2025 17:40:56 +0800 Subject: [PATCH] Penerapaan ke database untuk token device Add: src/app/api/mobile/auth/device-tokens/[id]/ Fix: modified: src/app/api/mobile/auth/device-tokens/route.ts modified: src/app/api/mobile/notifications/route.ts ### No Issue --- .../mobile/auth/device-tokens/[id]/route.ts | 44 +++++++++++++++++++ .../api/mobile/auth/device-tokens/route.ts | 26 +++++++++-- src/app/api/mobile/notifications/route.ts | 43 +++++++++++------- 3 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/app/api/mobile/auth/device-tokens/[id]/route.ts diff --git a/src/app/api/mobile/auth/device-tokens/[id]/route.ts b/src/app/api/mobile/auth/device-tokens/[id]/route.ts new file mode 100644 index 00000000..72ce5ed5 --- /dev/null +++ b/src/app/api/mobile/auth/device-tokens/[id]/route.ts @@ -0,0 +1,44 @@ +import { NextRequest, NextResponse } from "next/server"; +import { prisma } from "@/lib"; + +export { DELETE }; + +async function DELETE( + request: NextRequest, + { params }: { params: { id: string } } +) { + const { id } = params; + try { + const findFirst = await prisma.tokenUserDevice.findFirst({ + where: { + userId: id, + }, + }); + + if (!findFirst) { + return NextResponse.json({ + success: false, + message: "User tidak ditemukan !", + }); + } + + const deleted = await prisma.tokenUserDevice.delete({ + where: { + id: findFirst.id, + }, + }); + + console.log("DEL", deleted); + + return NextResponse.json({ + success: true, + message: "Berhasil menghapus device token user", + }); + } catch (error) { + console.log("ERROR", error); + return NextResponse.json( + { error: (error as Error).message, message: "Terjadi error pada API" }, + { status: 500 } + ); + } +} diff --git a/src/app/api/mobile/auth/device-tokens/route.ts b/src/app/api/mobile/auth/device-tokens/route.ts index 010aef4b..9d743e80 100644 --- a/src/app/api/mobile/auth/device-tokens/route.ts +++ b/src/app/api/mobile/auth/device-tokens/route.ts @@ -1,13 +1,14 @@ import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib"; -export async function POST(request: NextRequest) { +export { POST, GET }; + +async function POST(request: NextRequest) { const { data } = await request.json(); try { console.log("Data >>", JSON.stringify(data, null, 2)); - const { userId, platform, deviceId, model, appVersion, fcmToken } = - data; + const { userId, platform, deviceId, model, appVersion, fcmToken } = data; if (!fcmToken) { return NextResponse.json({ error: "Missing Token" }, { status: 400 }); @@ -62,3 +63,22 @@ export async function POST(request: NextRequest) { ); } } + +async function GET(request: NextRequest) { + try { + const data = await prisma.tokenUserDevice.findMany({ + where: { + isActive: true, + }, + }); + + return NextResponse.json({ success: true, data }); + + + } catch (error) { + return NextResponse.json( + { error: (error as Error).message }, + { status: 500 } + ); + } +} diff --git a/src/app/api/mobile/notifications/route.ts b/src/app/api/mobile/notifications/route.ts index 08cd879e..372d65e4 100644 --- a/src/app/api/mobile/notifications/route.ts +++ b/src/app/api/mobile/notifications/route.ts @@ -5,7 +5,7 @@ import { NextRequest, NextResponse } from "next/server"; export async function POST(request: NextRequest) { try { const { data } = await request.json(); - const { fcmToken, title, body: notificationBody } = data; + const { fcmToken, title, body: notificationBody, userLoginId } = data; console.log("Data Notifikasi >>", data); @@ -16,24 +16,37 @@ export async function POST(request: NextRequest) { ); } - const message = { - token: fcmToken, - notification: { - title, - body: notificationBody || "", + const deviceToken = await prisma.tokenUserDevice.findMany({ + where: { + isActive: true, + NOT: { + userId: userLoginId, + }, }, - data: { - sentAt: new Date().toISOString(), // ✅ Simpan metadata di data - // contoh: senderId, type, etc. - }, - }; + }); - console.log("[MSG]", message); + for (let i of deviceToken) { + const message = { + token: i.token, + notification: { + title, + body: notificationBody || "", + }, + data: { + sentAt: new Date().toISOString(), // ✅ Simpan metadata di data + // contoh: senderId, type, etc. + }, + }; + console.log("[MSG]", message); - const response = await adminMessaging.send(message); - console.log("✅ FCM sent:", response); + const response = await adminMessaging.send(message); + console.log("✅ FCM sent:", response); + } - return NextResponse.json({ success: true, messageId: response }); + return NextResponse.json({ + success: true, + message: "Notification sent successfully", + }); } catch (error: any) { console.error("❌ FCM error:", error); return NextResponse.json(