Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.nkcoder.notification;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class NotificationService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

public void sendWelcomeEmail(String email, String userName) {
// TODO: implement email sending
System.out.println("Sending Welcome email to " + email + ", for user: " + userName);
logger.info("Sending Welcome email to {}, for user: {}", email, userName);
}

public void sendPasswordResetEmail(String email, String userName) {
// TODO: implement password reset email
System.out.println("Sending password reset email to " + email + ", for user: " + userName);
logger.info("Sending password reset email to {}, for user: {}", email, userName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.nkcoder.user.application.service;

import java.time.LocalDateTime;
import org.nkcoder.shared.kernel.domain.event.DomainEventPublisher;
import org.nkcoder.shared.kernel.domain.event.UserRegisteredEvent;
import org.nkcoder.shared.kernel.exception.AuthenticationException;
import org.nkcoder.shared.kernel.exception.ValidationException;
import org.nkcoder.user.application.dto.command.LoginCommand;
Expand All @@ -27,7 +29,6 @@

/** Application service for authentication use cases. Orchestrates domain objects and infrastructure services. */
@Service
@Transactional
public class AuthApplicationService {

private static final Logger logger = LoggerFactory.getLogger(AuthApplicationService.class);
Expand All @@ -42,22 +43,26 @@ public class AuthApplicationService {
private final TokenGenerator tokenGenerator;
private final AuthenticationService authenticationService;
private final TokenRotationService tokenRotationService;
private final DomainEventPublisher eventPublisher;

public AuthApplicationService(
UserRepository userRepository,
RefreshTokenRepository refreshTokenRepository,
PasswordEncoder passwordEncoder,
TokenGenerator tokenGenerator,
AuthenticationService authenticationService,
TokenRotationService tokenRotationService) {
TokenRotationService tokenRotationService,
DomainEventPublisher eventPublisher) {
this.userRepository = userRepository;
this.refreshTokenRepository = refreshTokenRepository;
this.passwordEncoder = passwordEncoder;
this.tokenGenerator = tokenGenerator;
this.authenticationService = authenticationService;
this.tokenRotationService = tokenRotationService;
this.eventPublisher = eventPublisher;
}

@Transactional
public AuthResult register(RegisterCommand command) {
logger.debug("Registering new user with email: {}", command.email());

Expand All @@ -75,6 +80,9 @@ public AuthResult register(RegisterCommand command) {
user = userRepository.save(user);
logger.debug("User registered with ID: {}", user.getId().value());

eventPublisher.publish(new UserRegisteredEvent(
user.getId().value(), user.getEmail().value(), user.getName().value()));

// Generate tokens
TokenFamily tokenFamily = TokenFamily.generate();
TokenPair tokens = tokenRotationService.generateTokens(user, tokenFamily);
Expand All @@ -85,6 +93,7 @@ public AuthResult register(RegisterCommand command) {
return AuthResult.of(user.getId().value(), user.getEmail().value(), user.getRole(), tokens);
}

@Transactional()
public AuthResult login(LoginCommand command) {
logger.debug("Logging in user with email: {}", command.email());

Expand Down Expand Up @@ -151,6 +160,7 @@ public AuthResult refreshTokens(RefreshTokenCommand command) {
}
}

@Transactional
public void logout(String refreshToken) {
logger.debug("Logging out user (all devices)");

Expand All @@ -163,6 +173,7 @@ public void logout(String refreshToken) {
});
}

@Transactional
public void logoutSingle(String refreshToken) {
logger.debug("Logging out user (single device)");

Expand All @@ -171,6 +182,7 @@ public void logoutSingle(String refreshToken) {
logger.debug("Logged out from current device");
}

@Transactional
public void cleanupExpiredTokens() {
logger.debug("Cleaning up expired refresh tokens");
refreshTokenRepository.deleteExpiredTokens(LocalDateTime.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.nkcoder.shared.kernel.domain.event.DomainEventPublisher;
import org.nkcoder.shared.kernel.exception.AuthenticationException;
import org.nkcoder.shared.kernel.exception.ValidationException;
import org.nkcoder.user.application.dto.command.LoginCommand;
Expand Down Expand Up @@ -62,6 +63,9 @@ class AuthApplicationServiceTest {
@Mock
private TokenRotationService tokenRotationService;

@Mock
private DomainEventPublisher eventPublisher;

private AuthApplicationService authApplicationService;

@BeforeEach
Expand All @@ -72,7 +76,8 @@ void setUp() {
passwordEncoder,
tokenGenerator,
authenticationService,
tokenRotationService);
tokenRotationService,
eventPublisher);
}

@Nested
Expand Down
Loading