implemented transaction propagation via event bus
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
description = "Umbrella : Accounting"
|
description = "Umbrella : Accounting"
|
||||||
|
|
||||||
dependencies{
|
dependencies{
|
||||||
|
implementation(project(":bus"))
|
||||||
implementation(project(":core"))
|
implementation(project(":core"))
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,9 @@ import static de.srsoftware.umbrella.core.Util.mapValues;
|
|||||||
import static de.srsoftware.umbrella.core.constants.Path.*;
|
import static de.srsoftware.umbrella.core.constants.Path.*;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidField;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingField;
|
||||||
|
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||||
|
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.CREATE;
|
||||||
|
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.UPDATE;
|
||||||
import static java.lang.System.Logger.Level.WARNING;
|
import static java.lang.System.Logger.Level.WARNING;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
@@ -27,6 +30,9 @@ import java.io.IOException;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import de.srsoftware.umbrella.messagebus.events.Event;
|
||||||
|
import de.srsoftware.umbrella.messagebus.events.TransactionEvent;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@@ -177,6 +183,14 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
|
|
||||||
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
||||||
LOG.log(WARNING,"Missing authorization check in AccountingModule.getAccount(…)!");
|
LOG.log(WARNING,"Missing authorization check in AccountingModule.getAccount(…)!");
|
||||||
|
return sendContent(ex, loadAccount(accountId));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
|
return sendContent(ex,accountDb.listAccounts(user.id()).stream().map(Account::toMap));
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccountData loadAccount(long accountId){
|
||||||
var account = accountDb.loadAccount(accountId);
|
var account = accountDb.loadAccount(accountId);
|
||||||
var transactions = accountDb.loadTransactions(account);
|
var transactions = accountDb.loadTransactions(account);
|
||||||
var userMap = new HashMap<Long,UmbrellaUser>();
|
var userMap = new HashMap<Long,UmbrellaUser>();
|
||||||
@@ -193,20 +207,9 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
if (!userMap.containsKey(userId)) userMap.put(destination.id(),userService().loadUser(userId));
|
if (!userMap.containsKey(userId)) userMap.put(destination.id(),userService().loadUser(userId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return AccountData.of(account, transactions, userMap);
|
||||||
return sendContent(ex, Map.of(
|
|
||||||
Field.ACCOUNT,account.toMap(),
|
|
||||||
Field.TRANSACTIONS,transactions.stream().map(Transaction::toMap).toList(),
|
|
||||||
Field.USER_LIST,mapValues(userMap)
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
|
||||||
return sendContent(ex,accountDb.listAccounts(user.id()).stream().map(Account::toMap));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static boolean noNumbers(String s){
|
private static boolean noNumbers(String s){
|
||||||
try {
|
try {
|
||||||
Long.parseLong(s);
|
Long.parseLong(s);
|
||||||
@@ -226,7 +229,9 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
if (json.has(Field.PURPOSE)) transaction.purpose(json.getString(Field.PURPOSE));
|
if (json.has(Field.PURPOSE)) transaction.purpose(json.getString(Field.PURPOSE));
|
||||||
if (json.has(Field.SOURCE)) transaction.source(IdOrString.of(json.getString(Field.SOURCE)));
|
if (json.has(Field.SOURCE)) transaction.source(IdOrString.of(json.getString(Field.SOURCE)));
|
||||||
if (json.has(Field.TAG)) transaction.tags().add(json.getString(Field.TAG));
|
if (json.has(Field.TAG)) transaction.tags().add(json.getString(Field.TAG));
|
||||||
return sendContent(ex,accountDb.save(transaction));
|
var patched = accountDb.save(transaction);
|
||||||
|
messageBus().dispatch(new TransactionEvent(user,patched,UPDATE));
|
||||||
|
return sendContent(ex,patched);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean postEntry(UmbrellaUser user, HttpExchange ex) throws IOException {
|
private boolean postEntry(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
@@ -291,7 +296,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
|
|
||||||
|
|
||||||
var transaction = accountDb.save(new Transaction(0,accountId,dateTime,source,destination,amount.doubleValue(),purpose,tags));
|
var transaction = accountDb.save(new Transaction(0,accountId,dateTime,source,destination,amount.doubleValue(),purpose,tags));
|
||||||
|
messageBus().dispatch(new TransactionEvent(user,transaction, CREATE));
|
||||||
return sendContent(ex,newAccount != null ? newAccount : transaction);
|
return sendContent(ex,newAccount != null ? newAccount : transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import static de.srsoftware.umbrella.accounting.Constants.*;
|
|||||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||||
|
import static de.srsoftware.umbrella.messagebus.MessageBus.messageBus;
|
||||||
|
import static de.srsoftware.umbrella.messagebus.events.Event.EventType.UPDATE;
|
||||||
import static java.text.MessageFormat.format;
|
import static java.text.MessageFormat.format;
|
||||||
|
|
||||||
import de.srsoftware.tools.jdbc.Condition;
|
import de.srsoftware.tools.jdbc.Condition;
|
||||||
@@ -19,6 +21,8 @@ import de.srsoftware.umbrella.core.constants.Field;
|
|||||||
import de.srsoftware.umbrella.core.constants.Text;
|
import de.srsoftware.umbrella.core.constants.Text;
|
||||||
import de.srsoftware.umbrella.core.model.Account;
|
import de.srsoftware.umbrella.core.model.Account;
|
||||||
import de.srsoftware.umbrella.core.model.Transaction;
|
import de.srsoftware.umbrella.core.model.Transaction;
|
||||||
|
import de.srsoftware.umbrella.messagebus.events.TransactionEvent;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public class MessageApi extends BaseHandler{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendEvent(PrintWriter out, Event event) {
|
private void sendEvent(PrintWriter out, Event<?> event) {
|
||||||
if (event == null) return;
|
if (event == null) return;
|
||||||
out.print("event: ");
|
out.print("event: ");
|
||||||
out.println(event.eventType());
|
out.println(event.eventType());
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package de.srsoftware.umbrella.messagebus.events;
|
||||||
|
|
||||||
|
import de.srsoftware.umbrella.core.ModuleRegistry;
|
||||||
|
import de.srsoftware.umbrella.core.constants.Module;
|
||||||
|
import de.srsoftware.umbrella.core.constants.Text;
|
||||||
|
import de.srsoftware.umbrella.core.model.Transaction;
|
||||||
|
import de.srsoftware.umbrella.core.model.Translatable;
|
||||||
|
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||||
|
import de.srsoftware.umbrella.core.model.UnTranslatable;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static de.srsoftware.umbrella.core.ModuleRegistry.accountingService;
|
||||||
|
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||||
|
|
||||||
|
public class TransactionEvent extends Event<Transaction> {
|
||||||
|
private Collection<UmbrellaUser> audience;
|
||||||
|
|
||||||
|
public TransactionEvent(UmbrellaUser initiator, Transaction transaction, EventType type) {
|
||||||
|
super(initiator, Module.ACCOUNTING, transaction, type);
|
||||||
|
audience = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<UmbrellaUser> audience() {
|
||||||
|
if (audience == null) audience = accountingService().loadAccount(payload().accountId()).userMap().values();
|
||||||
|
return audience;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Translatable describe() {
|
||||||
|
return new UnTranslatable(payload().purpose());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Translatable subject() {
|
||||||
|
return switch (eventType()){
|
||||||
|
case CREATE -> t("user_created_entity",initiator().name(), Text.TRANSACTION);
|
||||||
|
case UPDATE -> t("user_updated_entity",initiator().name(), Text.TRANSACTION);
|
||||||
|
case null, default -> t("TODO"); // TODO
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,6 +53,10 @@ public class ModuleRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AccountingService accountingService() {
|
||||||
|
return singleton.accountingService;
|
||||||
|
}
|
||||||
|
|
||||||
public static BookmarkService bookmarkService(){
|
public static BookmarkService bookmarkService(){
|
||||||
return singleton.bookmarkService;
|
return singleton.bookmarkService;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,33 @@
|
|||||||
/* © SRSoftware 2025 */
|
/* © SRSoftware 2025 */
|
||||||
package de.srsoftware.umbrella.core.api;
|
package de.srsoftware.umbrella.core.api;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Mappable;
|
||||||
|
import de.srsoftware.umbrella.core.constants.Field;
|
||||||
|
import de.srsoftware.umbrella.core.model.Account;
|
||||||
|
import de.srsoftware.umbrella.core.model.Transaction;
|
||||||
|
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||||
|
|
||||||
public interface AccountingService {
|
public interface AccountingService {
|
||||||
|
public record AccountData(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) implements Mappable {
|
||||||
|
public static AccountData of(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) {
|
||||||
|
return new AccountData(account, transactions, userMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> toMap() {
|
||||||
|
return Map.of(
|
||||||
|
Field.ACCOUNT,account.toMap(),
|
||||||
|
Field.TRANSACTIONS,transactions.stream().map(Transaction::toMap).toList(),
|
||||||
|
Field.USER_LIST,mapValues(userMap)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AccountData loadAccount(long accountId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { api, get } from '../../urls.svelte';
|
import { api, eventStream, get } from '../../urls.svelte';
|
||||||
import { error, yikes } from '../../warn.svelte';
|
import { error, yikes } from '../../warn.svelte';
|
||||||
import { t } from '../../translations.svelte';
|
import { t } from '../../translations.svelte';
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
let { id } = $props();
|
let { id } = $props();
|
||||||
let account = $state(null);
|
let account = $state(null);
|
||||||
|
let eventSource = null;
|
||||||
let filter = $state([]);
|
let filter = $state([]);
|
||||||
let transactions = $state([]);
|
let transactions = $state([]);
|
||||||
let filtered = $derived(transactions.filter(t => checker(t.tags,filter)));
|
let filtered = $derived(transactions.filter(t => checker(t.tags,filter)));
|
||||||
@@ -27,7 +28,6 @@
|
|||||||
if (!transaction.destination.id) sums[0] += transaction.amount;
|
if (!transaction.destination.id) sums[0] += transaction.amount;
|
||||||
if (!transaction.source.id) sums[0] -= transaction.amount;
|
if (!transaction.source.id) sums[0] -= transaction.amount;
|
||||||
}
|
}
|
||||||
window.setTimeout(scrollToBottom,100);
|
|
||||||
return sums;
|
return sums;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,6 +50,34 @@
|
|||||||
filter = filter.filter(x => x != tag.toLowerCase());
|
filter = filter.filter(x => x != tag.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleCreateEvent(evt){
|
||||||
|
const event_data = JSON.parse(evt.data);
|
||||||
|
const new_transaction = event_data.transaction;
|
||||||
|
transactions.push(new_transaction);
|
||||||
|
window.setTimeout(scrollToBottom,100);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEvent(evt,method){
|
||||||
|
let event_data = JSON.parse(evt.data);
|
||||||
|
console.log({method, event_data});
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleDeleteEvent(evt){
|
||||||
|
handleEvent(evt,'delete');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleUpdateEvent(evt){
|
||||||
|
const event_data = JSON.parse(evt.data);
|
||||||
|
const updated_transaction = event_data.transaction;
|
||||||
|
for (var idx in transactions){
|
||||||
|
if (transactions[idx].id == updated_transaction.id) {
|
||||||
|
updated_transaction.tags = transactions[idx].tags;
|
||||||
|
transactions[idx] = updated_transaction;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function load(){
|
async function load(){
|
||||||
let url = api(`accounting/${id}`);
|
let url = api(`accounting/${id}`);
|
||||||
let res = await get(url);
|
let res = await get(url);
|
||||||
@@ -59,11 +87,15 @@
|
|||||||
transactions = json.transactions;
|
transactions = json.transactions;
|
||||||
users = json.user_list;
|
users = json.user_list;
|
||||||
account = json.account;
|
account = json.account;
|
||||||
|
try {
|
||||||
|
eventSource = eventStream(handleCreateEvent,handleUpdateEvent,handleDeleteEvent);
|
||||||
|
} catch (ignored) {}
|
||||||
|
window.setTimeout(scrollToBottom,100);
|
||||||
} else error(res);
|
} else error(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSave(){
|
function onSave(){
|
||||||
load();
|
// load();
|
||||||
}
|
}
|
||||||
|
|
||||||
function scrollToBottom(){
|
function scrollToBottom(){
|
||||||
|
|||||||
Reference in New Issue
Block a user