Skip to main content

Domain Events

The access-control libraries emit domain events so other services can react when user or role data changes. Events are dispatched via the @digiwedge/scl-events module and persisted in the outbox table for reliable delivery.

Events

EventTriggerPayload
user.createdNew user account createduserId, tenantId, invited
user.updatedUser details or role assignments changeduserId, changedFields
user.deletedUser soft-deleted or permanently removeduserId, tenantId
role.createdAdmin defines a new roleroleId, tenantId
role.assignedRole granted to a useruserId, roleId, tenantId
session.revokedAll sessions for a user revokeduserId, reason

Event Flow

  1. Domain service performs operation (e.g., UserDataService.createUser)
  2. Event is created and written to outbox table
  3. Outbox processor publishes event to message broker
  4. Subscribing services receive and process event

Subscribing to Events

import { OnEvent } from '@nestjs/event-emitter';

@Injectable()
export class UserEventHandler {
@OnEvent('user.created')
handleUserCreated(event: UserCreatedEvent) {
// React to new user
}

@OnEvent('role.assigned')
handleRoleAssigned(event: RoleAssignedEvent) {
// React to role assignment
}
}

Validation Rules

Incoming HTTP DTOs are validated with class-validator decorators:

EntityValidation
Tenant names3–50 characters (@Length(3,50))
Invitation tokensExactly 32 characters (@Length(32,32))
Pagination skipInteger, default 0
Pagination takeInteger, default 20, max 100

API Schemas

The backend exposes OpenAPI documentation:

  • Endpoint: GET /api/docs/json
  • UI: GET /api/docs

Frontend packages consume this schema with openapi-generator-cli to create typed clients and form validators.

Source Files

  • Events module: libs/scl/scl-events
  • Event types: libs/access-control/src/lib/events
  • Outbox processor: libs/scl/scl-events/src/lib/outbox