Use Case Documentation
Overview
The member management module implements various use cases for managing team members within the Bee O'clock system. The module follows Clean Architecture principles and CQRS pattern to separate business logic concerns.
Core Use Cases
1. Get Paged Members Use Case
Purpose: Retrieves a paginated list of members for a tenant with optional filtering and searching capabilities.
Location: GetPagedMembersUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationpageSize?: number- Number of items per pagepage?: number- Page number to retrievesearchTerm?: string- Optional search term for filteringassignmentFilter?: string- Optional assignment filter
Business Logic:
Validates actor permissions (
VIEW_MEMBER)Applies search filters if provided
Executes paginated query through repository
Returns paginated result with members and metadata
Output: PaginatedResponse<MemberDto>
2. Get Member By ID Use Case
Purpose: Retrieves a specific member by their unique identifier.
Location: GetMemberByIdUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmemberId: string- Unique identifier of the member
Business Logic:
Validates actor permissions (
VIEW_MEMBER)Retrieves member from repository
Throws exception if member not found
Maps persistence model to DTO
Output: MemberDto
3. Add New Member Use Case
Purpose: Creates a new member within a tenant, handling invitation codes and account linking.
Location: AddNewMemberUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationaccountId: string- Associated account identifiermember: MemberDto- Member data to create
Business Logic:
Validates actor permissions (
CREATE_MEMBER)Checks tenant tariff plan limits
Validates against maximum specialist limit
Initializes member with default values (role, assignments, referral code)
Creates member through command
Links existing account if found by email
Creates member context for account linkage
Emits member added event for notifications
Handles transaction rollback on errors
Special Considerations:
Checks tariff plan specialist limits
Generates unique invitation codes
Links to existing accounts automatically
Emits events for downstream processing
Output: string (Member ID)
4. Update Member Use Case
Purpose: Updates an existing member's information with role change validation.
Location: UpdateMemberUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmember: MemberDto- Updated member data
Business Logic:
Validates actor permissions (
EDIT_MEMBER)Retrieves existing member for comparison
Validates role changes:
Prevents self-removal of owner role
Ensures proper role change permissions
Executes update command
Handles transaction rollback on errors
Special Role Validation:
Owners cannot remove their own owner role
Role changes require specific validations
Compares current vs new roles for changes
Output: void
5. Delete Member Use Case
Purpose: Safely deletes a member with comprehensive validation checks.
Location: DeleteMemberUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmemberId: string- ID of member to delete
Business Logic:
Validates actor permissions (
DELETE_MEMBER)Checks member exists
Validates actor is owner
Prevents self-deletion
Checks for active events/orders
Marks member as deleted (soft delete)
Publishes deletion notification
Emits member deleted event
Clears related caches
Safety Validations:
Only owners can delete members
Cannot delete self
Cannot delete members with active events
Checks for active orders before deletion
Output: void
6. Update Member Assignment Use Case
Purpose: Updates member service assignments and permissions.
Location: UpdateMemberAssignmentUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmemberId: string- ID of member to updateassignments: AssignmentsDto- New assignment configuration
Business Logic:
Validates actor permissions (
EDIT_MEMBER)Retrieves existing member
Updates assignment configuration
Handles full access vs specific service assignments
Saves updated assignments
Output: void
7. Avatar Management Use Cases
Upsert Member Avatar
Purpose: Uploads or updates a member's avatar image.
Location: UpsertMemberAvatarUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmemberId: string- ID of memberfile: Express.Multer.File- Avatar image file
Business Logic:
Validates actor permissions (
EDIT_MEMBER)Validates file format and size
Uploads to storage service
Updates member avatar URL
Handles storage errors
Delete Member Avatar
Purpose: Removes a member's avatar image.
Location: DeleteMemberAvatarUseCase
Input Parameters:
ctx: TenantActorContext- Context with tenant and actor informationmemberId: string- ID of member
Business Logic:
Validates actor permissions (
EDIT_MEMBER)Removes avatar from storage
Clears avatar URL from member record
Permission Requirements
All use cases require specific permissions validated through PermissionValidatorUseCase:
VIEW_MEMBER: Required for read operations
CREATE_MEMBER: Required for creating new members
EDIT_MEMBER: Required for updates and avatar management
DELETE_MEMBER: Required for deletion operations
Error Handling
Common Exceptions
MemberNotFoundException: When member ID doesn't existMemberUpdationException: When update validation failsMemberCannotDeleteItselfException: Self-deletion preventionMemberNotAuthorizedToDeleteException: Insufficient permissions for deletionMemberHasActiveEventsException: Cannot delete member with active eventsTenantTariffPlanMaxMembersReachedException: Specialist limit exceededTenantHasNoActiveTariffPlanException: No active plan for member creation
Error Response Pattern
All use cases follow consistent error handling:
Log errors with context
Preserve original error for debugging
Throw domain-specific exceptions
Handle transaction rollback automatically
Integration Points
Event Publishing
MemberAddedMessage: Published when member is created
MemberDeletedMessage: Published when member is deleted
MemberDeletedNotification: Internal notification for cleanup
Cache Management
Automatic cache invalidation on mutations
Cache keys:
GET_MEMBER,GET_PAGED_MEMBERSUses
@DeleteCachedecorator for automatic cleanup
Transaction Management
Uses
@InitializeSharedDbConnectionfor transaction supportAutomatic rollback on failures
Shared unit of work pattern for consistency
Business Rules
Member Creation Rules
Tariff Plan Validation: Must have active tariff plan
Specialist Limits: Cannot exceed plan's specialist limit
Default Role Assignment: New members get SPECIALIST role by default
Invitation Codes: Automatically generated for each member
Account Linking: Automatically links to existing accounts by email
Member Update Rules
Role Change Validation: Complex validation for role modifications
Owner Protection: Owners cannot remove their own owner role
Permission Inheritance: Role changes affect permission inheritance
Member Deletion Rules
Owner Only: Only owners can delete members
Self-Protection: Cannot delete own account
Active Events Check: Cannot delete members with active events
Soft Delete: Members are marked as deleted, not physically removed
Assignment Rules
Service Assignments: Can be full access or specific services
Permission Inheritance: Assignments affect service access
Validation: Assignments must reference valid services
Performance Considerations
Caching Strategy
Paginated results are cached
Individual member lookups are cached
Cache invalidation on mutations
Distributed cache support
Query Optimization
Indexed queries for pagination
Optimized search operations
Population of related data in single queries
Efficient filtering and sorting
Async Processing
Event publishing for notifications
Background processing for file uploads
Deferred cleanup operations
Message queue integration
Testing Strategy
Unit Tests
Mock all external dependencies
Test business logic in isolation
Cover all error scenarios
Validate permission checks
Integration Tests
Test with real database
Validate transaction behavior
Test event publishing
Cache integration testing
End-to-End Tests
Full workflow testing
API endpoint validation
File upload testing
Permission integration testing
Last updated
Was this helpful?