Skip to content

Commit e5aa411

Browse files
committed
add step amount to reel widget
1 parent a3710a2 commit e5aa411

7 files changed

Lines changed: 88 additions & 16 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>io.github.opendonationassistant</groupId>
2626
<artifactId>oda-rabbit-conf</artifactId>
27-
<version>0.7.50</version>
27+
<version>0.7.69</version>
2828
</dependency>
2929
<dependency>
3030
<groupId>com.fasterxml.uuid</groupId>

src/main/java/io/github/opendonationassistant/reel/Reel.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import io.github.opendonationassistant.commons.logging.ODALogger;
55
import io.github.opendonationassistant.events.CompletedPaymentNotification;
66
import io.github.opendonationassistant.events.widget.Widget;
7+
import io.github.opendonationassistant.events.widget.WidgetCommandSender;
8+
import io.github.opendonationassistant.events.widget.WidgetConfig;
9+
import io.github.opendonationassistant.events.widget.WidgetProperty;
10+
import io.github.opendonationassistant.events.widget.WidgetUpdateCommand;
711
import java.util.List;
812
import java.util.Map;
913
import java.util.Random;
@@ -16,14 +20,17 @@ public class Reel {
1620
private final ReelCommandSender commandSender;
1721
private final ReelDataRepository repository;
1822
private final Random random;
23+
private final WidgetCommandSender widgetSender;
1924

2025
public Reel(
2126
ReelData data,
2227
ReelCommandSender commandSender,
23-
ReelDataRepository repository
28+
ReelDataRepository repository,
29+
WidgetCommandSender widgetSender
2430
) {
2531
this.data = data;
2632
this.commandSender = commandSender;
33+
this.widgetSender = widgetSender;
2734
this.repository = repository;
2835
this.random = new Random();
2936
}
@@ -35,7 +42,7 @@ public void handlePayment(CompletedPaymentNotification payment) {
3542
if (data.items() == null || data.items().isEmpty()) {
3643
return;
3744
}
38-
if (!data.enabled()){
45+
if (!data.enabled()) {
3946
return;
4047
}
4148
log.info(
@@ -51,6 +58,27 @@ public void handlePayment(CompletedPaymentNotification payment) {
5158
command.setWidgetId(data.widgetConfigId());
5259
command.setPaymentId(payment.id());
5360
command.setRecipientId(payment.recipientId());
61+
widgetSender.send(
62+
new WidgetUpdateCommand(
63+
data.widgetConfigId(),
64+
new WidgetConfig(
65+
List.of(
66+
new WidgetProperty(
67+
"requiredAmount",
68+
"widget-reel-required-amount",
69+
"number",
70+
data.requiredAmount().getMajor() + data.stepAmount().getMajor()
71+
)
72+
)
73+
)
74+
)
75+
);
76+
try {
77+
Thread.sleep(30000);
78+
} catch (InterruptedException e) {
79+
// TODO Auto-generated catch block
80+
e.printStackTrace();
81+
}
5482
log.info("Send reel command", Map.of("command", command));
5583
commandSender.send("reel", command);
5684
}
@@ -72,6 +100,11 @@ public Reel update(Widget widget) {
72100
new Amount((Integer) property.value(), 0, "RUB")
73101
);
74102
}
103+
if ("stepAmount".equals(property.name())) {
104+
return data.withStepAmount(
105+
new Amount((Integer) property.value(), 0, "RUB")
106+
);
107+
}
75108
return data;
76109
},
77110
(first, second) -> {
@@ -80,6 +113,6 @@ public Reel update(Widget widget) {
80113
);
81114
log.info("Update reel", Map.of("data", updatedData));
82115
repository.update(updatedData);
83-
return new Reel(updatedData, commandSender, repository);
116+
return new Reel(updatedData, commandSender, repository, widgetSender);
84117
}
85118
}

src/main/java/io/github/opendonationassistant/reel/ReelCommandListener.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,26 @@ public ReelCommandListener(
2626

2727
@Queue(io.github.opendonationassistant.rabbit.Queue.Commands.REEL)
2828
public void listen(ReelCommand command) {
29-
HistoryCommand historyCommand = new HistoryCommand();
30-
HistoryItemData data = new HistoryItemData();
31-
data.setPaymentId(command.getPaymentId());
32-
ReelResult result = new ReelResult();
33-
result.setTitle(command.getSelection());
34-
data.setReelResults(List.of(result));
35-
historyCommand.setPartial(data);
36-
historyCommand.setType("update");
37-
historyCommandSender.send("history", historyCommand);
29+
HistoryItemData data = new HistoryItemData(
30+
null,
31+
command.getPaymentId(),
32+
null,
33+
null,
34+
command.getRecipientId(),
35+
null,
36+
null,
37+
null,
38+
null,
39+
null,
40+
null,
41+
null,
42+
null,
43+
List.of(new ReelResult(command.getSelection()))
44+
);
45+
historyCommandSender.send(
46+
"history",
47+
new HistoryCommand("update", data, false, false, false, false, false)
48+
);
3849
commandSender.send("%sreel".formatted(command.getRecipientId()), command);
3950
}
4051
}

src/main/java/io/github/opendonationassistant/reel/ReelData.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public record ReelData(
1616
String widgetConfigId,
1717
Amount accumulatedAmount,
1818
Amount requiredAmount,
19+
Amount stepAmount,
1920
@MappedProperty(converter = StringListConverter.class) List<String> items,
2021
Boolean enabled
2122
) {
@@ -30,6 +31,7 @@ public ReelData withEnabled(boolean value) {
3031
widgetConfigId,
3132
accumulatedAmount,
3233
requiredAmount,
34+
stepAmount,
3335
items,
3436
value
3537
);
@@ -42,6 +44,7 @@ public ReelData withItems(List<String> value) {
4244
widgetConfigId,
4345
accumulatedAmount,
4446
requiredAmount,
47+
stepAmount,
4548
value,
4649
enabled
4750
);
@@ -54,6 +57,20 @@ public ReelData withRequiredAmount(Amount value) {
5457
widgetConfigId,
5558
accumulatedAmount,
5659
value,
60+
stepAmount,
61+
items,
62+
enabled
63+
);
64+
}
65+
66+
public ReelData withStepAmount(Amount value) {
67+
return new ReelData(
68+
id,
69+
recipientId,
70+
widgetConfigId,
71+
accumulatedAmount,
72+
requiredAmount,
73+
value,
5774
items,
5875
enabled
5976
);

src/main/java/io/github/opendonationassistant/reel/ReelRepository.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.github.opendonationassistant.commons.Amount;
55
import io.github.opendonationassistant.commons.logging.ODALogger;
66
import io.github.opendonationassistant.events.widget.Widget;
7+
import io.github.opendonationassistant.events.widget.WidgetCommandSender;
78
import jakarta.inject.Inject;
89
import java.util.List;
910
import java.util.Map;
@@ -14,14 +15,17 @@ public class ReelRepository {
1415
private final ODALogger log = new ODALogger(this);
1516
private final ReelDataRepository repository;
1617
private final ReelCommandSender commandSender;
18+
private final WidgetCommandSender widgetSender;
1719

1820
@Inject
1921
public ReelRepository(
2022
ReelDataRepository repository,
21-
ReelCommandSender commandSender
23+
ReelCommandSender commandSender,
24+
WidgetCommandSender widgetSender
2225
) {
2326
this.repository = repository;
2427
this.commandSender = commandSender;
28+
this.widgetSender = widgetSender;
2529
}
2630

2731
public Optional<Reel> getBy(String recipientId, String widgetId) {
@@ -44,6 +48,7 @@ public Reel create(Widget widget) {
4448
widget.id(),
4549
new Amount(0, 0, "RUB"),
4650
new Amount(0, 0, "RUB"),
51+
new Amount(0, 0, "RUB"),
4752
List.of(),
4853
true
4954
);
@@ -62,6 +67,6 @@ public List<Reel> findFor(String recipientId) {
6267

6368
private Reel from(ReelData data) {
6469
log.debug("Found data", Map.of("data", data));
65-
return new Reel(data, commandSender, repository);
70+
return new Reel(data, commandSender, repository, widgetSender);
6671
}
6772
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alter table reel add step_amount varchar(255) default 'RUB;0;0';

src/test/java/io/github/opendonationassistant/reel/ReelTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import io.github.opendonationassistant.commons.Amount;
77
import io.github.opendonationassistant.events.CompletedPaymentNotification;
8+
import io.github.opendonationassistant.events.widget.WidgetCommandSender;
9+
810
import java.time.Instant;
911
import java.util.List;
1012
import org.junit.jupiter.api.Test;
@@ -15,6 +17,7 @@ public class ReelTest {
1517
public void testSendingCommandWhenTriggerForEachPayment() {
1618
var commandSender = mock(ReelCommandSender.class);
1719
var repository = mock(ReelDataRepository.class);
20+
var configSender = mock(WidgetCommandSender.class);
1821

1922
var reel = new Reel(
2023
new ReelData(
@@ -23,11 +26,13 @@ public void testSendingCommandWhenTriggerForEachPayment() {
2326
"widgetId",
2427
new Amount(0, 0, "RUB"),
2528
new Amount(300, 0, "RUB"),
29+
new Amount(300, 0, "RUB"),
2630
List.of("test1"),
2731
true
2832
),
2933
commandSender,
30-
repository
34+
repository,
35+
configSender
3136
);
3237

3338
var expectedCommand = new ReelCommand();

0 commit comments

Comments
 (0)