Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| de1d668ff2 | |||
| f1c8432fdc | |||
| 602d759919 | |||
| 1cd4c3713e | |||
| 4307b383e3 | |||
| 0786d23336 | |||
| cb3511f973 | |||
| b4921c4e82 | |||
| a9325054eb | |||
| 819812149f | |||
| 75ba2b29ae | |||
| 54a4d15bdd | |||
| 1321f33da9 | |||
| fad0c33b9a | |||
| 565bab4998 | |||
| 7530a38c4d |
@@ -2,12 +2,6 @@
|
|||||||
|
|
||||||
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.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.29](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.28...v1.5.29) (2025-12-17)
|
|
||||||
|
|
||||||
## [1.5.28](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.27...v1.5.28) (2025-12-17)
|
## [1.5.28](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.27...v1.5.28) (2025-12-17)
|
||||||
|
|
||||||
## [1.5.27](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.26...v1.5.27) (2025-12-17)
|
## [1.5.27](https://wibugit.wibudev.com/wibu/hipmi/compare/v1.5.26...v1.5.27) (2025-12-17)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hipmi",
|
"name": "hipmi",
|
||||||
"version": "1.5.31",
|
"version": "1.5.28",
|
||||||
"private": true,
|
"private": true,
|
||||||
"prisma": {
|
"prisma": {
|
||||||
"seed": "bun prisma/seed.ts"
|
"seed": "bun prisma/seed.ts"
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
-- AlterTable
|
|
||||||
ALTER TABLE "Notifikasi" ADD COLUMN "type" TEXT;
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
-- DropForeignKey
|
|
||||||
ALTER TABLE "Notifikasi" DROP CONSTRAINT "Notifikasi_userRoleId_fkey";
|
|
||||||
|
|
||||||
-- AlterTable
|
|
||||||
ALTER TABLE "Notifikasi" ADD COLUMN "recipientId" TEXT,
|
|
||||||
ADD COLUMN "senderId" TEXT,
|
|
||||||
ALTER COLUMN "userRoleId" DROP NOT NULL;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "Notifikasi" ADD CONSTRAINT "Notifikasi_userRoleId_fkey" FOREIGN KEY ("userRoleId") REFERENCES "MasterUserRole"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "Notifikasi" ADD CONSTRAINT "Notifikasi_recipientId_fkey" FOREIGN KEY ("recipientId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
-- AddForeignKey
|
|
||||||
ALTER TABLE "Notifikasi" ADD CONSTRAINT "Notifikasi_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
|
||||||
@@ -59,10 +59,6 @@ model User {
|
|||||||
acceptedTermsAt DateTime?
|
acceptedTermsAt DateTime?
|
||||||
acceptedForumTermsAt DateTime?
|
acceptedForumTermsAt DateTime?
|
||||||
tokenUserDevices TokenUserDevice[]
|
tokenUserDevices TokenUserDevice[]
|
||||||
|
|
||||||
// For Mobile App
|
|
||||||
NotificationRecipient Notifikasi[] @relation("NotificationRecipient")
|
|
||||||
NotificationSender Notifikasi[] @relation("NotificationSender")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model MasterUserRole {
|
model MasterUserRole {
|
||||||
@@ -591,7 +587,7 @@ model Donasi_Invoice {
|
|||||||
|
|
||||||
imageId String?
|
imageId String?
|
||||||
MasterBank MasterBank? @relation(fields: [masterBankId], references: [id])
|
MasterBank MasterBank? @relation(fields: [masterBankId], references: [id])
|
||||||
masterBankId String?
|
masterBankId String?
|
||||||
}
|
}
|
||||||
|
|
||||||
model Donasi_Kabar {
|
model Donasi_Kabar {
|
||||||
@@ -978,36 +974,27 @@ model NomorAdmin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model Notifikasi {
|
model Notifikasi {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
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?
|
||||||
|
|
||||||
isRead Boolean @default(false)
|
isRead Boolean @default(false)
|
||||||
readAt DateTime? // kapan user membaca notifikasi ini
|
readAt DateTime? // kapan user membaca notifikasi ini
|
||||||
deepLink String? // misal: "announcement/123", "user/profile/cmha6wb9w0001cfndwl9fcse6"
|
deepLink String? // misal: "announcement/123", "user/profile/cmha6wb9w0001cfndwl9fcse6"
|
||||||
type String?
|
|
||||||
|
|
||||||
Role MasterUserRole? @relation(fields: [userRoleId], references: [id])
|
Role MasterUserRole? @relation(fields: [userRoleId], references: [id])
|
||||||
userRoleId String?
|
userRoleId String
|
||||||
|
|
||||||
User User? @relation("UserNotifikasi", fields: [userId], references: [id], map: "NotifikasiUser")
|
User User? @relation("UserNotifikasi", fields: [userId], references: [id], map: "NotifikasiUser")
|
||||||
userId String?
|
userId String?
|
||||||
Admin User? @relation("AdminNotifikasi", fields: [adminId], references: [id], map: "NotifikasiAdmin")
|
Admin User? @relation("AdminNotifikasi", fields: [adminId], references: [id], map: "NotifikasiAdmin")
|
||||||
adminId String?
|
adminId String?
|
||||||
|
|
||||||
// Recipient (user who receives the notification)
|
|
||||||
recipient User? @relation("NotificationRecipient", fields: [recipientId], references: [id])
|
|
||||||
recipientId String?
|
|
||||||
|
|
||||||
// Sender (user who sent the notification)
|
|
||||||
sender User? @relation("NotificationSender", fields: [senderId], references: [id])
|
|
||||||
senderId String?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MAPS
|
// MAPS
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { prisma } from "@/lib";
|
|
||||||
|
|
||||||
export { DELETE };
|
|
||||||
|
|
||||||
async function DELETE(
|
|
||||||
request: NextRequest,
|
|
||||||
{ 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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { prisma } from "@/lib";
|
import { prisma } from "@/lib";
|
||||||
|
|
||||||
export { POST, GET };
|
export async function POST(request: NextRequest) {
|
||||||
|
|
||||||
async function POST(request: NextRequest) {
|
|
||||||
const { data } = await request.json();
|
const { data } = await request.json();
|
||||||
try {
|
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) {
|
if (!fcmToken) {
|
||||||
return NextResponse.json({ error: "Missing Token" }, { status: 400 });
|
return NextResponse.json({ error: "Missing Token" }, { status: 400 });
|
||||||
@@ -23,9 +23,6 @@ async function POST(request: NextRequest) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
console.log("✅ EX", existing);
|
|
||||||
|
|
||||||
let deviceToken;
|
let deviceToken;
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
@@ -65,22 +62,3 @@ 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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
import { prisma } from "@/lib";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: NextRequest,
|
|
||||||
{ params }: { params: { id: string } }
|
|
||||||
) {
|
|
||||||
const { id } = params;
|
|
||||||
const { searchParams } = new URL(request.url);
|
|
||||||
const category = searchParams.get("category");
|
|
||||||
|
|
||||||
try {
|
|
||||||
let fixData;
|
|
||||||
|
|
||||||
if (category === "count-as-unread") {
|
|
||||||
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({
|
|
||||||
success: true,
|
|
||||||
data: fixData,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: (error as Error).message },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { prisma } from "@/lib";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
export async function GET(
|
|
||||||
request: NextRequest,
|
|
||||||
{ params }: { params: { id: string } }
|
|
||||||
) {
|
|
||||||
const { id } = params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const data = await prisma.notifikasi.count({
|
|
||||||
where: {
|
|
||||||
recipientId: id,
|
|
||||||
isRead: false,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("List Notification >>", data);
|
|
||||||
|
|
||||||
return NextResponse.json({
|
|
||||||
success: true,
|
|
||||||
data: data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
return NextResponse.json({
|
|
||||||
success: false,
|
|
||||||
message: "Failed to get unread count",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Pilihan = "PENGIRIM" | "PENERIMA";
|
|
||||||
|
|
||||||
const data: Pilihan = "PENERIMA";
|
|
||||||
@@ -1,372 +0,0 @@
|
|||||||
// app/api/test/notifications/route.ts
|
|
||||||
import { prisma } from "@/lib";
|
|
||||||
import { adminMessaging } from "@/lib/firebase-admin";
|
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
|
|
||||||
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 {
|
|
||||||
title,
|
|
||||||
body: notificationBody,
|
|
||||||
userLoginId,
|
|
||||||
type,
|
|
||||||
kategoriApp,
|
|
||||||
appId,
|
|
||||||
status,
|
|
||||||
deepLink,
|
|
||||||
// sendTo,
|
|
||||||
} = data as NotificationProp;
|
|
||||||
|
|
||||||
console.log("Notification Send >>", data);
|
|
||||||
|
|
||||||
// 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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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",
|
|
||||||
// },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
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",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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",
|
|
||||||
});
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error("❌ FCM error:", error);
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error.message || "Failed to send FCM" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
44
src/app/api/mobile/notifications/route.ts
Normal file
44
src/app/api/mobile/notifications/route.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// app/api/test/notifications/route.ts
|
||||||
|
import { adminMessaging } from "@/lib/firebase-admin";
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function POST(request: NextRequest) {
|
||||||
|
try {
|
||||||
|
const { data } = await request.json();
|
||||||
|
const { fcmToken, title, body: notificationBody } = data;
|
||||||
|
|
||||||
|
console.log("Data Notifikasi >>", data);
|
||||||
|
|
||||||
|
if (!fcmToken || !title) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Missing fcmToken or title" },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
token: fcmToken,
|
||||||
|
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);
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, messageId: response });
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error("❌ FCM error:", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: error.message || "Failed to send FCM" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
x.sh
7
x.sh
@@ -3,13 +3,10 @@ URL="http://localhost:3000"
|
|||||||
# curl -X GET -H "Authorization: Bearer $TOKEN" ${URL}/api/middleware
|
# curl -X GET -H "Authorization: Bearer $TOKEN" ${URL}/api/middleware
|
||||||
# curl -X GET -H "Cookie: hipmi-key=$TOKEN; user_id=789" ${URL}/dev/home | tee test.html
|
# curl -X GET -H "Cookie: hipmi-key=$TOKEN; user_id=789" ${URL}/dev/home | tee test.html
|
||||||
|
|
||||||
curl -X POST ${URL}/api/mobile/notification \
|
curl -X POST ${URL}/api/mobile/notifications \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{
|
-d '{
|
||||||
"fcmToken": "cVmHm-3P4E-1vjt6AA9kSF:APA91bHTkHjGTLxrFsb6Le6bZmzboZhwMGYXU4p0FP9yEeXixLDXNKS4F5vLuZV3sRgSnjjQsPpLOgstVLHJB8VJTObctKLdN-CxAp4dnP7Jbc_mH53jWvs",
|
"fcmToken": "cVmHm-3P4E-1vjt6AA9kSF:APA91bHTkHjGTLxrFsb6Le6bZmzboZhwMGYXU4p0FP9yEeXixLDXNKS4F5vLuZV3sRgSnjjQsPpLOgstVLHJB8VJTObctKLdN-CxAp4dnP7Jbc_mH53jWvs",
|
||||||
"title": "Test dari Backend (App Router)!",
|
"title": "Test dari Backend (App Router)!",
|
||||||
"body": "Berhasil di App Router!",
|
"body": "Berhasil di App Router!"
|
||||||
"userLoginId": "cmha7p6yc0000cfoe5w2e7gdr",
|
|
||||||
"type": "NOTIFICATION",
|
|
||||||
"kategoriApp": "PERCOBAAN"
|
|
||||||
}'
|
}'
|
||||||
Reference in New Issue
Block a user