-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpenseEventsListener.java
97 lines (85 loc) · 3.5 KB
/
ExpenseEventsListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package io.myfinbox.spendingplan.adapter.messaging;
import io.myfinbox.expense.ExpenseCreated;
import io.myfinbox.expense.ExpenseDeleted;
import io.myfinbox.expense.ExpenseUpdated;
import io.myfinbox.spendingplan.application.ExpenseRecordTrackerUseCase;
import io.myfinbox.spendingplan.application.ExpenseRecordTrackerUseCase.ExpenseModificationRecord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.stereotype.Component;
/**
* Listens for expense-related events and delegates the handling to the appropriate use case.
*/
@Slf4j
@RequiredArgsConstructor
@Component(value = "planExpenseEventsListener")
class ExpenseEventsListener {
private final ExpenseRecordTrackerUseCase expenseRecordTrackerUseCase;
/**
* Handles the ExpenseCreated event by recording it.
*
* @param event The ExpenseCreated event to handle.
*/
@ApplicationModuleListener
public void on(ExpenseCreated event) {
log.debug("[Plan] Received ExpenseCreated event: {}", event);
// Record the created expense
var expenseRecord = expenseRecordTrackerUseCase.recordCreated(ExpenseModificationRecord.builder()
.expenseId(event.expenseId())
.accountId(event.accountId())
.categoryId(event.categoryId())
.amount(event.amount())
.paymentType(event.paymentType())
.expenseDate(event.expenseDate())
.categoryName(event.categoryName())
.build());
if (expenseRecord.isEmpty()) {
log.debug("[Plan] ExpenseCreated event: {} skipped", event);
}
}
/**
* Handles the ExpenseUpdated event by recording it.
*
* @param event The ExpenseUpdated event to handle.
*/
@ApplicationModuleListener
public void on(ExpenseUpdated event) {
log.debug("[Plan] Received ExpenseUpdated event: {}", event);
// Record the updated expense
var expenseRecord = expenseRecordTrackerUseCase.recordUpdated(ExpenseModificationRecord.builder()
.expenseId(event.expenseId())
.accountId(event.accountId())
.categoryId(event.categoryId())
.amount(event.amount())
.paymentType(event.paymentType())
.expenseDate(event.expenseDate())
.categoryName(event.categoryName())
.build());
if (expenseRecord.isEmpty()) {
log.debug("[Plan] ExpenseUpdated event: {} skipped", event);
}
}
/**
* Handles the ExpenseDeleted event by recording it.
*
* @param event The ExpenseDeleted event to handle.
*/
@ApplicationModuleListener
public void on(ExpenseDeleted event) {
log.debug("[Plan] Received ExpenseDeleted event: {}", event);
// Record the deleted expense
var expenseRecord = expenseRecordTrackerUseCase.recordDeleted(ExpenseModificationRecord.builder()
.expenseId(event.expenseId())
.accountId(event.accountId())
.categoryId(event.categoryId())
.amount(event.amount())
.paymentType(event.paymentType())
.expenseDate(event.expenseDate())
.categoryName(event.categoryName())
.build());
if (expenseRecord.isEmpty()) {
log.debug("[Plan] ExpenseDeleted event: {} skipped", event);
}
}
}