notify.ts 831 B

123456789101112131415161718192021222324252627282930313233
  1. import { db } from '@/lib/db'
  2. import { notificationBus } from '@/lib/notification-bus'
  3. type NotifyInput = {
  4. organizationId: string
  5. type: string
  6. title: string
  7. message: string
  8. entityType: string
  9. entityId: string
  10. entityUrl: string
  11. }
  12. export async function notify(input: NotifyInput) {
  13. try {
  14. const notification = await db.notification.create({
  15. data: {
  16. type: input.type,
  17. title: input.title,
  18. message: input.message,
  19. entityType: input.entityType,
  20. entityId: input.entityId,
  21. entityUrl: input.entityUrl,
  22. organizationId: input.organizationId,
  23. },
  24. })
  25. // Emit to in-process bus — the WS route subscribes and broadcasts
  26. notificationBus.emit('notification', notification)
  27. } catch (error) {
  28. console.error('[notify] Failed:', error)
  29. }
  30. }