Compare commits

...

2 Commits

Author SHA1 Message Date
2086692897 Fix API notifikasi untuk job
### No Issue
2026-01-08 10:14:35 +08:00
87515ae19f Notifikasi mobile job
Add:
 src/lib/mobile/
 types/type-mobile-notification.ts

Fix:
src/app/api/auth/mobile-register/route.ts
src/app/api/mobile/job/route.ts

### No Issue
2026-01-06 17:52:28 +08:00
6 changed files with 270 additions and 48 deletions

View File

@@ -85,9 +85,8 @@ export async function POST(req: Request) {
// =========== START SEND NOTIFICATION =========== //
const findAllUserBySendTo = await prisma.user.findMany({
where: {
masterUserRoleId: "2",
},
where: { masterUserRoleId: "2" },
select: { id: true },
});
console.log("Users to notify:", findAllUserBySendTo);

View File

@@ -1,6 +1,8 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import _ from "lodash";
import { sendNotificationMobileToOneUser } from "@/lib/mobile/notification/send-notification";
import { routeUserMobile } from "@/lib/mobile/route-page-mobile";
export { GET, PUT };
@@ -54,10 +56,14 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
async function PUT(request: Request, { params }: { params: { id: string } }) {
const { id } = params;
const { data } = await request.json();
const { catatan, senderId } = data;
const { searchParams } = new URL(request.url);
const status = searchParams.get("status");
const fixStatus = _.startCase(status as string);
let fixData;
try {
const checkStatus = await prisma.masterStatus.findFirst({
@@ -83,7 +89,7 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
},
data: {
masterStatusId: checkStatus.id,
catatan: data,
catatan: catatan,
},
select: {
id: true,
@@ -97,6 +103,18 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
},
});
await sendNotificationMobileToOneUser({
recipientId: updt.authorId as any,
senderId: senderId,
payload: {
title: "Pengajuan Review",
body: "Pengajuan data anda telah di tolak !",
type: "announcement",
kategoriApp: "JOB",
deepLink: routeUserMobile.jobByStatus({ status: "reject" }),
},
});
fixData = updt;
} else if (fixStatus === "Publish") {
const updt = await prisma.job.update({
@@ -118,6 +136,18 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
},
});
await sendNotificationMobileToOneUser({
recipientId: updt.authorId as any,
senderId: senderId,
payload: {
title: "Pengajuan Review",
body: "Selamat data anda telah terpublikasi",
type: "announcement",
kategoriApp: "JOB",
deepLink: routeUserMobile.jobByStatus({ status: "publish" }),
},
});
fixData = updt;
}

View File

@@ -1,3 +1,5 @@
import { sendNotificationMobileToManyUser } from "@/lib/mobile/notification/send-notification";
import { routeAdminMobile } from "@/lib/mobile/route-page-mobile";
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
@@ -17,6 +19,25 @@ async function POST(request: Request) {
},
});
// kirim notifikasi ke semua admin untuk mengetahui ada job baru yang harus di review
const adminUsers = await prisma.user.findMany({
where: { masterUserRoleId: "2" },
select: { id: true },
});
await sendNotificationMobileToManyUser({
recipientIds: adminUsers.map((user) => user.id),
senderId: data.authorId,
payload: {
title: "Pengajuan Review",
body: "Terdapat pengajuan baru yang perlu direview",
type: "announcement",
deepLink: routeAdminMobile.jobByStatus({ status: "review" }),
kategoriApp: "JOB",
},
});
return NextResponse.json(
{
success: true,
@@ -54,10 +75,10 @@ async function GET(request: Request) {
MasterStatus: {
name: "Publish",
},
// title: {
// contains: search || "",
// mode: "insensitive",
// },
// title: {
// contains: search || "",
// mode: "insensitive",
// },
},
orderBy: {
createdAt: "desc",
@@ -90,46 +111,46 @@ async function GET(request: Request) {
fixData = data;
} else if (category === "beranda") {
const data = await prisma.job.findMany({
where: {
isActive: true,
isArsip: false,
MasterStatus: {
name: "Publish",
},
title: {
contains: search || "",
mode: "insensitive",
},
},
orderBy: {
createdAt: "desc",
},
select: {
id: true,
title: true,
deskripsi: true,
authorId: true,
MasterStatus: {
select: {
name: true,
},
},
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
imageId: true,
},
},
},
},
},
});
const data = await prisma.job.findMany({
where: {
isActive: true,
isArsip: false,
MasterStatus: {
name: "Publish",
},
title: {
contains: search || "",
mode: "insensitive",
},
},
orderBy: {
createdAt: "desc",
},
select: {
id: true,
title: true,
deskripsi: true,
authorId: true,
MasterStatus: {
select: {
name: true,
},
},
Author: {
select: {
id: true,
username: true,
Profile: {
select: {
id: true,
name: true,
imageId: true,
},
},
},
},
},
});
fixData = data;
}

