// src/server/features/reference_books/lib/access.notifications.ts
// Notification utilities for external access feature

import { db } from '@serverShared/db/client';
import { employees } from '@serverShared/db/schema';
import { eq } from 'drizzle-orm';
import { notifyUser } from '@serverShared/plugins/socket';

export async function notifyAccessUpdate(event: string, data: Record<string, any>, tx?: any): Promise<void> {
  const client = tx || db;

  const employeesResult = await client
    .select({ id: employees.id })
    .from(employees)
    .where(eq(employees.isFired, false));

  const userIds = employeesResult.map((e: { id: string }) => e.id);

  if (userIds.length > 0) {
    await Promise.all(userIds.map((userId: string) => notifyUser(userId, event as any, data)));
  }
}

