Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
GetTransactionsHistoryRequest.h
Go to the documentation of this file.
1 
8 #ifndef GETTRANSACTIONSHISTORYREQUEST_H
9 #define GETTRANSACTIONSHISTORYREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 {
22 private:
23  DB::DatabaseManager* dbManager = nullptr;
24 
25 public:
31  GetTransactionsHistoryRequest() : dbManager(DB::DatabaseManager::createInstance())
32  {
33  // Log to database log table (if needed)
34  }
35 
46  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
47  {
48  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
49 
50  QJsonObject response;
51  QJsonObject data;
52  QString sender_email;
53  QString sender_role;
54 
55  response.insert("Response", 4);
56 
57  // Extract the data array
58  if (jsonObj.contains("Data"))
59  {
60  QJsonObject dataObj = jsonObj["Data"].toObject();
61 
62  if (dataObj.contains("email"))
63  {
64  sender_email = dataObj.value("email").toString();
65  }
66  }
67  else
68  {
69  qCritical() << "Data not found";
70  }
71 
72  do
73  {
74  if (!isDBConnectionValid(dbManager))
75  {
76  return CreateDBConnectionError(response, data);
77  }
78 
79  // Check if the user is an admin
80  DB::DbResult result = dbManager->select("role")->table("Users")->where("email =", sender_email)->exec();
81 
82  if (result.isEmpty())
83  {
84  return CreateErrorResponse(response, data, "you are not registered user!");
85  }
86 
87  QString role = result.first().value("role").toString();
88 
89  if (role == "user")
90  {
91  // Get the account number for the user
92  result = dbManager->select("A.account_number")
93  ->table("Users U")
94  ->join("JOIN Accounts A ON U.id = A.user_id")
95  ->where("U.email =", sender_email)
96  ->exec();
97 
98  if (result.isEmpty())
99  {
100  return CreateErrorResponse(response, data, "No account found");
101  }
102 
103  int accountNumber = result.first().value("account_number").toInt();
104 
105  result = dbManager->select("*")
106  ->table("Transactions")
107  ->where("from_account_number =", accountNumber)
108  ->whereOr("to_account_number =" + QString::number(accountNumber))
109  ->exec();
110 
111  if (result.isEmpty())
112  {
113  return CreateErrorResponse(response, data, "No transactions found");
114  }
115 
116  QJsonArray transactionList;
117  for (int i = 0; i < result.size(); ++i)
118  {
119  QJsonObject transactionObj;
120  QJsonObject obj = result.data(i);
121 
122  transactionObj.insert("from_account_number", obj.value("from_account_number").toInt());
123  transactionObj.insert("to_account_number", obj.value("to_account_number").toInt());
124  transactionObj.insert("amount", obj.value("amount").toDouble());
125  transactionObj.insert("created_at", obj.value("created_at").toString());
126 
127  transactionList.append(transactionObj);
128  }
129 
130  data.insert("status", int(true));
131  data.insert("message",
132  "Transaction history retrieved for account number " + QString::number(accountNumber));
133  data.insert("List", transactionList);
134 
135  response.insert("Data", data);
136  }
137  else if (role == "admin")
138  {
139  result = dbManager->select("*")->table("Transactions")->exec();
140 
141  if (result.isEmpty())
142  {
143  return CreateErrorResponse(response, data, "No transactions found");
144  }
145 
146  QJsonArray transactionList;
147  for (int i = 0; i < result.size(); ++i)
148  {
149  QJsonObject transactionObj;
150  QJsonObject obj = result.data(i);
151 
152  transactionObj.insert("from_account_number", obj.value("from_account_number").toInt());
153  transactionObj.insert("to_account_number", obj.value("to_account_number").toInt());
154  transactionObj.insert("amount", obj.value("amount").toDouble());
155  transactionObj.insert("created_at", obj.value("created_at").toString());
156 
157  transactionList.append(transactionObj);
158  }
159 
160  data.insert("status", int(true));
161  data.insert("message", "Transaction history retrieved for all users");
162  data.insert("List", transactionList);
163 
164  response.insert("Data", data);
165  }
166 
167  } while (false);
168 
169  // Convert response to JSON
170  QJsonDocument responseDoc(response);
171  QByteArray responseData = responseDoc.toJson();
172 
173  // Send response
174  qDebug().noquote() << "<-- GetTransactionsHistory::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
175 
176  return response;
177  }
178 };
179 
180 #endif // GETTRANSACTIONSHISTORYREQUEST_H
This file contains the declaration of the Request class, which is an abstract base class for handling...
Manages database connections and SQL operations.
Definition: db.h:80
DatabaseManager * table(const QString &value)
Sets the table for the query.
Definition: db.cpp:115
DatabaseManager * whereOr(const QString &value)
Adds an OR condition to the WHERE clause.
Definition: db.cpp:180
DatabaseManager * where(const QString &value, const QVariant &val=QVariant())
Adds a WHERE clause to the query.
Definition: db.cpp:121
DbResult exec()
Executes the built query.
Definition: db.cpp:266
DatabaseManager * select(const QString &value)
Selects columns for the query.
Definition: db.cpp:107
DatabaseManager * join(const QString &value)
Adds a JOIN clause to the query.
Definition: db.cpp:188
The DbResult class represents a result set returned from a database query.
Definition: dbresult.h:24
QJsonObject first() const
Retrieves the first item in the result set.
Definition: dbresult.cpp:25
int size() const
Gets the number of items in the result set.
Definition: dbresult.cpp:30
QJsonObject data(int pos) const
Retrieves data at a specific position in the result set.
Definition: dbresult.cpp:35
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
The GetTransactionsHistoryRequest class handles the retrieval of transaction history.
Definition: GetTransactionsHistoryRequest.h:21
GetTransactionsHistoryRequest()
Constructor for the GetTransactionsHistoryRequest class.
Definition: GetTransactionsHistoryRequest.h:31
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to get the transaction history.
Definition: GetTransactionsHistoryRequest.h:46
The Request class is an abstract base class for handling different types of requests.
Definition: Request.h:25
QJsonObject CreateErrorResponse(QJsonObject &response, QJsonObject &dataObj, QString message)
Creates a generic error JSON response.
Definition: Request.h:90
QJsonObject CreateDBConnectionError(QJsonObject &response, QJsonObject &dataObj)
Creates a JSON response indicating a database connection error.
Definition: Request.h:65
bool isDBConnectionValid(DB::DatabaseManager *dbManager)
Checks if the database connection is valid.
Definition: Request.h:36
Database management classes for handling database connections and operations.