14-16 Mar 2026
Logica do Sistema

Notificacoes

Secao 5

Entidade Notification

Arquivo: cargo_fleet.Domain/Notifications/Notification.cs

PropriedadeTipoDescricao
TenantIdGuid?Multitenancy
TitlestringTitulo da notificacao
MessagestringCorpo da mensagem
EmitterIdstringQuem gerou (default: "Background Worker")
IsReadboolMarcacao de leitura

Metodo MarkAsRead(): seta IsRead = true se ainda nao marcado (idempotente).


NotificationManager

Arquivo: cargo_fleet.Domain/Notifications/NotificationManager.cs

Metodo CreateAsync(title, message, emitterId):

  • Cria e persiste diretamente no banco (InsertAsync com autoSave: true).
  • Usa [UnitOfWork] attribute para garantir transacao.
  • Usa CurrentTenant.Id do contexto para definir o TenantId.

NotificationAppService

Arquivo: cargo_fleet.Application/Notifications/NotificationAppService.cs

  • GetListAsync: lista notificacoes do tenant atual, ordenado por CreationTime DESC. Aceita input.Sorting via Dynamic LINQ sem whitelist (SEC2-03).
  • GetUnreadCountAsync: conta nao lidas do tenant atual.
  • MarkAllAsReadAsync: carrega todas nao lidas, marca, salva em batch via UpdateManyAsync.
  • DeleteAsync: deleta por ID sem verificar se pertence ao tenant atual (P-14 — IDOR potencial).

Classe herda de ApplicationService (sem autorizacao base — SEC2-08). Filtra manualmente por .Where(n => n.TenantId == CurrentTenant.Id) em vez de usar o filtro automatico ABP.


FirebaseNotificationDispatcher

Arquivo: cargo_fleet.Application/Notifications/FirebaseNotificationDispatcher.cs

  • DispatchAsync(notification)FirebaseMessagingAppService.SendNotificationToTenantAsync(title, body).
  • DispatchManyAsync(notifications) → loop sobre DispatchAsync (sequencial, nao em batch).

FirebaseMessagingAppService

Arquivo: cargo_fleet.Application/Messaging/FirebaseMessagingAppService.cs

Fluxo SendNotificationToTenantAsync:

  1. Carrega todos os DeviceToken do TenantId atual.
  2. Se nao ha tokens: log warning, retorna.
  3. Monta MulticastMessage com todos os tokens.
  4. Chama FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(message).
  5. Para cada falha: se erro Unregistered ou InvalidArgument → marca token para remocao.
  6. Deleta tokens invalidos em batch via DeleteManyAsync.

Incoerencias Identificadas

IDDescricao
P-14DeleteAsync sem verificacao de ownership de tenant
SEC2-08NotificationAppService sem [Authorize], filtro manual de TenantId
MED-05Notificacoes enviadas para todos os dispositivos do tenant, nao por usuario especifico
(escalabilidade)DispatchManyAsync sequencial — sem batching eficiente