View File

@@ -0,0 +1,117 @@
// lib/notifications/send-notification.ts
import { adminMessaging } from "@/lib/firebase-admin";
import prisma from "@/lib/prisma";
import { NotificationMobilePayload } from "../../../../types/type-mobile-notification";
import _ from "lodash";
/**
* Kirim notifikasi ke satu user (semua device aktifnya)
* @param recipientId - ID penerima
* @param senderId - ID pengirim
* @param payload - Data notifikasi
*/
export async function sendNotificationMobileToOneUser({
recipientId,
senderId,
payload,
}: {
recipientId: string;
senderId: string;
payload: NotificationMobilePayload;
}) {
try {
const kategoriToNormalCase = _.lowerCase(payload.kategoriApp);
const titleFix = `${_.startCase(kategoriToNormalCase)}: ${payload.title}`;
console.log("titleFix", titleFix);
// 1. Simpan notifikasi ke DB
const notification = await prisma.notifikasi.create({
data: {
title: titleFix,
pesan: payload.body,
deepLink: payload.deepLink,
kategoriApp: payload.kategoriApp,
recipientId: recipientId,
senderId: senderId,
type: payload.type.trim(),
},
});
// 2. Ambil semua token aktif milik penerima
const tokens = await prisma.tokenUserDevice.findMany({
where: { userId: recipientId, isActive: true },
select: { token: true, id: true },
});
if (tokens.length === 0) {
console.warn(`No active tokens found for user ${recipientId}`);
return;
}
// 3. Kirim FCM ke semua token
await Promise.allSettled(
tokens.map(async ({ token, id }) => {
try {
await adminMessaging.send({
token,
notification: {
title: titleFix,
body: payload.body,
},
data: {
sentAt: new Date().toISOString(), // ✅ Simpan metadata di data
id: notification.id,
deepLink: payload.deepLink,
},
android: {
priority: "high" as const,
notification: { channelId: "default" },
ttl: 0 as const,
},
apns: {
payload: { aps: { sound: "default" as const } },
},
});
} catch (fcmError: any) {
// Hapus token jika invalid
console.log("fcmError", fcmError);
if (fcmError.code === "messaging/invalid-registration-token") {
await prisma.tokenUserDevice.delete({ where: { id: id } });
console.log(`❌ Invalid token removed: ${token}`);
}
console.error(`FCM failed for token ${token}:`, fcmError.message);
}
})
);
console.log(`✅ Notification sent to user ${recipientId}`);
} catch (error) {
console.error("Failed to send notification:", error);
throw error; // biarkan caller handle error
}
}
/**
* Kirim notifikasi ke banyak user
*/
export async function sendNotificationMobileToManyUser({
recipientIds,
senderId,
payload,
}: {
recipientIds: string[];
senderId: string;
payload: NotificationMobilePayload;
}) {
await Promise.allSettled(
recipientIds.map((id) =>
sendNotificationMobileToOneUser({
recipientId: id,
senderId: senderId,
payload: payload,
})
)
);
}

View File

@@ -0,0 +1,19 @@
export { routeAdminMobile, routeUserMobile };
type StatusApp = "review" | "draft" | "reject" | "publish";
const routeAdminMobile = {
userAccess: ({ id }: { id: string }) => `/admin/user-access/${id}`,
// JOB
jobDetail: ({ id, status }: { id: string; status: StatusApp }) =>
`/admin/job/${id}/${status}`,
jobByStatus: ({ status }: { status: StatusApp }) =>
`/admin/job/${status}/status`,
};
const routeUserMobile = {
home: `/(user)/home`,
// JOB
jobByStatus: ({ status }: { status?: StatusApp }) =>
`/job/(tabs)/status?status=${status}`,
};

View File

@@ -0,0 +1,36 @@
// Jika semua custom type diawali "custom_"
export type NotificationMobilePayload = {
title: NotificationMobileTitleType;
body: NotificationMobileBodyType;
userLoginId?: string;
appId?: string;
status?: string;
type: "announcement" | "trigger";
deepLink: string;
kategoriApp: TypeNotificationCategoryApp;
};
export type NotificationMobileTitleType =
| (string & { __type: "NotificationMobileTitleType" })
| "Pengajuan Review"
| "Review Selesai";
export type NotificationMobileBodyType =
// USER
| (string & { __type: "NotificationMobileBodyType" })
| "Terdapat pengajuan baru yang perlu direview"
// ADMIN
| "Pengajuan data anda telah di tolak !"
| "Selamat data anda telah terpublikasi"
export type TypeNotificationCategoryApp =
| "EVENT"
| "JOB"
| "VOTING"
| "DONASI"
| "INVESTASI"
| "COLLABORATION"
| "FORUM"
| "OTHER";