personal_website/models/cashTransactionModel.ts
2024-09-23 23:24:59 +02:00

70 lines
1.7 KiB
TypeScript

import { type ResultSetHeader, type RowDataPacket, type QueryOptions } from "mysql2"
import pool from 'miscellaneous/db'
interface CashTransaction extends RowDataPacket {
id?: number;
rawMessage?: string;
day?: number;
month?: number;
year?: number;
amount?: number;
type?: number;
company?: string;
}
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;
}
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
};