Compare commits
20 Commits
v1.5.33
...
mobile-not
| Author | SHA1 | Date | |
|---|---|---|---|
| a54f8599b4 | |||
| 09825756f3 | |||
| 2086692897 | |||
| 87515ae19f | |||
| 3552cf4f39 | |||
| d207b6feed | |||
| f1c8432fdc | |||
| 1cd4c3713e | |||
| 4307b383e3 | |||
| 0786d23336 | |||
| cb3511f973 | |||
| b4921c4e82 | |||
| a9325054eb | |||
| 819812149f | |||
| 75ba2b29ae | |||
| 54a4d15bdd | |||
| 1321f33da9 | |||
| fad0c33b9a | |||
| 565bab4998 | |||
| 7530a38c4d |
@@ -85,9 +85,8 @@ export async function POST(req: Request) {
|
|||||||
// =========== START SEND NOTIFICATION =========== //
|
// =========== START SEND NOTIFICATION =========== //
|
||||||
|
|
||||||
const findAllUserBySendTo = await prisma.user.findMany({
|
const findAllUserBySendTo = await prisma.user.findMany({
|
||||||
where: {
|
where: { masterUserRoleId: "2" },
|
||||||
masterUserRoleId: "2",
|
select: { id: true },
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Users to notify:", findAllUserBySendTo);
|
console.log("Users to notify:", findAllUserBySendTo);
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
import {
|
||||||
|
sendNotificationMobileToManyUser,
|
||||||
|
sendNotificationMobileToOneUser,
|
||||||
|
} from "@/lib/mobile/notification/send-notification";
|
||||||
|
import { routeUserMobile } from "@/lib/mobile/route-page-mobile";
|
||||||
|
import { NotificationMobileBodyType } from "../../../../../../../types/type-mobile-notification";
|
||||||
|
|
||||||
export { GET, PUT };
|
export { GET, PUT };
|
||||||
|
|
||||||
@@ -54,6 +60,9 @@ 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 { catatan, senderId } = data;
|
||||||
|
|
||||||
const { searchParams } = new URL(request.url);
|
const { searchParams } = new URL(request.url);
|
||||||
const status = searchParams.get("status");
|
const status = searchParams.get("status");
|
||||||
const fixStatus = _.startCase(status as string);
|
const fixStatus = _.startCase(status as string);
|
||||||
@@ -83,7 +92,7 @@ async function PUT(request: Request, { params }: { params: { id: string } }) {
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
masterStatusId: checkStatus.id,
|
masterStatusId: checkStatus.id,
|
||||||
catatan: data,
|
catatan: catatan,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
@@ -97,6 +106,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;
|
fixData = updt;
|
||||||
} else if (fixStatus === "Publish") {
|
} else if (fixStatus === "Publish") {
|
||||||
const updt = await prisma.job.update({
|
const updt = await prisma.job.update({
|
||||||
@@ -118,6 +139,35 @@ 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" }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const adminUsers = await prisma.user.findMany({
|
||||||
|
where: { masterUserRoleId: "1", NOT: { id: updt.authorId as any } },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
await sendNotificationMobileToManyUser({
|
||||||
|
recipientIds: adminUsers.map((user) => user.id),
|
||||||
|
senderId: data.authorId,
|
||||||
|
payload: {
|
||||||
|
title: "Ada lowongan kerja baru",
|
||||||
|
body: `${updt.title}` as NotificationMobileBodyType,
|
||||||
|
type: "announcement",
|
||||||
|
deepLink: routeUserMobile.jobDetailPublised({ id: id }),
|
||||||
|
kategoriApp: "JOB",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
fixData = updt;
|
fixData = updt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ async function GET(request: Request, { params }: { params: { id: string } }) {
|
|||||||
include: {
|
include: {
|
||||||
Author: {
|
Author: {
|
||||||
select: {
|
select: {
|
||||||
|
id: true,
|
||||||
username: true,
|
username: true,
|
||||||
nomor: true,
|
nomor: true,
|
||||||
Profile: {
|
Profile: {
|
||||||
|
|||||||
@@ -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 prisma from "@/lib/prisma";
|
||||||
import { NextResponse } from "next/server";
|
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", NOT: { id: data.authorId } },
|
||||||
|
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(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
success: true,
|
success: true,
|
||||||
@@ -54,10 +75,10 @@ async function GET(request: Request) {
|
|||||||
MasterStatus: {
|
MasterStatus: {
|
||||||
name: "Publish",
|
name: "Publish",
|
||||||
},
|
},
|
||||||
// title: {
|
// title: {
|
||||||
// contains: search || "",
|
// contains: search || "",
|
||||||
// mode: "insensitive",
|
// mode: "insensitive",
|
||||||
// },
|
// },
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: "desc",
|
createdAt: "desc",
|
||||||
@@ -90,46 +111,46 @@ async function GET(request: Request) {
|
|||||||
|
|
||||||
fixData = data;
|
fixData = data;
|
||||||
} else if (category === "beranda") {
|
} else if (category === "beranda") {
|
||||||
const data = await prisma.job.findMany({
|
const data = await prisma.job.findMany({
|
||||||
where: {
|
where: {
|
||||||
isActive: true,
|
isActive: true,
|
||||||
isArsip: false,
|
isArsip: false,
|
||||||
MasterStatus: {
|
MasterStatus: {
|
||||||
name: "Publish",
|
name: "Publish",
|
||||||
},
|
},
|
||||||
title: {
|
title: {
|
||||||
contains: search || "",
|
contains: search || "",
|
||||||
mode: "insensitive",
|
mode: "insensitive",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: "desc",
|
createdAt: "desc",
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
title: true,
|
title: true,
|
||||||
deskripsi: true,
|
deskripsi: true,
|
||||||
authorId: true,
|
authorId: true,
|
||||||
MasterStatus: {
|
MasterStatus: {
|
||||||
select: {
|
select: {
|
||||||
name: true,
|
name: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Author: {
|
Author: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
username: true,
|
username: true,
|
||||||
Profile: {
|
Profile: {
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
imageId: true,
|
imageId: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
fixData = data;
|
fixData = data;
|
||||||
}
|
}
|
||||||
|
|||||||
117
src/lib/mobile/notification/send-notification.ts
Normal file
117
src/lib/mobile/notification/send-notification.ts
Normal 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,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
20
src/lib/mobile/route-page-mobile.ts
Normal file
20
src/lib/mobile/route-page-mobile.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
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
|
||||||
|
jobDetailPublised: ({ id }: { id: string }) => `/job/${id}`,
|
||||||
|
jobByStatus: ({ status }: { status?: StatusApp }) =>
|
||||||
|
`/job/(tabs)/status?status=${status}`,
|
||||||
|
};
|
||||||
38
types/type-mobile-notification.ts
Normal file
38
types/type-mobile-notification.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// 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"
|
||||||
|
// to ALL user
|
||||||
|
| "Ada lowongan kerja baru"
|
||||||
|
|
||||||
|
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";
|
||||||
Reference in New Issue
Block a user