From 20866928975114a7cb4e9496a4078b8d552ae7b8 Mon Sep 17 00:00:00 2001 From: bagasbanuna Date: Thu, 8 Jan 2026 10:14:35 +0800 Subject: [PATCH] Fix API notifikasi untuk job ### No Issue --- src/app/api/mobile/admin/job/[id]/route.ts | 32 ++++++++++++++++++- src/app/api/mobile/job/route.ts | 4 +-- .../mobile/notification/send-notification.ts | 10 ++++-- src/lib/mobile/route-page-mobile.ts | 9 ++++-- types/type-mobile-notification.ts | 22 +++++++++++-- 5 files changed, 67 insertions(+), 10 deletions(-) diff --git a/src/app/api/mobile/admin/job/[id]/route.ts b/src/app/api/mobile/admin/job/[id]/route.ts index 67975023..694d8c56 100644 --- a/src/app/api/mobile/admin/job/[id]/route.ts +++ b/src/app/api/mobile/admin/job/[id]/route.ts @@ -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; } diff --git a/src/app/api/mobile/job/route.ts b/src/app/api/mobile/job/route.ts index 3744f347..2a39a504 100644 --- a/src/app/api/mobile/job/route.ts +++ b/src/app/api/mobile/job/route.ts @@ -30,8 +30,8 @@ async function POST(request: Request) { recipientIds: adminUsers.map((user) => user.id), senderId: data.authorId, payload: { - title: "Job: Pengajuan Review", - body: data.title, + title: "Pengajuan Review", + body: "Terdapat pengajuan baru yang perlu direview", type: "announcement", deepLink: routeAdminMobile.jobByStatus({ status: "review" }), kategoriApp: "JOB", diff --git a/src/lib/mobile/notification/send-notification.ts b/src/lib/mobile/notification/send-notification.ts index faf8898e..17700f31 100644 --- a/src/lib/mobile/notification/send-notification.ts +++ b/src/lib/mobile/notification/send-notification.ts @@ -2,6 +2,7 @@ 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) @@ -20,15 +21,20 @@ export async function sendNotificationMobileToOneUser({ 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: payload.title, + title: titleFix, pesan: payload.body, deepLink: payload.deepLink, kategoriApp: payload.kategoriApp, recipientId: recipientId, senderId: senderId, + type: payload.type.trim(), }, }); @@ -51,7 +57,7 @@ export async function sendNotificationMobileToOneUser({ await adminMessaging.send({ token, notification: { - title: payload.title, + title: titleFix, body: payload.body, }, data: { diff --git a/src/lib/mobile/route-page-mobile.ts b/src/lib/mobile/route-page-mobile.ts index d9ba6084..cd46ae12 100644 --- a/src/lib/mobile/route-page-mobile.ts +++ b/src/lib/mobile/route-page-mobile.ts @@ -5,10 +5,15 @@ 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`, + 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}`, }; diff --git a/types/type-mobile-notification.ts b/types/type-mobile-notification.ts index b3c9884f..2991ad43 100644 --- a/types/type-mobile-notification.ts +++ b/types/type-mobile-notification.ts @@ -1,14 +1,30 @@ +// Jika semua custom type diawali "custom_" + export type NotificationMobilePayload = { - title: string; - body: string; + title: NotificationMobileTitleType; + body: NotificationMobileBodyType; userLoginId?: string; appId?: string; status?: string; type: "announcement" | "trigger"; deepLink: string; - kategoriApp: TypeNotificationCategoryApp + 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"