working on tag handling in accounting module
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -19,4 +19,6 @@ public interface AccountDb {
|
||||
Transaction save(Transaction transaction);
|
||||
|
||||
Set<String> searchField(long userId, String field, String key);
|
||||
|
||||
Set<String> searchTagsContaining(String key, long accountId);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package de.srsoftware.umbrella.accounting;
|
||||
import static de.srsoftware.tools.Optionals.nullIfEmpty;
|
||||
import static de.srsoftware.umbrella.accounting.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.tagService;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.*;
|
||||
@@ -71,10 +72,18 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case null -> postEntry(user.get(),ex);
|
||||
case SOURCES -> postSearchSources(user.get(),ex);
|
||||
case DESTINATIONS -> postSearchDestinations(user.get(),ex);
|
||||
case PURPOSES -> postSearchPurposes(user.get(),ex);
|
||||
default -> super.doPost(path,ex);
|
||||
case SOURCES -> postSearchSources(user.get(),ex);
|
||||
default -> {
|
||||
try {
|
||||
var accountId = Long.parseLong(head);
|
||||
yield postToAccount(accountId,path,user.get(),ex);
|
||||
} catch (NumberFormatException ignored) {
|
||||
yield super.doPost(path,ex);
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
@@ -82,6 +91,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
}
|
||||
|
||||
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
||||
LOG.log(WARNING,"Missing authorization check in AccountingModule.getAccount(…)!");
|
||||
var account = accountDb.loadAccount(accountId);
|
||||
var transactions = accountDb.loadTransactions(account);
|
||||
var userMap = new HashMap<Long,UmbrellaUser>();
|
||||
@@ -202,6 +212,21 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
return sendContent(ex,searchOptions(user, Field.SOURCE, body(ex)));
|
||||
}
|
||||
|
||||
private boolean postSearchTags(long accountId, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
LOG.log(WARNING,"Missing authorization check in AccountingModule.getAccount(…)!");
|
||||
var key = body(ex);
|
||||
var tags = accountDb.searchTagsContaining(key,accountId);
|
||||
if (tags.size()<10) tags.addAll(tagService().search(key,user));
|
||||
return sendContent(ex,tags);
|
||||
}
|
||||
|
||||
private boolean postToAccount(long accountId, Path path, UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
return switch (path.pop()) {
|
||||
case TAGS -> postSearchTags(accountId,user,ex);
|
||||
case null, default -> super.doPost(path,ex);
|
||||
};
|
||||
}
|
||||
|
||||
public ArrayList<Map<String,?>> searchOptions(UmbrellaUser user, String field, String key){
|
||||
var users = userService().search(key);
|
||||
var destinations = accountDb.searchField(user.id(), field, key);
|
||||
|
||||
@@ -7,6 +7,7 @@ import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.umbrella.accounting.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Translatable.t;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Condition;
|
||||
@@ -20,10 +21,7 @@ import de.srsoftware.umbrella.core.model.Transaction;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class SqliteDb extends BaseDb implements AccountDb {
|
||||
public SqliteDb(Connection connection) {
|
||||
@@ -155,11 +153,21 @@ public class SqliteDb extends BaseDb implements AccountDb {
|
||||
@Override
|
||||
public List<Transaction> loadTransactions(Account account) {
|
||||
try {
|
||||
var list = new ArrayList<Transaction>();
|
||||
var transactions = new HashMap<Long,Transaction>();
|
||||
var rs = select(ALL).from(TABLE_TRANSACTIONS).where(Field.ACCOUNT,equal(account.id())).sort(Field.TIMESTAMP).exec(db);
|
||||
while (rs.next()) list.add(Transaction.of(rs));
|
||||
while (rs.next()) {
|
||||
var transaction = Transaction.of(rs);
|
||||
transactions.put(transaction.id(),transaction);
|
||||
}
|
||||
rs.close();
|
||||
return list;
|
||||
var transactionIds = transactions.keySet().toArray();
|
||||
rs = select(ALL).from(TABLE_TAGS_TRANSACTIONS).leftJoin(Field.TAG_ID,TABLE_TAGS,Field.ID).where(Field.TRANSACTION_ID,in(transactionIds)).exec(db);
|
||||
while (rs.next()) {
|
||||
var transaction = transactions.get(rs.getLong(Field.TRANSACTION_ID));
|
||||
if (transaction != null) transaction.tags().add(rs.getString(Field.TAG));
|
||||
}
|
||||
rs.close();
|
||||
return new ArrayList<>(transactions.values());
|
||||
} catch (SQLException e) {
|
||||
throw failedToLoadMembers(account);
|
||||
}
|
||||
@@ -197,11 +205,10 @@ public class SqliteDb extends BaseDb implements AccountDb {
|
||||
} else { // TODO : implement update
|
||||
throw UmbrellaException.failedToStoreObject(transaction);
|
||||
}
|
||||
saveTags(transaction);
|
||||
return transaction;
|
||||
return saveTags(transaction);
|
||||
}
|
||||
|
||||
private void saveTags(Transaction transaction) {
|
||||
private Transaction saveTags(Transaction transaction) {
|
||||
var remaining = new HashSet<String>(transaction.tags());
|
||||
var existingTags = new HashMap<String,Long>();
|
||||
try {
|
||||
@@ -228,6 +235,7 @@ public class SqliteDb extends BaseDb implements AccountDb {
|
||||
} catch (SQLException e){
|
||||
throw failedToStoreObject(transaction);
|
||||
}
|
||||
return transaction;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -244,4 +252,17 @@ public class SqliteDb extends BaseDb implements AccountDb {
|
||||
throw failedToReadFromTable(field,TABLE_TRANSACTIONS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> searchTagsContaining(String key, long accountId) {
|
||||
try {
|
||||
var tags = new HashSet<String>();
|
||||
var rs = select(ALL).from(TABLE_TRANSACTIONS).leftJoin(Field.ID,TABLE_TAGS_TRANSACTIONS,Field.TRANSACTION_ID).leftJoin(Field.TAG_ID,TABLE_TAGS,Field.ID).where(Field.TAG,like(format("%{0}%",key))).exec(db);
|
||||
while (rs.next()) tags.add(rs.getString(Field.TAG));
|
||||
rs.close();
|
||||
return tags;
|
||||
} catch (SQLException e){
|
||||
throw failedToSearchDb(t(Text.TAGS));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,5 +30,7 @@ public interface TagService {
|
||||
|
||||
String save(String module, long entityId, Collection<Long> userIds, String tag);
|
||||
|
||||
Collection<String> search(String key, UmbrellaUser user);
|
||||
|
||||
void updateId(String module, Object oldId, Object newId);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ public class Path {
|
||||
public static final String STATE = "state";
|
||||
public static final String STOP = "stop";
|
||||
|
||||
public static final String TAGS = "tags";
|
||||
public static final String TAGGED = "tagged";
|
||||
public static final String TOKEN = "token";
|
||||
|
||||
|
||||
@@ -52,7 +52,12 @@ public class IdOrString implements Mappable {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String value(){
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String value(){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
public record Transaction(long id, long accountId, LocalDateTime date, IdOrString source, IdOrString destination, double amount, String purpose, Set<String> tags) implements Mappable {
|
||||
|
||||
|
||||
@@ -35,10 +37,16 @@ public record Transaction(long id, long accountId, LocalDateTime date, IdOrStrin
|
||||
Field.SOURCE, source.toMap(),
|
||||
Field.DESTINATION, destination.toMap(),
|
||||
Field.AMOUNT, amount,
|
||||
Field.PURPOSE, purpose
|
||||
Field.PURPOSE, purpose,
|
||||
Field.TAGS, tags
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format("Transaction ({0} -[{1}]-> {2})",source,amount,destination);
|
||||
}
|
||||
|
||||
public Transaction withId(long id) {
|
||||
return new Transaction(id, accountId, date, source, destination, amount, purpose, new HashSet<>(tags));
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@
|
||||
{/each}
|
||||
<th>{t('other party')}</th>
|
||||
<th>{t('purpose')}</th>
|
||||
<th>{t('tags')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -78,7 +79,7 @@
|
||||
{/if}
|
||||
</td>
|
||||
{/each}
|
||||
<td>
|
||||
<td class="party">
|
||||
{#if !transaction.source.id}
|
||||
← {transaction.source.value}
|
||||
{/if}
|
||||
@@ -86,7 +87,10 @@
|
||||
→ {transaction.destination.value}
|
||||
{/if}
|
||||
</td>
|
||||
<td>{transaction.purpose}</td>
|
||||
<td class="purpose">{transaction.purpose}</td>
|
||||
<td class="tags">
|
||||
{transaction.tags.join(', ')}
|
||||
</td>
|
||||
</tr>
|
||||
{/each}
|
||||
<tr>
|
||||
@@ -109,5 +113,7 @@
|
||||
</table>
|
||||
</fieldset>
|
||||
|
||||
<span class="warn">TODO: Bearbeiten von Umsätzen</span>
|
||||
|
||||
<EntryForm {account} {onSave} />
|
||||
{/if}
|
||||
@@ -29,16 +29,6 @@
|
||||
});
|
||||
let router = useTinyRouter();
|
||||
|
||||
function mapDisplay(object){
|
||||
if (object.display){
|
||||
return object;
|
||||
} else if (object.name) {
|
||||
return {...object, display: object.name};
|
||||
} else {
|
||||
return { display : object }
|
||||
}
|
||||
}
|
||||
|
||||
async function getTerminal(text,url){
|
||||
var res = await post(url,text);
|
||||
if (res.ok){
|
||||
@@ -51,6 +41,11 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function getAccountTags(text){
|
||||
var url = api(`accounting/${entry.account.id}/tags`)
|
||||
return await getTerminal(text,url);
|
||||
}
|
||||
|
||||
async function getDestinations(text){
|
||||
var url = api('accounting/destinations');
|
||||
return await getTerminal(text,url);
|
||||
@@ -66,6 +61,16 @@
|
||||
return await getTerminal(text,url);
|
||||
}
|
||||
|
||||
function mapDisplay(object){
|
||||
if (object.display){
|
||||
return object;
|
||||
} else if (object.name) {
|
||||
return {...object, display: object.name};
|
||||
} else {
|
||||
return { display : object }
|
||||
}
|
||||
}
|
||||
|
||||
async function save(){
|
||||
let data = {
|
||||
...entry,
|
||||
@@ -75,6 +80,10 @@
|
||||
let res = await post(url, data);
|
||||
if (res.ok) {
|
||||
yikes();
|
||||
if (new_account){
|
||||
router.navigate('/accounting');
|
||||
return;
|
||||
}
|
||||
entry.tags = [];
|
||||
onSave();
|
||||
document.getElementById('date-input').focus();
|
||||
@@ -93,8 +102,6 @@
|
||||
}
|
||||
</style>
|
||||
|
||||
<span class="warn">TODO: Tagging von Umsätzen</span>
|
||||
|
||||
<fieldset class="grid2">
|
||||
{#if new_account}
|
||||
<legend>{t('create_new_object',{object:t('account')})}</legend>
|
||||
@@ -134,7 +141,7 @@
|
||||
<Autocomplete bind:candidate={entry.purpose} getCandidates={getPurposes} />
|
||||
|
||||
<span>{t('tags')}</span>
|
||||
<Tags module={null} bind:tags={entry.tags} />
|
||||
<Tags getCandidates={getAccountTags} module={null} bind:tags={entry.tags} />
|
||||
|
||||
<span></span>
|
||||
<span>
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
let {
|
||||
id = null,
|
||||
getCandidates = getCandidateTags,
|
||||
module,
|
||||
tags = $bindable([]),
|
||||
user_list = [],
|
||||
@@ -64,7 +65,7 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
async function getCandidates(input){
|
||||
async function getCandidateTags(input){
|
||||
if (!input || input.length <3) return [];
|
||||
const url = api(`tags/search/${encodeURI(input)}`);
|
||||
const res = await get(url);
|
||||
|
||||
@@ -153,8 +153,13 @@ public class TagModule extends BaseHandler implements TagService {
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> search(String key, UmbrellaUser user) {
|
||||
return tagDb.search(key, user);
|
||||
}
|
||||
|
||||
private boolean searchTags(HttpExchange ex, String head, UmbrellaUser user) throws IOException {
|
||||
return sendContent(ex, tagDb.search(head, user));
|
||||
return sendContent(ex, search(head, user));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user