diff --git a/src/main/java/io/autoinvestor/application/EmitAlertsCommandHandler.java b/src/main/java/io/autoinvestor/application/EmitAlertsCommandHandler.java index 8ecb9a8..170f39a 100644 --- a/src/main/java/io/autoinvestor/application/EmitAlertsCommandHandler.java +++ b/src/main/java/io/autoinvestor/application/EmitAlertsCommandHandler.java @@ -39,11 +39,15 @@ public void handle(EmitAlertsCommand command) { List usersId = this.portfolioRepository.getUsersIdByAssetAndRiskLevel(command.assetId(), command.riskLevel()); for (UserId userId : usersId) { - InboxId inboxId = this.inboxReadModel.getInboxId(userId) - .orElseThrow(() -> new InternalErrorException("Inbox not found for userId " + userId.value())); + Optional inboxIdOpt = this.inboxReadModel.getInboxId(userId); + if (inboxIdOpt.isEmpty()) { + log.warn("Inbox not found for userId {}", userId.value()); + continue; + } - Optional userOpt = this.eventStore.get(inboxId); + InboxId inboxId = inboxIdOpt.get(); + Optional userOpt = this.eventStore.get(inboxId); if (userOpt.isEmpty()) { log.warn("User with ID {} not found in event store", userId); continue; diff --git a/src/main/java/io/autoinvestor/application/RegisterPortfolioAssetCommandHandler.java b/src/main/java/io/autoinvestor/application/RegisterPortfolioAssetCommandHandler.java index 72661d5..8ca0609 100644 --- a/src/main/java/io/autoinvestor/application/RegisterPortfolioAssetCommandHandler.java +++ b/src/main/java/io/autoinvestor/application/RegisterPortfolioAssetCommandHandler.java @@ -8,11 +8,14 @@ import io.autoinvestor.domain.model.InboxId; import io.autoinvestor.domain.model.UserId; import io.autoinvestor.exceptions.InternalErrorException; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; +@Slf4j @Service public class RegisterPortfolioAssetCommandHandler { @@ -31,16 +34,28 @@ public RegisterPortfolioAssetCommandHandler(EventStoreRepository eventStore, Por public void handle(RegisterPortfolioAssetCommand command) { if (this.portfolioRepository.existsPortfolioAsset(command.userId(), command.assetId())) { - // do nothing + log.info("Asset {} already registered for user {}", command.assetId(), command.userId()); return; } UserId userId = UserId.from(command.userId()); - InboxId inboxId = this.inboxReadModel.getInboxId(userId) - .orElseThrow(() -> new InternalErrorException("Inbox not found for userId " + userId.value())); - Inbox inbox = this.eventStore.get(inboxId) - .orElseThrow(() -> new UserDoesNotExist("User doesn't exist with ID: " + command.userId())); + Optional optInboxId = this.inboxReadModel.getInboxId(userId); + if (optInboxId.isEmpty()) { + log.warn("Inbox not found for userId {}", userId.value()); + return; + } + + InboxId inboxId = optInboxId.get(); + + Optional optInbox = this.eventStore.get(inboxId); + + if (optInbox.isEmpty()) { + log.warn("User doesn't exist with ID: {}", command.userId()); + return; + } + + Inbox inbox = optInbox.get(); inbox.addPortfolioAsset(command.assetId()); @@ -54,4 +69,4 @@ public void handle(RegisterPortfolioAssetCommand command) { inbox.markEventsAsCommitted(); } -} \ No newline at end of file +} diff --git a/src/main/java/io/autoinvestor/application/RegisterUserCommandHandler.java b/src/main/java/io/autoinvestor/application/RegisterUserCommandHandler.java index 8ef8dec..386ff95 100644 --- a/src/main/java/io/autoinvestor/application/RegisterUserCommandHandler.java +++ b/src/main/java/io/autoinvestor/application/RegisterUserCommandHandler.java @@ -5,14 +5,14 @@ import io.autoinvestor.domain.events.EventPublisher; import io.autoinvestor.domain.events.EventStoreRepository; import io.autoinvestor.domain.model.Inbox; -import io.autoinvestor.domain.model.InboxId; import io.autoinvestor.domain.model.UserId; -import io.autoinvestor.exceptions.InternalErrorException; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.List; +@Slf4j @Service public class RegisterUserCommandHandler { @@ -32,7 +32,8 @@ public RegisterUserCommandHandler(EventStoreRepository eventStore, PortfolioRepo public void handle(RegisterUserCommand command) { UserId userId = UserId.from(command.userId()); if (this.inboxReadModel.getInboxId(userId).isPresent()) { - throw new UserAlreadyExists("User already exists with ID: " + command.userId()); + log.warn("Inbox already exists for userId {}", command.userId()); + return; } Inbox inbox = Inbox.create(command.userId(), command.riskLevel());