everything working without error codes

This commit is contained in:
Makussu 2024-06-05 18:07:08 +02:00
parent 542ea2fdf1
commit db5c4dfdca
3 changed files with 134 additions and 57 deletions

View File

@ -2,71 +2,144 @@ const express = require("express");
const router = express.Router(); const router = express.Router();
const db = require("../dependencies"); const db = require("../dependencies");
const { update } = require("./reset");
// Fix if balance is null
function fix_balance_is_null(acc) {
if (acc["localBalance"] == null) {
acc["localBalance"] = 0;
}
if (acc["balance"] == null) {
acc["balance"] = 0;
}
}
// Get subaccounts from database, recursivly calls return_account
// Fuck IO we ball
function get_subaccounts(acc) {
var base_query = `SELECT number, name, qualifiedName, description, type, tf.balance, te.localBalance, parentAccount FROM accounts
LEFT JOIN (SELECT SUM(amount) as localBalance, account_name FROM transaction_entries GROUP BY account_name) te ON te.account_name = qualifiedName
LEFT JOIN (SELECT SUM(amount) as balance, account_name FROM transaction_entries GROUP BY account_name) tf ON tf.account_name = qualifiedName
WHERE parentAccount = ?`;
var subs = [];
const accs = db.prepare(base_query).all(acc["qualifiedName"]);
for (let i = 0; i < accs.length; i++) {
var one_sub = return_account(accs[i]);
fix_balance_is_null(one_sub);
acc["balance"] = acc["balance"] + one_sub["balance"];
subs.push(one_sub);
}
return subs;
}
// Get important account info out of account, recursivly calls get_subaccounts
function return_account(acc) {
test = {
number: acc["number"],
name: acc["name"],
qualifiedName: acc["qualifiedName"],
description: acc["description"],
type: acc["type"],
localBalance: acc["localBalance"],
subaccounts: get_subaccounts(acc),
balance: acc["balance"],
};
return test;
}
router.get("/", (req, res) => { router.get("/", (req, res) => {
const accs = db.prepare(`SELECT number, name, qualifiedName, description, type, localBalance, te.balance FROM accounts var base_query = `SELECT number, name, qualifiedName, description, type, tf.balance, te.localBalance, parentAccount FROM accounts
JOIN (SELECT SUM(amount) as balance, account_name FROM transaction_entries GROUP BY account_name) te ON te.account_name = name`).all(); LEFT JOIN (SELECT SUM(amount) as localBalance, account_name FROM transaction_entries GROUP BY account_name) te ON te.account_name = qualifiedName
new_accs = []; LEFT JOIN (SELECT SUM(amount) as balance, account_name FROM transaction_entries GROUP BY account_name) tf ON tf.account_name = qualifiedName`;
for (let i=0; i < accs.length; i++) { const accs = db.prepare(base_query).all();
accs[i]["subaccounts"] = [];
var real_accs = [];
for (let i = 0; i < accs.length; i++) {
fix_balance_is_null(accs[i]);
if (accs[i]["parentAccount"] == null) {
real_accs.push(return_account(accs[i]));
}
} }
res.send(accs); res.send(real_accs);
}); });
router.post("/", (req, res) => { router.post("/", (req, res) => {
const number = req.body.number; const number = req.body.number;
const name = req.body.name; const name = req.body.name;
const des = req.body.description; const des = req.body.description;
const type = req.body.type; const type = req.body.type;
const qualifiedName = req.body.parentAccount + ":" + name var qualifiedName;
const new_acc = db.prepare(`INSERT INTO accounts (number, name, qualifiedName, description, type, balance, localBalance) VALUES (?, ?, ? ,?, ?, 0, 0)`).run(number ,name, qualifiedName,des, type) if (req.body.parentAccount =="--- Wurzel Konto ---") {
res.status(204).send() qualifiedName = name;
} else {
qualifiedName = req.body.parentAccount + ":" + name;
}
const new_acc = db
.prepare(
`INSERT INTO accounts (number, name, qualifiedName, description, type, parentAccount) VALUES (?, ?, ? ,?, ?, ?)`,
)
.run(number, name, qualifiedName, des, type, req.body.parentAccount);
res.status(204).send();
for (res of update.values()) { for (res of update.values()) {
// res.write("data: update\n\n") // res.write("data: update\n\n")
res.write('event: update\n'); res.write("event: update\n");
res.write('data: {}\n\n'); res.write("data: {}\n\n");
} }
}); });
// TODO add correct balance
router.get("/:account", (req, res) => { router.get("/:account", (req, res) => {
const acc = db var base_query = `SELECT number, name, qualifiedName, description, type, tf.balance, te.localBalance FROM accounts
.prepare("SELECT * FROM accounts WHERE name = ?") LEFT JOIN (SELECT SUM(amount) as localBalance, account_name FROM transaction_entries GROUP BY account_name) te ON te.account_name = qualifiedName
.get(req.params.account); LEFT JOIN (SELECT SUM(amount) as balance, account_name FROM transaction_entries GROUP BY account_name) tf ON tf.account_name = qualifiedName
WHERE qualifiedName = ?`;
const acc = db.prepare(base_query).get(req.params.account);
console.log(acc)
if (acc == undefined) { if (acc == undefined) {
res.status(404).send({ res.status(404).send({
code: 404, code: 404,
message: "Account not found", message: "Account not found",
additionalPropl: {} additionalPropl: {},
}); });
} else { } else {
entries = db.prepare(` fix_balance_is_null(acc);
SELECT
transaction_entries.transaction_id as id,
offsetAccounts,
transaction_entries.amount,
transaction_entries.label,
postingDate,
valueDate,
title
from transaction_entries
JOIN transactions ON transaction_entries.transaction_id = transactions.transaction_id
JOIN (SELECT account_name as offsetAccounts, transaction_id FROM transaction_entries) te ON te.transaction_id = id AND offsetAccounts IS NOT transaction_entries.account_name
WHERE transaction_entries.account_name = ?`).all(req.params.account)
acc['entries'] = entries
res.send(acc);
var real_acc;
fix_balance_is_null(acc);
if (acc["parentAccount"] == null) {
real_acc = return_account(acc);
}
acc["balance"] = real_acc["balance"]
entries = db
.prepare(
`
SELECT
transaction_entries.transaction_id as id,
offsetAccounts,
transaction_entries.amount,
transaction_entries.label,
postingDate,
valueDate,
title
from transaction_entries
JOIN transactions ON transaction_entries.transaction_id = transactions.transaction_id
JOIN (SELECT account_name as offsetAccounts, transaction_id FROM transaction_entries) te ON te.transaction_id = id AND offsetAccounts IS NOT transaction_entries.account_name
WHERE transaction_entries.account_name = ?`,
)
.all(req.params.account);
acc["entries"] = entries;
res.send(acc);
} }
}); });
router.put("/:account", (req, res) => { router.put("/:account", (req, res) => {
const oldname = req.params.account; const oldname = req.params.account;
const name = req.body.name; const name = req.body.name;
@ -74,38 +147,39 @@ router.put("/:account", (req, res) => {
const type = req.body.type; const type = req.body.type;
const qualifiedName = req.body.parentAccount + ":" + name; const qualifiedName = req.body.parentAccount + ":" + name;
const acc = db.prepare(`UPDATE accounts SET description = ?, name = ?, qualifiedName = ?, type = ? WHERE name=?`).run(description, name, qualifiedName, type, oldname); const acc = db
.prepare(
`UPDATE accounts SET description = ?, name = ?, qualifiedName = ?, type = ? WHERE name=?`,
)
.run(description, name, qualifiedName, type, oldname);
if (acc == undefined) { if (acc == undefined) {
res.status(404).send({ res.status(404).send({
code: 404, code: 404,
message: "Account not found", message: "Account not found",
additionalPropl: {} additionalPropl: {},
}) });
} else { } else {
res.status(204).send() res.status(204).send();
for (res of update.values()) { for (res of update.values()) {
// res.write("data: update\n\n") // res.write("data: update\n\n")
res.write('event: update\n'); res.write("event: update\n");
res.write('data: {}\n\n'); res.write("data: {}\n\n");
} }
} }
}); });
router.delete("/:account", (req, res) => { router.delete("/:account", (req, res) => {
const acc = db.prepare("DELETE FROM accounts WHERE name = ? RETURNING *").run(req.params.account) const acc = db
.prepare("DELETE FROM accounts WHERE name = ? RETURNING *")
.run(req.params.account);
res.status(200).send() res.status(200).send();
for (res of update.values()) { for (res of update.values()) {
// res.write("data: update\n\n") // res.write("data: update\n\n")
res.write('event: update\n'); res.write("event: update\n");
res.write('data: {}\n\n'); res.write("data: {}\n\n");
} }
}); });
module.exports = router; module.exports = router;

View File

@ -1,2 +1,6 @@
INSERT INTO accounts (name, qualifiedName, description, type, balance, localBalance,) INSERT INTO accounts (number, name, qualifiedName, description, type)
VALUES ('meins', 'Aktiva:meins', 'Mein Geld lol', 'normal', 0.0, 0.0) VALUES (1000, 'Aktiva', 'Aktiva', 'Mein Geld lol', 'meta');
INSERT INTO accounts (number, name, qualifiedName, description, type, parentAccount)
VALUES (1001, 'Bargeld', 'Aktiva:Bargeld', 'Mein Geld lol', 'default', 'Aktiva');
INSERT INTO accounts (number, name, qualifiedName, description, type)
VALUES (1002, 'Passiva', 'Passiva', 'Mein Geld lol', 'default');

View File

@ -2,10 +2,9 @@ CREATE TABLE accounts (
number integer NOT NULL, number integer NOT NULL,
name text NOT NULL, name text NOT NULL,
qualifiedName text NOT NULL UNIQUE, qualifiedName text NOT NULL UNIQUE,
parentAccount text,
description text NOT NULL, description text NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
balance float NOT NULL,
localBalance float NOT NULL
); );
CREATE TABLE transactions ( CREATE TABLE transactions (
@ -20,6 +19,6 @@ CREATE TABLE transaction_entries (
account_name text NOT NULL, account_name text NOT NULL,
amount float, amount float,
label text, label text,
FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) FOREIGN KEY (account_name) REFERENCES account (account_qualifiedName) PRIMARY KEY (transaction_id, account_id) FOREIGN KEY (transaction_id) REFERENCES transactions (transaction_id) FOREIGN KEY (account_name) REFERENCES accounts (qualifiedName) PRIMARY KEY (transaction_id, account_name)
); );