130 lines
3.5 KiB
TypeScript
130 lines
3.5 KiB
TypeScript
// import { Queue, Worker } from 'bullmq';
|
|
// import { db } from '@/src/db/db_index'
|
|
import { scaffoldAssetUrl } from '@/src/lib/s3-client'
|
|
import { queueDataPusher } from '@/src/lib/queue-data-pusher'
|
|
import {
|
|
ORDER_PACKAGED_MESSAGE,
|
|
ORDER_DELIVERED_MESSAGE
|
|
} from '@/src/lib/const-strings';
|
|
|
|
|
|
export const notificationQueue:any = {};
|
|
|
|
// export const notificationQueue = new Queue(NOTIFS_QUEUE, {
|
|
// connection: { url: redisUrl },
|
|
// defaultJobOptions: {
|
|
// removeOnComplete: true,
|
|
// removeOnFail: 10,
|
|
// attempts: 3,
|
|
// },
|
|
// });
|
|
|
|
export const notificationWorker:any = {};
|
|
// export const notificationWorker = new Worker(NOTIFS_QUEUE, async (job) => {
|
|
// if (!job) return;
|
|
//
|
|
// const { name, data } = job;
|
|
// console.log(`Processing notification job ${job.id} - ${name}`);
|
|
//
|
|
// if (name === 'send-admin-notification') {
|
|
// await sendAdminNotification(data);
|
|
// } else if (name === 'send-notification') {
|
|
// // Handle legacy notification type
|
|
// console.log('Legacy notification job - not implemented yet');
|
|
// }
|
|
// }, {
|
|
// connection: { url: redisUrl },
|
|
// concurrency: 5,
|
|
// });
|
|
|
|
export async function sendAdminNotification(data: {
|
|
tokens: string[];
|
|
title: string;
|
|
body: string;
|
|
imageUrl: string | null;
|
|
orderId?: number | null;
|
|
}) {
|
|
const { tokens, title, body, imageUrl, orderId } = data;
|
|
const { Expo } = await import('expo-server-sdk')
|
|
|
|
// Validate Expo push tokens (drop invalid ones)
|
|
const validTokens = tokens.filter((token) => {
|
|
if (!Expo.isExpoPushToken(token)) {
|
|
console.error(`Invalid Expo push token: ${token}`);
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
if (validTokens.length === 0) {
|
|
return;
|
|
}
|
|
|
|
// Generate signed URL for image if provided
|
|
const signedImageUrl = imageUrl ? scaffoldAssetUrl(imageUrl) : null;
|
|
|
|
// Send notification
|
|
const expo = new Expo();
|
|
const messages = validTokens.map((token) => ({
|
|
to: token,
|
|
sound: 'default' as const,
|
|
title,
|
|
body,
|
|
data: {
|
|
imageUrl,
|
|
...(orderId != null ? { orderId } : {}),
|
|
},
|
|
...(signedImageUrl ? {
|
|
attachments: [
|
|
{
|
|
url: signedImageUrl,
|
|
contentType: 'image/jpeg',
|
|
}
|
|
]
|
|
} : {}),
|
|
}));
|
|
|
|
try {
|
|
await expo.sendPushNotificationsAsync(messages);
|
|
} catch (error) {
|
|
console.log(`Failed to send notifications:`, error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// notificationWorker.on('completed', (job) => {
|
|
// if (job) console.log(`Notification job ${job.id} completed`);
|
|
// });
|
|
// notificationWorker.on('failed', (job, err) => {
|
|
// if (job) console.error(`Notification job ${job.id} failed:`, err);
|
|
// });
|
|
|
|
export async function scheduleNotification(userId: number, payload: any, options?: { delay?: number; priority?: number }) {
|
|
const jobData = { userId, ...payload };
|
|
await queueDataPusher.pushNotifQueue({ name: 'send-notification', jobData, options })
|
|
}
|
|
|
|
// Utility methods for specific notification events
|
|
export async function sendOrderPackagedNotification(userId: number, orderId?: string) {
|
|
await scheduleNotification(userId, {
|
|
title: 'Order Packaged',
|
|
body: ORDER_PACKAGED_MESSAGE,
|
|
type: 'order',
|
|
orderId
|
|
});
|
|
}
|
|
|
|
export async function sendOrderDeliveredNotification(userId: number, orderId?: string) {
|
|
await scheduleNotification(userId, {
|
|
title: 'Order Delivered',
|
|
body: ORDER_DELIVERED_MESSAGE,
|
|
type: 'order',
|
|
orderId
|
|
});
|
|
}
|
|
|
|
//
|
|
// process.on('SIGTERM', async () => {
|
|
// await notificationQueue.close();
|
|
// await notificationWorker.close();
|
|
// });
|