Move cash transaction to sql

This commit is contained in:
Nikola Petrov 2024-07-16 22:23:54 +02:00
parent e89a8536be
commit 12b9e62f8f
2 changed files with 75 additions and 20 deletions

View File

@ -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" });
});

View File

@ -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<number> {
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<ResultSetHeader>(options);
return result.affectedRows;
}
catch (err) {
console.log(err);
}
return 0;
}
export default CashTransaction;
async function find(): Promise<CashTransaction[]> {
try {
const [rows, fields] = await pool.query<CashTransaction[]>("SELECT * FROM bankCardTransaction;");
return rows;
}
catch (err) {
console.log(err);
}
return [];
}
async function findWithPar(month: number, year: number): Promise<CashTransaction[]> {
try {
const [rows, fields] = await pool.query<CashTransaction[]>("SELECT * FROM bankCardTransaction;");
return rows;
}
catch (err) {
console.log(err);
}
return [];
}
async function findOneAndDelete(type: number): Promise<number> {
try {
const [result, fields] = await pool.query<ResultSetHeader>("DELETE FROM bankCardTransaction WHERE type = ?;", [type]);
return result.affectedRows;
}
catch (err) {
console.log(err);
}
return 0;
}
export default {
find,
findWithPar,
save,
findOneAndDelete
};