Fix notification reuse component
Fix: - modified: src/app/api/auth/mobile-register/route.ts - modified: src/lib/mobile/notification/send-notification.ts ### No Issue
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
import { sessionCreate } from "@/app/(auth)/_lib/session_create";
|
|
||||||
import { randomOTP } from "@/app_modules/auth/fun/rondom_otp";
|
import { randomOTP } from "@/app_modules/auth/fun/rondom_otp";
|
||||||
import { adminMessaging } from "@/lib/firebase-admin";
|
import { sendNotificationMobileToManyUser } from "@/lib/mobile/notification/send-notification";
|
||||||
|
import { routeAdminMobile } from "@/lib/mobile/route-page-mobile";
|
||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
import {
|
||||||
|
NotificationMobileBodyType,
|
||||||
|
NotificationMobileTitleType,
|
||||||
|
} from "../../../../../types/type-mobile-notification";
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
if (req.method !== "POST") {
|
if (req.method !== "POST") {
|
||||||
@@ -84,12 +88,12 @@ export async function POST(req: Request) {
|
|||||||
|
|
||||||
// =========== START SEND NOTIFICATION =========== //
|
// =========== START SEND NOTIFICATION =========== //
|
||||||
|
|
||||||
const findAllUserBySendTo = await prisma.user.findMany({
|
const adminUsers = await prisma.user.findMany({
|
||||||
where: { masterUserRoleId: "2" },
|
where: { masterUserRoleId: "2", NOT: { id: data.authorId } },
|
||||||
select: { id: true },
|
select: { id: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Users to notify:", findAllUserBySendTo);
|
console.log("Users to notify:", adminUsers);
|
||||||
|
|
||||||
const dataNotification = {
|
const dataNotification = {
|
||||||
title: "Pendaftaran Baru",
|
title: "Pendaftaran Baru",
|
||||||
@@ -101,63 +105,17 @@ export async function POST(req: Request) {
|
|||||||
senderId: createUser.id,
|
senderId: createUser.id,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (let a of findAllUserBySendTo) {
|
await sendNotificationMobileToManyUser({
|
||||||
const createdNotification = await prisma.notifikasi.create({
|
recipientIds: adminUsers.map((user) => user.id),
|
||||||
data: {
|
senderId: data.authorId,
|
||||||
...dataNotification,
|
payload: {
|
||||||
recipientId: a.id,
|
title: "Pendaftaran User Baru" as NotificationMobileTitleType,
|
||||||
},
|
body: "User baru telah melakukan registrasi. Ayo cek dan verifikasi!" as NotificationMobileBodyType,
|
||||||
});
|
type: "announcement",
|
||||||
|
deepLink: routeAdminMobile.userAccess({ id: createUser.id }),
|
||||||
if (createdNotification) {
|
kategoriApp: "OTHER",
|
||||||
const deviceToken = await prisma.tokenUserDevice.findMany({
|
},
|
||||||
where: {
|
});
|
||||||
userId: a.id,
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
for (let i of deviceToken) {
|
|
||||||
const message = {
|
|
||||||
token: i.token,
|
|
||||||
notification: {
|
|
||||||
title: dataNotification.title,
|
|
||||||
body: dataNotification.pesan,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
|
|
||||||
id: createdNotification.id,
|
|
||||||
deepLink: dataNotification.deepLink,
|
|
||||||
},
|
|
||||||
// Konfigurasi Android untuk prioritas tinggi
|
|
||||||
android: {
|
|
||||||
priority: "high" as const, // Kirim secepatnya, bahkan di doze mode untuk notifikasi penting
|
|
||||||
notification: {
|
|
||||||
channelId: "default", // Sesuaikan dengan channel yang kamu buat di Android
|
|
||||||
},
|
|
||||||
ttl: 0 as const, // Kirim secepatnya, jangan tunda
|
|
||||||
},
|
|
||||||
// Opsional: tambahkan untuk iOS juga
|
|
||||||
apns: {
|
|
||||||
payload: {
|
|
||||||
aps: {
|
|
||||||
sound: "default" as const,
|
|
||||||
// 'content-available': 1 as const, // jika butuh silent push
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await adminMessaging.send(message);
|
|
||||||
console.log("✅ FCM sent successfully", "Response:", response);
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error("❌ FCM send failed:", error);
|
|
||||||
// Lanjutkan ke token berikutnya meski satu gagal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// =========== END SEND NOTIFICATION =========== //
|
// =========== END SEND NOTIFICATION =========== //
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,10 @@ export async function sendNotificationMobileToOneUser({
|
|||||||
}) {
|
}) {
|
||||||
try {
|
try {
|
||||||
const kategoriToNormalCase = _.lowerCase(payload.kategoriApp);
|
const kategoriToNormalCase = _.lowerCase(payload.kategoriApp);
|
||||||
const titleFix = `${_.startCase(kategoriToNormalCase)}: ${payload.title}`;
|
const titleFix =
|
||||||
|
kategoriToNormalCase === "other"
|
||||||
|
? payload.title
|
||||||
|
: `${_.startCase(kategoriToNormalCase)}: ${payload.title}`;
|
||||||
console.log("titleFix", titleFix);
|
console.log("titleFix", titleFix);
|
||||||
|
|
||||||
// 1. Simpan notifikasi ke DB
|
// 1. Simpan notifikasi ke DB
|
||||||
|
|||||||
Reference in New Issue
Block a user