Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
UserInitRequest.h
Go to the documentation of this file.
1 
8 #ifndef USERINITREQUEST_H
9 #define USERINITREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 class UserInitRequest : public Request
22 {
23 private:
24  DB::DatabaseManager* dbManager = nullptr;
25 
26 public:
32  UserInitRequest() : dbManager(DB::DatabaseManager::createInstance())
33  {
34  // Log to database log table (if needed)
35  }
36 
48  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
49  {
50  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
51 
52  QString email;
53  QString password;
54 
55  QJsonObject response;
56  QJsonObject data;
57 
58  response.insert("Response", 11);
59 
60  // Extract the data array
61  if (jsonObj.contains("Data"))
62  {
63  QJsonObject dataObj = jsonObj["Data"].toObject();
64 
65  if (dataObj.contains("email"))
66  {
67  email = dataObj.value("email").toString();
68  }
69 
70  if (dataObj.contains("password"))
71  {
72  password = dataObj.value("password").toString();
73  }
74  }
75  else
76  {
77  qCritical() << "Data not found";
78  }
79 
80  do
81  {
82  if (!isDBConnectionValid(dbManager))
83  {
84  return CreateDBConnectionError(response, data);
85  }
86 
87  // Validate user credentials
88  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", email)->exec();
89  int user_id = result.first().value("id").toInt();
90  QJsonObject userObj = result.first();
91 
92  if (result.isEmpty())
93  {
94  return CreateErrorResponse(response, data, "email not found");
95  }
96 
97  result = dbManager->select("password")->table("users")->where("id =", user_id)->exec();
98  if (result.first().value("password").toString() != password)
99  {
100  return CreateErrorResponse(response, data, "Invalid password");
101  }
102 
103  QString role = userObj.value("role").toString();
104 
105  data.insert("status", int(true));
106  data.insert("first_name", userObj.value("first_name").toString());
107  data.insert("role", role);
108  data.insert("email", email);
109 
110  if (role == "user")
111  {
112  // Retrieve account number and current balance for the user
113  DB::DbResult accountResult = dbManager->select("account_number, balance")
114  ->table("accounts")
115  ->where("user_id =", userObj.value("id").toInt())
116  ->exec();
117 
118  QJsonObject accountObj = accountResult.first();
119 
120  int accountNumber = accountObj.value("account_number").toInt();
121  double currentBalance = accountObj.value("balance").toDouble();
122 
123  data.insert("account_number", accountNumber);
124  data.insert("current_balance", currentBalance);
125  }
126 
127  response.insert("Data", data);
128  } while (false);
129 
130  // Convert response to JSON
131  QJsonDocument responseDoc(response);
132  QByteArray responseData = responseDoc.toJson();
133 
134  // Send response
135  qDebug().noquote() << "<-- InitRequest::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
136 
137  return response;
138  }
139 };
140 
141 #endif // USERINITREQUEST_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 * 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
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
bool isEmpty() const
Checks if the result set is empty.
Definition: dbresult.cpp:20
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
The UserInitRequest class handles user initialization requests for User Widget and Admin Widget.
Definition: UserInitRequest.h:22
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the user initialization request.
Definition: UserInitRequest.h:48
UserInitRequest()
Constructor for the UserInitRequest class.
Definition: UserInitRequest.h:32
Database management classes for handling database connections and operations.