Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 44d6788f6e | |||
| ac634100b5 | |||
| 1b206102b0 | |||
| 94a545bd30 | |||
| d50fda90e0 |
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
||||||
|
|
||||||
|
## [1.5.33](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.32...v1.5.33) (2026-01-06)
|
||||||
|
|
||||||
|
## [1.5.32](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.31...v1.5.32) (2026-01-05)
|
||||||
|
|
||||||
## [1.5.31](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.30...v1.5.31) (2025-12-24)
|
## [1.5.31](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.30...v1.5.31) (2025-12-24)
|
||||||
|
|
||||||
## [1.5.30](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.29...v1.5.30) (2025-12-19)
|
## [1.5.30](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.29...v1.5.30) (2025-12-19)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hipmi",
|
"name": "hipmi",
|
||||||
"version": "1.5.31",
|
"version": "1.5.33",
|
||||||
"private": true,
|
"private": true,
|
||||||
"prisma": {
|
"prisma": {
|
||||||
"seed": "bun prisma/seed.ts"
|
"seed": "bun prisma/seed.ts"
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Notifikasi" ALTER COLUMN "appId" DROP NOT NULL,
|
||||||
|
ALTER COLUMN "kategoriApp" DROP NOT NULL,
|
||||||
|
ALTER COLUMN "pesan" DROP NOT NULL;
|
||||||
@@ -982,9 +982,9 @@ model Notifikasi {
|
|||||||
isActive Boolean @default(true)
|
isActive Boolean @default(true)
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
appId String
|
appId String?
|
||||||
kategoriApp String
|
kategoriApp String?
|
||||||
pesan String
|
pesan String?
|
||||||
title String?
|
title String?
|
||||||
status String?
|
status String?
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { sessionCreate } from "@/app/(auth)/_lib/session_create";
|
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 prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
@@ -51,12 +52,6 @@ export async function POST(req: Request) {
|
|||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
|
|
||||||
// const token = await sessionCreate({
|
|
||||||
// sessionKey: process.env.NEXT_PUBLIC_BASE_SESSION_KEY!,
|
|
||||||
// encodedKey: process.env.NEXT_PUBLIC_BASE_TOKEN_KEY!,
|
|
||||||
// user: createUser as any,
|
|
||||||
// });
|
|
||||||
|
|
||||||
const createOtpId = await prisma.kodeOtp.create({
|
const createOtpId = await prisma.kodeOtp.create({
|
||||||
data: {
|
data: {
|
||||||
nomor: data.nomor,
|
nomor: data.nomor,
|
||||||
@@ -87,11 +82,90 @@ export async function POST(req: Request) {
|
|||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// =========== START SEND NOTIFICATION =========== //
|
||||||
|
|
||||||
|
const findAllUserBySendTo = await prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
masterUserRoleId: "2",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Users to notify:", findAllUserBySendTo);
|
||||||
|
|
||||||
|
const dataNotification = {
|
||||||
|
title: "Pendaftaran Baru",
|
||||||
|
type: "announcement",
|
||||||
|
kategoriApp: "OTHER",
|
||||||
|
createdAt: new Date(),
|
||||||
|
pesan: "User baru telah melakukan registrasi. Ayo cek dan verifikasi!",
|
||||||
|
deepLink: `/admin/user-access/${createUser.id}`,
|
||||||
|
senderId: createUser.id,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let a of findAllUserBySendTo) {
|
||||||
|
const createdNotification = await prisma.notifikasi.create({
|
||||||
|
data: {
|
||||||
|
...dataNotification,
|
||||||
|
recipientId: a.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (createdNotification) {
|
||||||
|
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 =========== //
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
message: "Registrasi Berhasil",
|
message: "Registrasi Berhasil",
|
||||||
// token: token,
|
|
||||||
kodeId: createOtpId.id,
|
kodeId: createOtpId.id,
|
||||||
},
|
},
|
||||||
{ status: 201 }
|
{ status: 201 }
|
||||||
|
|||||||
@@ -34,9 +34,15 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
const { data } = await request.json();
|
const { data } = await request.json();
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const category = searchParams.get("category");
|
||||||
|
|
||||||
|
console.log("Received data:", data);
|
||||||
|
console.log("User ID:", id);
|
||||||
|
console.log("Category:", category);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (data.active) {
|
if (category === "access") {
|
||||||
const updateData = await prisma.user.update({
|
const updateData = await prisma.user.update({
|
||||||
where: {
|
where: {
|
||||||
id: id,
|
id: id,
|
||||||
@@ -47,7 +53,7 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log("[Update Active Berhasil]", updateData);
|
console.log("[Update Active Berhasil]", updateData);
|
||||||
} else if (data.role) {
|
} else if (category === "role") {
|
||||||
const fixName = _.startCase(data.role.replace(/_/g, " "));
|
const fixName = _.startCase(data.role.replace(/_/g, " "));
|
||||||
|
|
||||||
const checkRole = await prisma.masterUserRole.findFirst({
|
const checkRole = await prisma.masterUserRole.findFirst({
|
||||||
@@ -68,6 +74,12 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log("[Update Role Berhasil]", updateData);
|
console.log("[Update Role Berhasil]", updateData);
|
||||||
|
} else {
|
||||||
|
return NextResponse.json({
|
||||||
|
status: 400,
|
||||||
|
success: false,
|
||||||
|
message: "Invalid category",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import { prisma } from "@/lib";
|
import { prisma } from "@/lib";
|
||||||
|
import _ from "lodash";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { NotificationProp } from "../route";
|
||||||
|
import { adminMessaging } from "@/lib/firebase-admin";
|
||||||
|
|
||||||
export async function GET(
|
export async function GET(
|
||||||
request: NextRequest,
|
request: NextRequest,
|
||||||
@@ -9,32 +12,21 @@ export async function GET(
|
|||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const category = searchParams.get("category");
|
const category = searchParams.get("category");
|
||||||
|
|
||||||
|
let fixData;
|
||||||
|
const fixCategory = _.upperCase(category || "");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let fixData;
|
const data = await prisma.notifikasi.findMany({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: "desc",
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
recipientId: id,
|
||||||
|
kategoriApp: fixCategory,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (category === "count-as-unread") {
|
fixData = data;
|
||||||
const data = await prisma.notifikasi.findMany({
|
|
||||||
where: {
|
|
||||||
userId: id,
|
|
||||||
isRead: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fixData = data.length;
|
|
||||||
} else if (category === "all") {
|
|
||||||
const data = await prisma.notifikasi.findMany({
|
|
||||||
where: {
|
|
||||||
userId: id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
fixData = data;
|
|
||||||
} else {
|
|
||||||
return NextResponse.json({
|
|
||||||
success: false,
|
|
||||||
message: "Invalid category",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -47,3 +39,159 @@ export async function GET(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function PUT(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await prisma.notifikasi.update({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
isRead: true,
|
||||||
|
readAt: new Date(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "Notifications marked as read",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: (error as Error).message },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(
|
||||||
|
request: NextRequest,
|
||||||
|
{ params }: { params: { id: string } }
|
||||||
|
) {
|
||||||
|
const { id } = params;
|
||||||
|
|
||||||
|
const { data } = await request.json();
|
||||||
|
|
||||||
|
const {
|
||||||
|
title,
|
||||||
|
body: notificationBody,
|
||||||
|
userLoginId,
|
||||||
|
type,
|
||||||
|
kategoriApp,
|
||||||
|
appId,
|
||||||
|
status,
|
||||||
|
deepLink,
|
||||||
|
} = data as NotificationProp;
|
||||||
|
|
||||||
|
console.log("Notification Send >>", data);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Cari user yang login
|
||||||
|
const findUserLogin = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id: userLoginId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!findUserLogin) {
|
||||||
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cari token fcm user yang login
|
||||||
|
const checkFcmToken = await prisma.tokenUserDevice.findFirst({
|
||||||
|
where: {
|
||||||
|
userId: findUserLogin.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!checkFcmToken) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "FCM Token not found" },
|
||||||
|
{ status: 404 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const created = await prisma.notifikasi.create({
|
||||||
|
data: {
|
||||||
|
title,
|
||||||
|
type,
|
||||||
|
createdAt: new Date(),
|
||||||
|
appId,
|
||||||
|
kategoriApp,
|
||||||
|
pesan: notificationBody || "",
|
||||||
|
userRoleId: findUserLogin.masterUserRoleId,
|
||||||
|
status,
|
||||||
|
deepLink,
|
||||||
|
senderId: findUserLogin.id,
|
||||||
|
recipientId: id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (created) {
|
||||||
|
const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||||
|
where: {
|
||||||
|
userId: id,
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for (let i of deviceToken) {
|
||||||
|
const message = {
|
||||||
|
token: i.token,
|
||||||
|
notification: {
|
||||||
|
title,
|
||||||
|
body: notificationBody || "",
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
|
||||||
|
id: created.id,
|
||||||
|
deepLink: 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "Notification sent successfully",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ FCM error:", error);
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: (error as Error).message },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export async function GET(
|
|||||||
{ params }: { params: { id: string } }
|
{ params }: { params: { id: string } }
|
||||||
) {
|
) {
|
||||||
const { id } = params;
|
const { id } = params;
|
||||||
|
console.log("User ID:", id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await prisma.notifikasi.count({
|
const data = await prisma.notifikasi.count({
|
||||||
@@ -28,7 +29,3 @@ export async function GET(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pilihan = "PENGIRIM" | "PENERIMA";
|
|
||||||
|
|
||||||
const data: Pilihan = "PENERIMA";
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { prisma } from "@/lib";
|
|||||||
import { adminMessaging } from "@/lib/firebase-admin";
|
import { adminMessaging } from "@/lib/firebase-admin";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
type NotificationProp = {
|
export type NotificationProp = {
|
||||||
title: string;
|
title: string;
|
||||||
body: string;
|
body: string;
|
||||||
userLoginId: string;
|
userLoginId: string;
|
||||||
@@ -12,7 +12,6 @@ type NotificationProp = {
|
|||||||
kategoriApp?: string;
|
kategoriApp?: string;
|
||||||
type?: string;
|
type?: string;
|
||||||
deepLink?: string;
|
deepLink?: string;
|
||||||
// sendTo: "user" | "admin" | "all";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
@@ -28,7 +27,6 @@ export async function POST(request: NextRequest) {
|
|||||||
appId,
|
appId,
|
||||||
status,
|
status,
|
||||||
deepLink,
|
deepLink,
|
||||||
// sendTo,
|
|
||||||
} = data as NotificationProp;
|
} = data as NotificationProp;
|
||||||
|
|
||||||
console.log("Notification Send >>", data);
|
console.log("Notification Send >>", data);
|
||||||
@@ -75,7 +73,7 @@ export async function POST(request: NextRequest) {
|
|||||||
console.log("Find All User By Send To >>", findAllUserBySendTo);
|
console.log("Find All User By Send To >>", findAllUserBySendTo);
|
||||||
|
|
||||||
for (let a of findAllUserBySendTo) {
|
for (let a of findAllUserBySendTo) {
|
||||||
const responseCreateToAdmin = await createNotification({
|
const responseCreatedNotifications = await createNotification({
|
||||||
title,
|
title,
|
||||||
type: type as string,
|
type: type as string,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
@@ -89,14 +87,11 @@ export async function POST(request: NextRequest) {
|
|||||||
recipientId: a.id,
|
recipientId: a.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (responseCreateToAdmin) {
|
if (responseCreatedNotifications) {
|
||||||
const deviceToken = await prisma.tokenUserDevice.findMany({
|
const deviceToken = await prisma.tokenUserDevice.findMany({
|
||||||
where: {
|
where: {
|
||||||
userId: a.id,
|
userId: a.id,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
// user: {
|
|
||||||
// masterUserRoleId: "2",
|
|
||||||
// },
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -109,12 +104,44 @@ export async function POST(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
|
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
|
||||||
|
id: responseCreatedNotifications.id,
|
||||||
|
deepLink: deepLink || "",
|
||||||
// contoh: senderId, type, etc.
|
// contoh: senderId, type, etc.
|
||||||
},
|
},
|
||||||
|
// 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
|
||||||
|
// Opsional: sesuaikan icon & warna
|
||||||
|
// icon: 'ic_notification',
|
||||||
|
// color: '#FFD700',
|
||||||
|
},
|
||||||
|
// FCM akan bangunkan app jika perlu
|
||||||
|
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
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await adminMessaging.send(message);
|
try {
|
||||||
console.log("✅ FCM sent:", response);
|
const response = await adminMessaging.send(message);
|
||||||
|
console.log(
|
||||||
|
"✅ FCM sent to token:",
|
||||||
|
"Response:",
|
||||||
|
response
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error("❌ FCM send failed for token:", i.token, error);
|
||||||
|
// Lanjutkan ke token berikutnya meski satu gagal
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
@@ -124,193 +151,6 @@ export async function POST(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
message: "Notification sent successfully",
|
message: "Notification sent successfully",
|
||||||
@@ -334,7 +174,6 @@ async function createNotification({
|
|||||||
userRoleId,
|
userRoleId,
|
||||||
status,
|
status,
|
||||||
deepLink,
|
deepLink,
|
||||||
|
|
||||||
senderId,
|
senderId,
|
||||||
recipientId,
|
recipientId,
|
||||||
}: {
|
}: {
|
||||||
@@ -362,7 +201,6 @@ async function createNotification({
|
|||||||
userRoleId,
|
userRoleId,
|
||||||
status,
|
status,
|
||||||
deepLink,
|
deepLink,
|
||||||
|
|
||||||
senderId,
|
senderId,
|
||||||
recipientId,
|
recipientId,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user