New code for transaction processing and frontend

This commit is contained in:
James Cole
2023-08-22 08:25:06 +02:00
parent f67ff98d78
commit 2404f5299c
13 changed files with 380 additions and 51 deletions

View File

@@ -33,6 +33,8 @@ let chart = null;
let chartData = null;
let afterPromises = false;
const CHART_CACHE_KEY = 'dashboard-accounts-chart';
const ACCOUNTS_CACHE_KEY = 'dashboard-accounts-data';
export default () => ({
loading: false,
loadingAccounts: false,
@@ -44,12 +46,28 @@ export default () => ({
setVariable('autoConversion', this.autoConversion);
},
getFreshData() {
const dashboard = new Dashboard();
dashboard.dashboard(new Date(window.store.get('start')), new Date(window.store.get('end')), null).then((response) => {
this.chartData = response.data;
this.drawChart(this.generateOptions(this.chartData));
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(CHART_CACHE_KEY);
if (cacheValid && typeof cachedData !== 'undefined') {
let options = window.store.get(CHART_CACHE_KEY);
this.drawChart(options);
this.loading = false;
});
console.log('Chart from cache');
}
if (!cacheValid || typeof cachedData === 'undefined') {
const dashboard = new Dashboard();
dashboard.dashboard(new Date(window.store.get('start')), new Date(window.store.get('end')), null).then((response) => {
this.chartData = response.data;
// cache generated options:
let options = this.generateOptions(this.chartData);
window.store.set(CHART_CACHE_KEY, options);
this.drawChart(options);
this.loading = false;
console.log('Chart FRESH');
});
}
},
generateOptions(data) {
currencies = [];
@@ -145,6 +163,15 @@ export default () => ({
this.loadingAccounts = false;
return;
}
const cacheValid = window.store.get('cacheValid');
let cachedData = window.store.get(ACCOUNTS_CACHE_KEY);
if (cacheValid && typeof cachedData !== 'undefined') {
this.accountList = cachedData;
this.loadingAccounts = false;
return;
}
// console.log('loadAccounts continue!');
const max = 10;
let totalAccounts = 0;
@@ -180,7 +207,10 @@ export default () => ({
group.transactions.push({
description: currentTransaction.description,
id: current.id,
type: currentTransaction.type,
amount_raw: parseFloat(currentTransaction.amount),
amount: formatMoney(currentTransaction.amount, currentTransaction.currency_code),
native_amount_raw: parseFloat(currentTransaction.native_amount),
native_amount: formatMoney(currentTransaction.native_amount, currentTransaction.native_code),
});
}
@@ -190,7 +220,9 @@ export default () => ({
accounts.push({
name: parent.attributes.name,
id: parent.id,
balance_raw: parseFloat(parent.attributes.current_balance),
balance: formatMoney(parent.attributes.current_balance, parent.attributes.currency_code),
native_balance_raw: parseFloat(parent.attributes.native_current_balance),
native_balance: formatMoney(parent.attributes.native_current_balance, parent.attributes.native_code),
groups: groups,
});
@@ -198,6 +230,7 @@ export default () => ({
if (count === totalAccounts) {
this.accountList = accounts;
this.loadingAccounts = false;
window.store.set(ACCOUNTS_CACHE_KEY, accounts);
}
});
});

View File

@@ -21,16 +21,66 @@
import '../../boot/bootstrap.js';
import dates from '../../pages/shared/dates.js';
import {createEmptySplit} from "./shared/create-empty-split.js";
import {parseFromEntries} from "./shared/parse-from-entries.js";
import formatMoney from "../../util/format-money.js";
//import Autocomplete from "bootstrap5-autocomplete";
import Post from "../../api/v2/model/transaction/post.js";
let transactions = function () {
return {
count: 0,
totalAmount: 0,
entries: [],
// error and success messages:
showError: false,
showSuccess: false,
init() {
this.entries.push(createEmptySplit());
const opts = {
onSelectItem: console.log,
};
let src = [];
for (let i = 0; i < 50; i++) {
src.push({
title: "Option " + i,
id: "opt" + i,
data: {
key: i,
},
});
}
// for each thing, make autocomplete?
this.addSplit();
console.log('Ik ben init hoera');
// let element = document.getElementById('source_0');
// new Autocomplete(element, {
// items: src,
// valueField: "id",
// labelField: "title",
// highlightTyped: true,
// onSelectItem: console.log,
// });
},
submitTransaction() {
let transactions = parseFromEntries(this.entries);
let submission = {
group_title: null,
fire_webhooks: false,
apply_rules: false,
transactions: transactions
};
let poster = new Post();
console.log(submission);
poster.post(submission).then((response) => {
console.log(response);
}).catch((error) => {
console.error(error);
});
},
addSplit() {
this.entries.push(createEmptySplit());

View File

@@ -19,6 +19,8 @@
*/
import format from "date-fns/format";
function getAccount() {
return {
id: '',
@@ -27,10 +29,13 @@ function getAccount() {
}
export function createEmptySplit() {
let now = new Date();
let formatted = format(now, 'yyyy-MM-dd HH:mm');
return {
description: 'I am descr',
description: 'OK then',
amount: '',
source_account: getAccount(),
destination_account: getAccount(),
date: formatted
};
}

View File

@@ -0,0 +1,47 @@
/*
* parse-from-entries.js
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
*
* @param entries
*/
export function parseFromEntries(entries) {
let returnArray = [];
for (let i in entries) {
if (entries.hasOwnProperty(i)) {
const entry = entries[i];
let current = {};
// fields for transaction
current.description = entry.description;
current.source_name = entry.source_account.name;
current.destination_name = entry.destination_account.name;
current.amount = entry.amount;
current.date = entry.date;
// TODO transaction type is hard coded:
current.type = 'withdrawal';
returnArray.push(current);
}
}
return returnArray;
}