diff --git a/src/main/java/io/github/opendonationassistant/goal/Goal.java b/src/main/java/io/github/opendonationassistant/goal/Goal.java index 464f39c..de9cae7 100644 --- a/src/main/java/io/github/opendonationassistant/goal/Goal.java +++ b/src/main/java/io/github/opendonationassistant/goal/Goal.java @@ -35,7 +35,7 @@ public Goal( this.setDefault(isDefault); } - public void handlePayment(CompletedPaymentNotification payment) { + public Goal handlePayment(CompletedPaymentNotification payment) { var paid = payment.getAmount().getMajor(); var oldAmount = this.getAccumulatedAmount(); this.setAccumulatedAmount( @@ -47,6 +47,7 @@ public void handlePayment(CompletedPaymentNotification payment) { ); log.debug("Updating goal {} to {}", getId(), this); repository.update(this); + return this; } public Goal update(Map config) { @@ -69,14 +70,14 @@ public void delete(){ repository.delete(this); } - private GoalCommand createCommand(String type) { + public GoalCommand createUpdateCommand() { var command = new GoalCommand(); command.setAccumulatedAmount(this.getAccumulatedAmount()); command.setRequiredAmount(this.getRequiredAmount()); command.setBriefDescription(this.getBriefDescription()); command.setFullDescription(this.getFullDescription()); command.setGoalId(this.getId()); - command.setType(type); + command.setType("update"); return command; } diff --git a/src/main/java/io/github/opendonationassistant/goal/GoalsPaymentListener.java b/src/main/java/io/github/opendonationassistant/goal/GoalsPaymentListener.java index 8908796..cc43474 100644 --- a/src/main/java/io/github/opendonationassistant/goal/GoalsPaymentListener.java +++ b/src/main/java/io/github/opendonationassistant/goal/GoalsPaymentListener.java @@ -6,33 +6,40 @@ import io.micronaut.rabbitmq.annotation.Queue; import io.micronaut.rabbitmq.annotation.RabbitListener; import jakarta.inject.Inject; - import java.util.List; import java.util.Optional; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @RabbitListener public class GoalsPaymentListener { - private final Logger log = LoggerFactory.getLogger(GoalsPaymentListener.class); + private final Logger log = LoggerFactory.getLogger( + GoalsPaymentListener.class + ); private final GoalFactory goalFactory; private final ConfigCommandSender configCommandSender; + private final GoalCommandSender goalCommandSender; @Inject - public GoalsPaymentListener(GoalFactory goalFactory, ConfigCommandSender configCommandSender) { + public GoalsPaymentListener( + GoalFactory goalFactory, + ConfigCommandSender configCommandSender, + GoalCommandSender goalCommandSender + ) { this.goalFactory = goalFactory; this.configCommandSender = configCommandSender; + this.goalCommandSender = goalCommandSender; } @Queue("payments_for_goal") public void listen(CompletedPaymentNotification payment) { log.info("Received notification: {}", payment); - Optional.ofNullable(payment.getGoal()) + Optional updatedGoal = Optional + .ofNullable(payment.getGoal()) .flatMap(goalFactory::getBy) - .ifPresent(goal -> goal.handlePayment(payment)); + .map(goal -> goal.handlePayment(payment)); List savedGoals = goalFactory.findFor(payment.getRecipientId()); var command = new ConfigPutCommand(); command.setKey("goals"); @@ -40,6 +47,7 @@ public void listen(CompletedPaymentNotification payment) { command.setOwnerId(payment.getRecipientId()); command.setName("paymentpage"); configCommandSender.send(command); + updatedGoal.map(goal -> goal.createUpdateCommand()) + .ifPresent(updateCommand -> goalCommandSender.send("%sgoal".formatted(payment.getRecipientId()), updateCommand)); } - }