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
10 changes: 10 additions & 0 deletions src/main/java/com/financebot/alert/domain/TightBudgetAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.financebot.alert.domain;

import java.math.BigDecimal;

public record TightBudgetAlert(
BigDecimal commitmentPercentage,
BigDecimal projectedNet,
String explanation
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.financebot.alert.service;

import com.financebot.alert.domain.TightBudgetAlert;
import com.financebot.analysis.dto.response.FinancialCommitmentResponse;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class TightBudgetDetector {

private static final BigDecimal ALERT_THRESHOLD = BigDecimal.valueOf(60);

public TightBudgetAlert detect(FinancialCommitmentResponse analysis) {
if (analysis == null) {
throw new IllegalArgumentException("Financial analysis is required");
}
BigDecimal percentage = valueOrZero(analysis.commitmentPercentage());
BigDecimal projectedNet = valueOrZero(analysis.projectedNetNextMonth());
if (percentage.compareTo(ALERT_THRESHOLD) < 0 && projectedNet.signum() >= 0) {
return null;
}
String reason = projectedNet.signum() < 0
? "a projeção do próximo mês ficou negativa"
: "o comprometimento projetado atingiu " + percentage + "%";
return new TightBudgetAlert(
percentage,
projectedNet,
"Seu orçamento está apertado: " + reason + "."
);
}

private BigDecimal valueOrZero(BigDecimal value) {
return value == null ? BigDecimal.ZERO : value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.financebot.alert.service;

import com.financebot.analysis.dto.response.FinancialCommitmentResponse;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class TightBudgetDetectorTest {

private final TightBudgetDetector detector = new TightBudgetDetector();

@Test
void shouldDetectCommitmentAtSixtyPercent() {
var alert = detector.detect(analysis("60", "100"));

assertThat(alert).isNotNull();
assertThat(alert.commitmentPercentage()).isEqualByComparingTo("60");
assertThat(alert.explanation()).contains("60");
}

@Test
void shouldDetectNegativeProjectedNetEvenBelowPercentageThreshold() {
var alert = detector.detect(analysis("20", "-1"));

assertThat(alert).isNotNull();
assertThat(alert.explanation()).contains("negativa");
}

@Test
void shouldIgnoreHealthyBudget() {
assertThat(detector.detect(analysis("59.99", "100"))).isNull();
}

@Test
void shouldRejectNullAnalysis() {
assertThatThrownBy(() -> detector.detect(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Financial analysis is required");
}

private FinancialCommitmentResponse analysis(String percentage, String projectedNet) {
return new FinancialCommitmentResponse(
BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO,
BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO,
new BigDecimal(projectedNet), new BigDecimal(percentage), 0L,
false, false, false, "LOW", ""
);
}
}
Loading