diff --git a/controllers/cashTransactionController.ts b/controllers/cashTransactionController.ts index d6d21b4..6f6f3f2 100644 --- a/controllers/cashTransactionController.ts +++ b/controllers/cashTransactionController.ts @@ -1,5 +1,6 @@ import { type Request, type Response } from "express"; -import cashTransactionModel from '../models/cashTransactionModel.js'; + +import cashTransactionModel from '../models/cashTransactionModel'; const types = ['ZAVRNITEV POS NAKUP', 'POS NAKUP', 'BA DVIG', 'priliv', 'SDD', 'SPLET/TEL NAKUP', 'PREDAVTORIZACIJE']; @@ -16,9 +17,9 @@ export default { var month = splitDate[1]; year = parseInt(year); month = parseInt(month); - transactions = await cashTransactionModel.find({ year, month }, { _id: 0, __v: 0, raw: 0 }); + transactions = await cashTransactionModel.findWithPar(year, month); } else { - transactions = await cashTransactionModel.find({}, { _id: 0, __v: 0, raw: 0 }); + transactions = await cashTransactionModel.find(); } const data = { transactions: transactions, types: types }; @@ -37,15 +38,15 @@ export default { if (rawString == "") return res.status(400).json({ message: "empty string" }); - const transaction = new cashTransactionModel({ - raw: rawString, + const transaction = { + rawMessage: rawString, day: 0, month: 0, year: 0, amount: 0, type: -1, company: "", - }); + }; const datePattern = /(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(19|20)\d{2}/; const amountPattern = /\d{1,3}(?:[.]\d{3})*(?:[.,]\d{2})(?=\sEUR)/; @@ -76,7 +77,7 @@ export default { transaction.year = date[2]; } } - const trans = await transaction.save() + const trans = await cashTransactionModel.save(transaction.rawMessage, transaction.day, transaction.month, transaction.year, transaction.amount, transaction.type, transaction.company); if (trans) { return res.status(201).json(trans); } @@ -86,7 +87,7 @@ export default { }, delete: async function (req: Request, res: Response) { - cashTransactionModel.deleteMany({ type: -1 }) + cashTransactionModel.findOneAndDelete(-1) .then(data => { res.status(201).json({ message: "OK" }); }); diff --git a/models/cashTransactionModel.ts b/models/cashTransactionModel.ts index e71ca90..7169f77 100644 --- a/models/cashTransactionModel.ts +++ b/models/cashTransactionModel.ts @@ -1,15 +1,69 @@ -import mongoose, { Schema } from 'mongoose'; +import { type ResultSetHeader, type RowDataPacket, type QueryOptions } from "mysql2" +import pool from '../miscellaneous/db' -const cashTransaction = new Schema({ - 'raw': String, - 'day': Number, - 'month': Number, - 'year': Number, - 'amount': Number, - 'type': Number, - 'company': String, -}); +interface CashTransaction extends RowDataPacket { + id?: number; + rawMessage?: string; + day?: number; + month?: number; + year?: number; + amount?: number; + type?: number; + company?: string; +} -const CashTransaction = mongoose.model('cashTransaction', cashTransaction); +async function save(rawMessage: string, day: number, month: number, year: number, amount: number, type: number, company: string): Promise { + try { + const options: QueryOptions = { + sql: "INSERT INTO bankCardTransaction (raw, day, month, year, amount, type, company) VALUES (?,?,?,?,?,?,?)", + values: [rawMessage, day, month, year, amount, type, company] + }; + const [result, fields] = await pool.query(options); + return result.affectedRows; + } + catch (err) { + console.log(err); + } + return 0; +} -export default CashTransaction; +async function find(): Promise { + try { + const [rows, fields] = await pool.query("SELECT * FROM bankCardTransaction;"); + return rows; + } + catch (err) { + console.log(err); + } + return []; +} + +async function findWithPar(month: number, year: number): Promise { + try { + const [rows, fields] = await pool.query("SELECT * FROM bankCardTransaction;"); + return rows; + } + catch (err) { + console.log(err); + } + return []; +} + +async function findOneAndDelete(type: number): Promise { + try { + const [result, fields] = await pool.query("DELETE FROM bankCardTransaction WHERE type = ?;", [type]); + return result.affectedRows; + } + catch (err) { + console.log(err); + } + return 0; +} + + +export default { + find, + findWithPar, + save, + findOneAndDelete +};