feat : ask for noti
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import "server-only";
|
||||
|
||||
import { and, eq, ne, or } from "drizzle-orm";
|
||||
import { and, eq, ne, or, sql } from "drizzle-orm";
|
||||
import webPush from "web-push";
|
||||
import { createNotifications } from "@/lib/notifications/repository";
|
||||
import { getDb } from "@/db";
|
||||
import { commissionPushSubscriptions, users } from "@/db/schema";
|
||||
|
||||
@@ -25,6 +26,12 @@ export async function sendCommissionMessagePush(message: {
|
||||
id: string; ticketId: string; ticketTitle: string; customerId: string; authorId: string;
|
||||
authorName: string; text: string | null;
|
||||
}) {
|
||||
const recipients = await getDb().select({ id: users.id, role: users.role, verified: users.emailVerified }).from(users)
|
||||
.where(and(ne(users.id, message.authorId), sql`${users.banned} is not true`, or(eq(users.id, message.customerId), and(eq(users.role, "admin"), eq(users.emailVerified, true)))));
|
||||
await createNotifications(recipients.map(user => {
|
||||
const admin = user.role === "admin" && user.verified && user.id !== message.customerId;
|
||||
return { userId: user.id, adminOnly: admin, url: `/${admin ? "admin/commission" : "commission/tickets"}/${message.ticketId}` };
|
||||
}), { eventKey: `commission:${message.id}`, kind: "commission_message", title: `${message.authorName} - ${message.ticketTitle}`, body: message.text?.slice(0, 300) || "ส่งรูปภาพ", messageId: message.id });
|
||||
if (!pushPublicKey()) return;
|
||||
const subscriptions = await getDb().select({ subscription: commissionPushSubscriptions })
|
||||
.from(commissionPushSubscriptions)
|
||||
|
||||
Reference in New Issue
Block a user