Skip to content

Commit

Permalink
send goal command
Browse files Browse the repository at this point in the history
  • Loading branch information
stCarolas committed Jul 16, 2024
1 parent b5a98f3 commit aefa695
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/main/java/io/github/opendonationassistant/goal/Goal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<String, Object> config) {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,48 @@
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<Goal> updatedGoal = Optional
.ofNullable(payment.getGoal())
.flatMap(goalFactory::getBy)
.ifPresent(goal -> goal.handlePayment(payment));
.map(goal -> goal.handlePayment(payment));
List<Goal> savedGoals = goalFactory.findFor(payment.getRecipientId());
var command = new ConfigPutCommand();
command.setKey("goals");
command.setValue(savedGoals);
command.setOwnerId(payment.getRecipientId());
command.setName("paymentpage");
configCommandSender.send(command);
updatedGoal.map(goal -> goal.createUpdateCommand())
.ifPresent(updateCommand -> goalCommandSender.send("%sgoal".formatted(payment.getRecipientId()), updateCommand));
}

}

0 comments on commit aefa695

Please sign in to comment.