Services Deep Dive
In-depth look at the business logic layer: UserService and SecurityDetailsService.
User Service (UserServiceImpl)
The UserServiceImpl is the orchestrator for all user lifecycle events.
1. User Registration (register)
This is a multi-step orchestration:
- Duplicate Check: Verifies if the email or primary contact already exists in MongoDB.
- Mapping: Uses
UserMapperto convertRegisterDtoto aUserentity. - User ID Generation: Creates a unique
userIdusing a SHA-256 hash. - Save User: Persists the initial User document to MongoDB.
- Keycloak Sync: Calls the Auth Service to create the identity record.
- Progress Tracking: Saves the initial
RegistrationProgressrecord. - Security Setup: Initializes an empty
SecurityDetailsrecord. - Kafka Event: Publishes the first
user-create-event.
2. Profile Management (updateUser)
Handles both status changes and profile updates:
- Locking: If
isLockUseris true, sets status toBLOCKEDand emits a Kafka event. - Contacts: Manages primary and secondary contact numbers without deleting history.
- Addresses: Manages permanent and residential addresses, replacing existing ones of the same type.
Security Details Service (SecurityDetailsServiceImpl)
Manages sensitive security metadata and account integrity.
1. Update Security Credentials
Merges new security questions and answers into the SecurityDetails document. It then triggers the UserValidator to check if the registration is complete.
2. Login Failure Handling
Increments the loginFailedAttempts counter.
Java code-highlight
if (loginFailed) {
securityDetails.setLoginFailedAttempts(securityDetails.getLoginFailedAttempts() + 1);
if (securityDetails.getLoginFailedAttempts() >= 5) {
// Trigger account block
userService.updateUser(userId, new UserUpdateDto(true, null, null));
}
}
Registration Engine (UserValidator)
The UserValidator is not a service but a utility consumed by the service layer. It acts as a state transition listener:
- Step 1 Complere?: Advance to
ADD_ADDRESS. - Step 2 Complete?: Advance to
ADD_SECURITY_CREDENTIALS. - Step 3 Complete?: Set status to
REGISTRATION_COMPLETE.
Each transition triggers a new
RegistrationProgress entry and a Kafka message.