Fix: - prisma/migrations/20251223084450_add_recipient_and_sender Add: - prisma/schema.prisma - src/app/api/mobile/auth/device-tokens/[id]/route.ts - src/app/api/mobile/auth/device-tokens/route.ts - src/app/api/mobile/notification/[id]/unread-count/route.ts - src/app/api/mobile/notification/route.ts ### No Issue
87 lines
1.8 KiB
TypeScript
87 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib";
|
|
|
|
export { POST, GET };
|
|
|
|
async function POST(request: NextRequest) {
|
|
const { data } = await request.json();
|
|
try {
|
|
|
|
const { userId, platform, deviceId, model, appVersion, fcmToken } = data;
|
|
|
|
if (!fcmToken) {
|
|
return NextResponse.json({ error: "Missing Token" }, { status: 400 });
|
|
}
|
|
|
|
const existing = await prisma.tokenUserDevice.findFirst({
|
|
where: {
|
|
token: fcmToken,
|
|
userId: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
});
|
|
|
|
|
|
console.log("✅ EX", existing);
|
|
|
|
let deviceToken;
|
|
|
|
if (existing) {
|
|
deviceToken = await prisma.tokenUserDevice.update({
|
|
where: {
|
|
id: existing?.id,
|
|
},
|
|
data: {
|
|
platform,
|
|
deviceId,
|
|
model,
|
|
appVersion,
|
|
isActive: true,
|
|
updatedAt: new Date(),
|
|
},
|
|
});
|
|
} else {
|
|
// Buat baru jika belum ada
|
|
deviceToken = await prisma.tokenUserDevice.create({
|
|
data: {
|
|
token: fcmToken,
|
|
userId: userId,
|
|
platform,
|
|
deviceId,
|
|
model,
|
|
appVersion,
|
|
isActive: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ success: true, data: deviceToken });
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: (error as Error).message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
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 }
|
|
);
|
|
}
|
|
}
|