Skip to content

Commit 8433163

Browse files
committed
PR feedback: Handle multiple active transactions gracefully
Handle cases with multiple active transactions by returning the most recent one instead of throwing an IllegalStateException.
1 parent d28d20c commit 8433163

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/de/rwth/idsg/steve/service/NotificationServiceForUser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import de.rwth.idsg.steve.service.notification.OcppTransactionEnded;
2828
import de.rwth.idsg.steve.service.notification.OcppTransactionStarted;
2929
import de.rwth.idsg.steve.utils.TransactionStopServiceHelper;
30-
import de.rwth.idsg.steve.web.dto.UserQueryForm;
3130
import lombok.RequiredArgsConstructor;
3231
import lombok.extern.slf4j.Slf4j;
3332
import org.joda.time.DateTime;
@@ -57,7 +56,7 @@ public class NotificationServiceForUser {
5756
public void ocppStationStatusFailure(OcppStationStatusFailure event) {
5857
log.debug("Processing: {}", event);
5958

60-
var transaction = transactionService.getActiveTransaction(event.getChargeBoxId(), event.getConnectorId());
59+
var transaction = transactionService.getLatestActiveTransaction(event.getChargeBoxId(), event.getConnectorId());
6160
if (transaction == null) {
6261
return;
6362
}
@@ -117,7 +116,7 @@ public void ocppTransactionStarted(OcppTransactionStarted event) {
117116
public void ocppStationStatusSuspendedEV(OcppStationStatusSuspendedEV event) {
118117
log.debug("Processing: {}", event);
119118

120-
var transaction = transactionService.getActiveTransaction(event.getChargeBoxId(), event.getConnectorId());
119+
var transaction = transactionService.getLatestActiveTransaction(event.getChargeBoxId(), event.getConnectorId());
121120
if (transaction == null) {
122121
return;
123122
}

src/main/java/de/rwth/idsg/steve/service/TransactionService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Transaction getTransaction(int transactionPk) {
8080
return transactionRepository.getTransactions(form).getFirst();
8181
}
8282

83-
public Transaction getActiveTransaction(String chargeBoxId, Integer connectorId) {
83+
public Transaction getLatestActiveTransaction(String chargeBoxId, Integer connectorId) {
8484
TransactionQueryForm form = new TransactionQueryForm();
8585
form.setChargeBoxId(chargeBoxId);
8686
form.setConnectorId(connectorId);
@@ -93,7 +93,10 @@ public Transaction getActiveTransaction(String chargeBoxId, Integer connectorId)
9393
} else if (transactions.size() == 1) {
9494
return transactions.get(0);
9595
} else {
96-
throw new IllegalStateException("There are multiple active transactions with the same charge box id and connector id");
96+
log.warn("Found multiple active transactions for chargeBoxId '{}' and connectorId '{}'. Returning the most recent one.", chargeBoxId, connectorId);
97+
return transactions.stream()
98+
.max(Comparator.comparing(Transaction::getStartTimestamp))
99+
.orElse(null); // Should not be null here, but for safety
97100
}
98101
}
99102

0 commit comments

Comments
 (0)