From 489b05886bd123e4da8f7d9185d4bc44eea3caa1 Mon Sep 17 00:00:00 2001 From: theenoro Date: Thu, 21 Dec 2023 10:05:39 +0100 Subject: [PATCH] 2023.12.21.10.05 --- data/database.sqlite | Bin 40960 -> 40960 bytes .../helper/getFirstLastDayOfMonth.ts | 7 + src/controller/helper/index.ts | 1 + src/controller/models/MonthlyReport.ts | 10 +- src/controller/router/reports.ts | 0 src/controller/router/transactions.ts | 211 +++++++----------- .../router/transactions/addTransaction.ts | 43 ++++ .../router/transactions/addTransfer.ts | 57 +++++ src/controller/router/transactions/index.ts | 5 + .../router/transactions/listTransactions.ts | 34 +++ views/index.pug | 18 +- views/transactions/modifyTransaction.pug | 39 ++++ 12 files changed, 283 insertions(+), 142 deletions(-) create mode 100644 src/controller/helper/getFirstLastDayOfMonth.ts create mode 100644 src/controller/helper/index.ts create mode 100644 src/controller/router/reports.ts create mode 100644 src/controller/router/transactions/addTransaction.ts create mode 100644 src/controller/router/transactions/addTransfer.ts create mode 100644 src/controller/router/transactions/index.ts create mode 100644 src/controller/router/transactions/listTransactions.ts create mode 100644 views/transactions/modifyTransaction.pug diff --git a/data/database.sqlite b/data/database.sqlite index df90da9af50d695dda85efa3ceae3c9296b0596d..7882ebb2a5a46dce00f6910de4385ca556ee4b6e 100644 GIT binary patch delta 1719 zcma)+&ui0Q7{`-twN2Y3Q!6sp+6=7wp^a~TC&kM|g@9{ODrgZZXa_p%j}6hoLhDsT zloz}R{sTLWfHz?Wz3DDq2BJ3wZ(b(r!5^tj-lSQ5wa`QI{r2;Hp67k~b~hv4&B&7@ z2gV-~1d(vQ?uY#H^rXl+Z~3LKG`mYs(QSfRW473L^t06a07XgW60xm-YqEryXf?#k#)$Zl01>bKdBF68i&|1s~ z?2>`AO9p(qI3KW!3eGMnkSk3G>;kqJL4?bt*?=A3dNlxmuSzq%UDt87J1S(##{zZ@ zSG%q$@LKu!oIhF>SB$R0^&z;dVFshin%yg#Yy!~@2&{C{e>C87E$fFHI(Be6+^8K5 z{91*Abui`ARMqa)=!x_W!9Hd)%sQjfZ|IAuPpR9<9ee-8hh%4!VhEF<$jDUOGzpUM zmP-VjAQyq^>?{w^ag6=S)bF?Ijn(=dpRDshlR~{C%8(jci4Q`_Y^I+|p;UjR72a8j zH7k3?ZKnUMhv>s@Pi@Ekz$r3|VGA<+W=#d)f{cSBb!DGd`QPEdSvbqe_;BY12JWlv z=)Wrmn%J+rgT}IV=)tl0YJ1#;hYotkt$2mL4jpsY)frr8-WG)Aff{L!xtWugY02Kd z@${|6jpiM+Swl=@auYj&+fU&El3S5rm!lU8pqunNu1n+^)nvzqw8;O6MEBnoE)uy$ PMFOPK1YwVLf8>4xaI9)# delta 1625 zcmai!O=}ZD7{_1i_mm2*ugWPId{|Ng#)r|8Jj{|31&|p9uRW!oln% zLFYJbiu{8guN>Y%d2*{h{v~I+oFsNRrK6l?zR0iBUsLyzqS#G*a4$~pi0(I05u}oo zxLPRiFD?8h@g@uG5Pqe3-~yBjyL4H?#$HtD=P!a0cJoUfV3h+ zMd;+Cs@q%GC{o^5fo8|@zKVA%g$Ryx7LIiMxv~<$A!OkY;(T?H;+E<-S}g9Xc(Zyl zf?Ey?w;X(2U5?-;(;CynN^L2E8_Z%14L_{ixD{Hf16DCQep_2gQW~FEX43SaI>>N) zwQ`0ABfi`5rFtsftmh)Mgb{Bd^wD7$mk*lstP&l^LuPKH}` zbj(*Tmpng$GoAFwtz zsB5_XVs { + let startDate = moment(date).startOf('month'); + let endDate = moment(date).endOf('month'); + return { startDate, endDate } +} \ No newline at end of file diff --git a/src/controller/helper/index.ts b/src/controller/helper/index.ts new file mode 100644 index 0000000..16ff0ec --- /dev/null +++ b/src/controller/helper/index.ts @@ -0,0 +1 @@ +export { getFirstLastDayOfMonth } from "./getFirstLastDayOfMonth"; diff --git a/src/controller/models/MonthlyReport.ts b/src/controller/models/MonthlyReport.ts index 662ca9f..b0941e7 100644 --- a/src/controller/models/MonthlyReport.ts +++ b/src/controller/models/MonthlyReport.ts @@ -3,6 +3,7 @@ import { Sequelize, DataTypes, Model, Optional, Op, fn, col } from 'sequelize'; import moment from 'moment'; import { Transaction } from './Transaction'; // Assuming you have a Transaction model import { Account } from './Account'; +import { getFirstLastDayOfMonth } from '../helper'; interface MonthlyReportAttributes { MonthlyReportID: number; @@ -66,10 +67,9 @@ class MonthlyReport extends Model total + transaction.Amount, 0); -console.log(totalAmount) + console.log(totalAmount) // Create a MonthlyReport entry with the generated data await MonthlyReport.create({ Month: month, diff --git a/src/controller/router/reports.ts b/src/controller/router/reports.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/router/transactions.ts b/src/controller/router/transactions.ts index 8c22674..20e200a 100644 --- a/src/controller/router/transactions.ts +++ b/src/controller/router/transactions.ts @@ -3,6 +3,10 @@ import { Account, Category, Transaction } from "../models"; import moment from "moment"; import { convertDateArray } from '../helper/dateConversion'; + +import { addTransaction, addTransfer, listTransactions } from './transactions/index'; +import { Op } from 'sequelize'; + export class TransactionRouter { static routes() { let router = express.Router(); @@ -12,149 +16,96 @@ export class TransactionRouter { }) - router.post('/addTransaction', async (req, res, next) => { - console.log(req.body) + router.post('/addTransaction', addTransaction.POST) + router.get('/addTransaction', addTransaction.GET) + + + router.post('/addTransfer', addTransfer.POST) + router.get('/addTransfer', addTransfer.GET) + + router.get('/modifyTransfer', async (req, res) => { - try { - /* - if( - req.body.date == undefined - || req.body.Type == undefined - || req.body.CategoryID == undefined - || req.body.AccountID == undefined - ){throw "ERROR"} - */ - let date = moment(req.body.date).set({ - hour: req.body.time.split(":")[0], - minute:req.body.time.split(":")[1] - }) - console.log(date.toDate()) - await Transaction.create({ - Amount: req.body.amount, - Date: date.toDate(), - Type: req.body.type, - //@ts-ignore - UserID: 1, // Associate the transaction with the user - //@ts-ignore - CategoryID: req.body.category, - AccountID: req.body.account - }) - } catch (e) { - console.log(e) - } - res.redirect("/transactions"); - //next(); - }) - router.post('/addTransfer', async (req, res, next) => { - console.log(req.body) - try { - /* - if( - req.body.date == undefined - || req.body.Type == undefined - || req.body.CategoryID == undefined - || req.body.AccountID == undefined - ){throw "ERROR"} - */ - let date = moment(req.body.date).set({ - hour: req.body.time.split(":")[0], - minute:req.body.time.split(":")[1] - }) - console.log(date.toDate()) - await Transaction.transferAmountFromTo(1,req.body.fromAccount,req.body.toAccount,req.body.amount,req.body.categoryID) - await Transaction.create({ - Amount: req.body.amount, - Date: date.toDate(), - Type: req.body.type, - //@ts-ignore - UserID: 1, // Associate the transaction with the user - //@ts-ignore - CategoryID: req.body.category, - AccountID: req.body.account - }) - } catch (e) { - console.log(e) - } - res.redirect("/transactions"); - //next(); }) - router.get('/addTransaction', async (req, res) => { + + router.get('/acc/:acc/_/:year/:month',async(req,res,next)=>{ try { - const transactions = await Transaction.findAll({ - include: Account + let m = moment(); + m.set({ + year: parseInt(req.params.year), + month: parseInt(req.params.month) - 1, + date: 1 }); - const categories = await Category.findAll({order:[["Name","ASC"]]}); - const accounts = await Account.findAll({order:[["Name","ASC"]]}); - //console.log(transactions) - res.render('transactions/formTransaction', { title: 'Transactions', transactions, categories, accounts }); - } catch (err) { - console.error(err); - res.status(500).send('Internal Server Error'); - } - }) - router.get('/addTransfer', async (req, res) => { - try { - const transactions = await Transaction.findAll({ - include: Account - }); - const categories = await Category.findAll({order:[["Name","ASC"]]}); - const accounts = await Account.findAll({order:[["Name","ASC"]]}); - //console.log(transactions) - res.render('transactions/formTransfer', { title: 'Transactions', transactions, categories, accounts }); - } catch (err) { - console.error(err); - res.status(500).send('Internal Server Error'); - } - }) - - router.get('/acc/:acc', async (req: Request, res: Response) => { - try { - const transactions = await Transaction.findAll({ - include: [Account,Category], + await listTransactions.GET(req, res, next, { + date: m.toDate(), where: { AccountID: req.params.acc } - }); - let arr = convertDateArray(transactions); - res.render('transactions/listTransactions', { title: 'Transactions', transactions:arr, cssClass:"" }); - } catch (err) { - console.error(err); - res.status(500).send('Internal Server Error'); - } - }); - router.get('/cat/:cat', async (req: Request, res: Response) => { - try { - const transactions = await Transaction.findAll({ - include: [Account, Category], - where: { - //@ts-ignore - CategoryID: req.params.cat - } - }); - let arr = convertDateArray(transactions); - res.render('transactions/listTransactions', { title: 'Transactions', transactions:arr, cssClass:"" }); - } catch (err) { - console.error(err); - res.status(500).send('Internal Server Error'); - } - }); - router.all('/', async (req, res) => { - try { - let transactions:any = await Transaction.findAll({ - include: [Account,Category] - }); - const categories = await Category.findAll({}); - const accounts = await Account.findAll({}); - let arr = convertDateArray(transactions); - console.log(transactions) - res.render('transactions/listTransactions', { title: 'Transactions', transactions: arr, categories, accounts, cssClass:"fullList" }); + }) } catch (err) { console.error(err); res.status(500).send('Internal Server Error'); } }) + router.get('/acc/:acc', async (req: Request, res: Response,next) => { + try { + await listTransactions.GET(req, res, next, { + where: { + AccountID: req.params.acc + } + }) + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } + }); + router.get('/cat/:cat/_/:year/:month', async (req: Request, res: Response, next) => { + try { + let m = moment(); + m.set({ + year: parseInt(req.params.year), + month: parseInt(req.params.month) - 1, + date: 1 + }); + await listTransactions.GET(req, res, next, { + date: m.toDate(), + where: { + CategoryID: req.params.cat + } + }) + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } + }); + router.get('/cat/:cat', async (req: Request, res: Response, next) => { + try { + await listTransactions.GET(req, res, next, { + where: { + CategoryID: req.params.cat + } + }) + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } + }); + router.get('/_/:year/:month', async (req, res, next) => { + try { + let m = moment(); + m.set({ + year: parseInt(req.params.year), + month: parseInt(req.params.month) - 1, + date: 1 + }); + await listTransactions.GET(req, res, next, { date: m.toDate() }) + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } + }) + router.get('/', listTransactions.GET) return router; } -} \ No newline at end of file +} diff --git a/src/controller/router/transactions/addTransaction.ts b/src/controller/router/transactions/addTransaction.ts new file mode 100644 index 0000000..d48adc3 --- /dev/null +++ b/src/controller/router/transactions/addTransaction.ts @@ -0,0 +1,43 @@ +import { Request, NextFunction, Response } from "express"; +import moment from "moment"; +import { Category, Transaction, Account } from "../../models"; + +const POST = async (req: Request, res: Response, next: NextFunction, data = {}) => { + try { + let date = moment(req.body.date).set({ + hour: req.body.time.split(":")[0], + minute: req.body.time.split(":")[1] + }) + console.log(date.toDate()) + await Transaction.create({ + Amount: req.body.amount, + Date: date.toDate(), + Type: req.body.type, + //@ts-ignore + UserID: 1, // Associate the transaction with the user + //@ts-ignore + CategoryID: req.body.category, + AccountID: req.body.account + }) + } catch (e) { + console.log(e) + } + res.redirect("/transactions"); +} + +const GET = async (req: Request, res: Response, next: NextFunction, data = {}) => { + try { + const transactions = await Transaction.findAll({ + include: Account + }); + const categories = await Category.findAll({order:[["Name","ASC"]]}); + const accounts = await Account.findAll({order:[["Name","ASC"]]}); + //console.log(transactions) + res.render('transactions/formTransaction', { title: 'Transactions', transactions, categories, accounts }); + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } +} + +export default { POST, GET }; \ No newline at end of file diff --git a/src/controller/router/transactions/addTransfer.ts b/src/controller/router/transactions/addTransfer.ts new file mode 100644 index 0000000..adc939d --- /dev/null +++ b/src/controller/router/transactions/addTransfer.ts @@ -0,0 +1,57 @@ +import { Request, NextFunction, Response } from "express"; +import moment from "moment"; +import { Category, Transaction, Account } from "../../models"; + +const POST = async (req: Request, res: Response, next: NextFunction, data = {}) => { + + try { + /* + if( + req.body.date == undefined + || req.body.Type == undefined + || req.body.CategoryID == undefined + || req.body.AccountID == undefined + ){throw "ERROR"} + */ + let date = moment(req.body.date).set({ + hour: req.body.time.split(":")[0], + minute: req.body.time.split(":")[1] + }) + console.log(date.toDate()) + await Transaction.transferAmountFromTo(1, req.body.fromAccount, req.body.toAccount, req.body.amount, req.body.categoryID) + await Transaction.create({ + Amount: req.body.amount, + Date: date.toDate(), + Type: req.body.type, + //@ts-ignore + UserID: 1, // Associate the transaction with the user + //@ts-ignore + CategoryID: req.body.category, + AccountID: req.body.account + }) + } catch (e) { + console.log(e) + } + res.redirect("/transactions"); +} + +const GET = async (req: Request, res: Response, next: NextFunction, data = {}) => { + try { + const transactions = await Transaction.findAll({ + include: Account + }); + const categories = await Category.findAll({ + order: [["Name", "ASC"]] + }); + const accounts = await Account.findAll({ + order: [["Name", "ASC"]] + }); + + res.render('transactions/formTransfer', { title: 'Transactions', transactions, categories, accounts }); + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } +} + +export default { POST, GET }; \ No newline at end of file diff --git a/src/controller/router/transactions/index.ts b/src/controller/router/transactions/index.ts new file mode 100644 index 0000000..0d92920 --- /dev/null +++ b/src/controller/router/transactions/index.ts @@ -0,0 +1,5 @@ +import addTransaction from "./addTransaction"; +import addTransfer from "./addTransfer"; +import listTransactions from "./listTransactions"; + +export { addTransaction, addTransfer, listTransactions } \ No newline at end of file diff --git a/src/controller/router/transactions/listTransactions.ts b/src/controller/router/transactions/listTransactions.ts new file mode 100644 index 0000000..c6488cc --- /dev/null +++ b/src/controller/router/transactions/listTransactions.ts @@ -0,0 +1,34 @@ +import { Request, NextFunction, Response } from "express"; +import moment from "moment"; +import { Op } from "sequelize"; + +import { Category, Transaction, Account } from "../../models"; +import { convertDateArray } from "../../helper/dateConversion"; +import { getFirstLastDayOfMonth } from "../../helper"; + +const GET = async (req: Request, res: Response, next: NextFunction, data:any = {date:new Date(),where:{}}) => { + try { + moment.locale("de") + let m = moment(data.date); + let { startDate, endDate } = getFirstLastDayOfMonth(m); + let transactions:any = await Transaction.findAll({ + include: [Account,Category], + where:{ + Date:{ + [Op.between]:[startDate.toDate(),endDate.toDate()] + }, + ...data.where + } + }); + const categories = await Category.findAll({}); + const accounts = await Account.findAll({}); + let arr = convertDateArray(transactions); + + res.render('transactions/listTransactions', { title: 'Transactions', transactions: arr, categories, accounts, cssClass:"fullList" }); + } catch (err) { + console.error(err); + res.status(500).send('Internal Server Error'); + } +} + +export default { GET }; \ No newline at end of file diff --git a/views/index.pug b/views/index.pug index 0acb5ea..1b236bc 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,10 +1,14 @@ -doctype html -html - head - title= title - body - h1 Welcome to our Web App! - p This is a simple example using Express and Pug. +extends layout/base.pug + +block nav + a(href="/transactions") Transactions + a(href="/transactions/addTransaction") + Transaction + a(href="/transactions/addTransfer") + Transfer + +block headline + h1 Home + +block content a(href='/users') View Users div diff --git a/views/transactions/modifyTransaction.pug b/views/transactions/modifyTransaction.pug new file mode 100644 index 0000000..0b4e3fb --- /dev/null +++ b/views/transactions/modifyTransaction.pug @@ -0,0 +1,39 @@ +extends ./transactions.pug +block content + form(action="/transactions/addTransaction",method="POST") + div.form-group + .text Description + .input + textarea(name="description", cols="30", rows="5") + div.form-group.half + .text Date + .input + input(type="date",name="date") + div.form-group.half + .text Time + .input + input(type="time",name="time") + div.form-group.half + .text Type + .input + select(name="type") + option(value="income") Income + option(value="expense",select) Expense + option(value="transfer") Transfer + div.form-group.half + .text Account + .input + select(name="account") + each account in accounts + option(value=account.AccountID)=account.Name + div.form-group.half + .text Amount + .input + input(type="number",min="0",step="0.01",name="amount",required) + div.form-group.half + .text Category + .input + select(name="category") + each category in categories + option(value=category.CategoryID,title=category.Description)=category.Name + button(type="submit") Modify Transaction \ No newline at end of file