Fix database notification untuk mobile
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
This commit is contained in:
@@ -8,10 +8,17 @@ async function DELETE(
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
const { id } = params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const deviceId = searchParams.get("deviceId");
|
||||
|
||||
console.log("ID", id);
|
||||
console.log("DEVICE ID", deviceId);
|
||||
|
||||
try {
|
||||
const findFirst = await prisma.tokenUserDevice.findFirst({
|
||||
where: {
|
||||
userId: id,
|
||||
deviceId: deviceId as any,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ 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;
|
||||
|
||||
|
||||
@@ -7,21 +7,19 @@ export async function GET(
|
||||
) {
|
||||
const { id } = params;
|
||||
|
||||
console.log("Id >>", id);
|
||||
|
||||
try {
|
||||
const data = await prisma.notifikasi.findMany({
|
||||
const data = await prisma.notifikasi.count({
|
||||
where: {
|
||||
userId: id,
|
||||
recipientId: id,
|
||||
isRead: false,
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Data >>", data);
|
||||
console.log("List Notification >>", data);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: data.length,
|
||||
data: data,
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
@@ -30,3 +28,7 @@ export async function GET(
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
type Pilihan = "PENGIRIM" | "PENERIMA";
|
||||
|
||||
const data: Pilihan = "PENERIMA";
|
||||
|
||||
@@ -1,29 +1,39 @@
|
||||
// app/api/test/notifications/route.ts
|
||||
import { prisma } from "@/lib";
|
||||
import { adminMessaging } from "@/lib/firebase-admin";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib";
|
||||
|
||||
type NotificationProp = {
|
||||
title: string;
|
||||
body: string;
|
||||
userLoginId: string;
|
||||
appId?: string;
|
||||
status?: string;
|
||||
kategoriApp?: string;
|
||||
type?: string;
|
||||
deepLink?: string;
|
||||
// sendTo: "user" | "admin" | "all";
|
||||
};
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { data } = await request.json();
|
||||
|
||||
const {
|
||||
fcmToken,
|
||||
title,
|
||||
body: notificationBody,
|
||||
userLoginId,
|
||||
type,
|
||||
kategoriApp,
|
||||
} = data;
|
||||
appId,
|
||||
status,
|
||||
deepLink,
|
||||
// sendTo,
|
||||
} = data as NotificationProp;
|
||||
|
||||
console.log("Data Notifikasi >>", data);
|
||||
|
||||
if (!fcmToken || !title) {
|
||||
return NextResponse.json(
|
||||
{ error: "Missing fcmToken or title" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
console.log("Notification Send >>", data);
|
||||
|
||||
// Cari user yang login
|
||||
const findUserLogin = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userLoginId,
|
||||
@@ -34,82 +44,273 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||
// Cari token fcm user yang login
|
||||
const checkFcmToken = await prisma.tokenUserDevice.findFirst({
|
||||
where: {
|
||||
isActive: true,
|
||||
NOT: {
|
||||
userId: findUserLogin.id,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
user: true,
|
||||
userId: findUserLogin.id,
|
||||
},
|
||||
});
|
||||
|
||||
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);
|
||||
if (!checkFcmToken) {
|
||||
return NextResponse.json(
|
||||
{ error: "FCM Token not found" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
if (i.user?.masterUserRoleId === "1") {
|
||||
const createNotification = await prisma.notifikasi.create({
|
||||
data: {
|
||||
title,
|
||||
type,
|
||||
createdAt: message.data.sentAt,
|
||||
appId: "test-id-app",
|
||||
userRoleId: findUserLogin.masterUserRoleId,
|
||||
kategoriApp: "PERCOBAAN",
|
||||
pesan: notificationBody || "",
|
||||
userId: i.userId,
|
||||
// Jika user yang masuk maka notifikasik akan dikirim ke semua admin , begitu sebaliknya !
|
||||
const filterByCurrentLoginId =
|
||||
findUserLogin.masterUserRoleId === "1" ? "2" : "1";
|
||||
|
||||
// Cari user yang akan menerima notifikasi
|
||||
const findAllUserBySendTo = await prisma.user.findMany({
|
||||
where: {
|
||||
masterUserRoleId: filterByCurrentLoginId,
|
||||
NOT: {
|
||||
id: findUserLogin.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Find All User By Send To >>", findAllUserBySendTo);
|
||||
|
||||
for (let a of findAllUserBySendTo) {
|
||||
const responseCreateToAdmin = await createNotification({
|
||||
title,
|
||||
type: type as string,
|
||||
createdAt: new Date(),
|
||||
pesan: notificationBody || "",
|
||||
appId: appId as string,
|
||||
kategoriApp: kategoriApp as string,
|
||||
userRoleId: findUserLogin.masterUserRoleId,
|
||||
status: status,
|
||||
deepLink: deepLink,
|
||||
senderId: findUserLogin.id,
|
||||
recipientId: a.id,
|
||||
});
|
||||
|
||||
if (responseCreateToAdmin) {
|
||||
const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||
where: {
|
||||
userId: a.id,
|
||||
isActive: true,
|
||||
// user: {
|
||||
// masterUserRoleId: "2",
|
||||
// },
|
||||
},
|
||||
});
|
||||
|
||||
if (createNotification) {
|
||||
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.
|
||||
},
|
||||
};
|
||||
|
||||
const response = await adminMessaging.send(message);
|
||||
console.log("✅ FCM sent:", response);
|
||||
} else {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Failed to create notification",
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const createNotification = await prisma.notifikasi.create({
|
||||
data: {
|
||||
title,
|
||||
type,
|
||||
createdAt: message.data.sentAt,
|
||||
appId: "test-id-app",
|
||||
userRoleId: findUserLogin.masterUserRoleId,
|
||||
kategoriApp: "PERCOBAAN",
|
||||
pesan: notificationBody || "",
|
||||
adminId: i.userId,
|
||||
},
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Failed to create notification",
|
||||
});
|
||||
|
||||
if (createNotification) {
|
||||
const response = await adminMessaging.send(message);
|
||||
console.log("✅ FCM sent:", response);
|
||||
} else {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: "Failed to create notification",
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// if (sendTo === "admin") {
|
||||
// for (let a of findAllUserBySendTo) {
|
||||
// const responseCreateToAdmin = await createNotification({
|
||||
// title,
|
||||
// type: type as string,
|
||||
// createdAt: new Date(),
|
||||
// pesan: notificationBody || "",
|
||||
// appId: appId as string,
|
||||
// kategoriApp: kategoriApp as string,
|
||||
// userRoleId: findUserLogin.masterUserRoleId,
|
||||
// status: status,
|
||||
// deepLink: deepLink,
|
||||
// adminId: a.id,
|
||||
// userId: findUserLogin.id,
|
||||
// });
|
||||
|
||||
// if (responseCreateToAdmin) {
|
||||
// const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||
// where: {
|
||||
// userId: a.id,
|
||||
// isActive: true,
|
||||
// // user: {
|
||||
// // masterUserRoleId: "2",
|
||||
// // },
|
||||
// },
|
||||
// });
|
||||
|
||||
// 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.
|
||||
// },
|
||||
// };
|
||||
|
||||
// const response = await adminMessaging.send(message);
|
||||
// console.log("✅ FCM sent:", response);
|
||||
// }
|
||||
// } else {
|
||||
// return NextResponse.json({
|
||||
// success: false,
|
||||
// message: "Failed to create notification",
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// } else if (sendTo === "user") {
|
||||
// for (let a of findAllUserBySendTo) {
|
||||
// const responseCreateToUser = await createNotification({
|
||||
// title,
|
||||
// type: type as string,
|
||||
// createdAt: new Date(),
|
||||
// pesan: notificationBody || "",
|
||||
// appId: appId as string,
|
||||
// kategoriApp: kategoriApp as string,
|
||||
// userRoleId: findUserLogin.masterUserRoleId,
|
||||
// status: status,
|
||||
// deepLink: deepLink,
|
||||
// adminId: findUserLogin.id,
|
||||
// userId: a.id,
|
||||
// });
|
||||
|
||||
// if (responseCreateToUser) {
|
||||
// const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||
// where: {
|
||||
// userId: a.id,
|
||||
// isActive: true,
|
||||
// // user: {
|
||||
// // masterUserRoleId: "1",
|
||||
// // },
|
||||
// },
|
||||
// });
|
||||
|
||||
// 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.
|
||||
// },
|
||||
// };
|
||||
|
||||
// const response = await adminMessaging.send(message);
|
||||
// console.log("✅ FCM sent:", response);
|
||||
// }
|
||||
// } else {
|
||||
// return NextResponse.json({
|
||||
// success: false,
|
||||
// message: "Failed to create notification",
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// return NextResponse.json({
|
||||
// success: false,
|
||||
// message: "Invalid ' to ' value",
|
||||
// });
|
||||
// }
|
||||
|
||||
// ================== BATAS ================== //
|
||||
// const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||
// where: {
|
||||
// isActive: true,
|
||||
// NOT: {
|
||||
// userId: findUserLogin.id,
|
||||
// },
|
||||
// user: {
|
||||
// masterUserRoleId: "1",
|
||||
// },
|
||||
// },
|
||||
// include: {
|
||||
// user: true,
|
||||
// },
|
||||
// });
|
||||
|
||||
// console.log("Device Token >>", deviceToken);
|
||||
|
||||
// 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.
|
||||
// },
|
||||
// };
|
||||
|
||||
// if (i.user?.masterUserRoleId === "1") {
|
||||
// const responseCreate = await createNotification({
|
||||
// title,
|
||||
// type: type as string,
|
||||
// createdAt: message.data.sentAt,
|
||||
// pesan: notificationBody || "",
|
||||
// appId: appId as string,
|
||||
// kategoriApp: kategoriApp as string,
|
||||
// userRoleId: findUserLogin.masterUserRoleId,
|
||||
// userId: findUserLogin.id,
|
||||
// status: status,
|
||||
// deepLink: deepLink,
|
||||
// });
|
||||
|
||||
// if (responseCreate) {
|
||||
// const response = await adminMessaging.send(message);
|
||||
// console.log("✅ FCM sent:", response);
|
||||
// } else {
|
||||
// return NextResponse.json({
|
||||
// success: false,
|
||||
// message: "Failed to create notification",
|
||||
// });
|
||||
// }
|
||||
// } else {
|
||||
// const responseCreate = await createNotification({
|
||||
// title: title,
|
||||
// type: type as string,
|
||||
// createdAt: message.data.sentAt,
|
||||
// pesan: notificationBody || "",
|
||||
// appId: appId as string,
|
||||
// kategoriApp: kategoriApp as string,
|
||||
// userRoleId: findUserLogin.masterUserRoleId,
|
||||
// adminId: findUserLogin.id,
|
||||
// status: status,
|
||||
// deepLink: deepLink,
|
||||
// });
|
||||
|
||||
// if (responseCreate) {
|
||||
// const response = await adminMessaging.send(message);
|
||||
// console.log("✅ FCM sent:", response);
|
||||
// } else {
|
||||
// return NextResponse.json({
|
||||
// success: false,
|
||||
// message: "Failed to create notification",
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Notification sent successfully",
|
||||
@@ -122,3 +323,50 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function createNotification({
|
||||
title,
|
||||
type,
|
||||
createdAt,
|
||||
appId,
|
||||
kategoriApp,
|
||||
pesan,
|
||||
userRoleId,
|
||||
status,
|
||||
deepLink,
|
||||
|
||||
senderId,
|
||||
recipientId,
|
||||
}: {
|
||||
title: string;
|
||||
type: string;
|
||||
createdAt: Date;
|
||||
appId: string;
|
||||
kategoriApp: string;
|
||||
userRoleId: string;
|
||||
status?: string;
|
||||
deepLink?: string;
|
||||
pesan: string;
|
||||
|
||||
senderId: string;
|
||||
recipientId: string;
|
||||
}) {
|
||||
const createNotification = await prisma.notifikasi.create({
|
||||
data: {
|
||||
title,
|
||||
type,
|
||||
createdAt,
|
||||
appId,
|
||||
kategoriApp,
|
||||
pesan,
|
||||
userRoleId,
|
||||
status,
|
||||
deepLink,
|
||||
|
||||
senderId,
|
||||
recipientId,
|
||||
},
|
||||
});
|
||||
|
||||
return createNotification;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user