import { getAllStaffNotifTokens } from '@/src/dbService' import { queueDataPusher } from '@/src/lib/queue-data-pusher' // Number of push tokens batched into a single queue message. const TOKENS_PER_QUEUE_MESSAGE = 5 /** * Queues a push notification to every admin/staff device. `orderId`, when set, * is delivered in the push `data` so the app can deep-link to that order. * Returns the number of tokens the notification was queued for. */ export async function notifyAllAdmins(payload: { title: string body: string imageUrl?: string | null orderId?: number | null }): Promise { const tokens = (await getAllStaffNotifTokens()).map((t) => t.token) let queuedCount = 0 for (let i = 0; i < tokens.length; i += TOKENS_PER_QUEUE_MESSAGE) { const tokenChunk = tokens.slice(i, i + TOKENS_PER_QUEUE_MESSAGE) try { await queueDataPusher.pushNotifQueue({ name: 'send-admin-notification', jobData: { tokens: tokenChunk, title: payload.title, body: payload.body, imageUrl: payload.imageUrl ?? null, orderId: payload.orderId ?? null, }, options: { attempts: 3, backoff: { type: 'exponential', delay: 2000, }, }, }) queuedCount += tokenChunk.length } catch (error) { console.error(`Failed to queue admin notification for tokens:`, error) } } return queuedCount }