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
Expand Up @@ -39,11 +39,15 @@ public void handle(EmitAlertsCommand command) {
List<UserId> 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<InboxId> inboxIdOpt = this.inboxReadModel.getInboxId(userId);
if (inboxIdOpt.isEmpty()) {
log.warn("Inbox not found for userId {}", userId.value());
continue;
}

Optional<Inbox> userOpt = this.eventStore.get(inboxId);
InboxId inboxId = inboxIdOpt.get();

Optional<Inbox> userOpt = this.eventStore.get(inboxId);
if (userOpt.isEmpty()) {
log.warn("User with ID {} not found in event store", userId);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<InboxId> optInboxId = this.inboxReadModel.getInboxId(userId);
if (optInboxId.isEmpty()) {
log.warn("Inbox not found for userId {}", userId.value());
return;
}

InboxId inboxId = optInboxId.get();

Optional<Inbox> 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());

Expand All @@ -54,4 +69,4 @@ public void handle(RegisterPortfolioAssetCommand command) {

inbox.markEventsAsCommitted();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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());
Expand Down