Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
UpdateUserRequest.h
Go to the documentation of this file.
1 
8 #ifndef UPDATEUSERREQUEST_H
9 #define UPDATEUSERREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 class UpdateUserRequest : public Request
22 {
23 private:
24  DB::DatabaseManager* dbManager = nullptr;
25 
26 public:
32  UpdateUserRequest() : 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  int account_number;
54  QString new_first_name;
55  QString new_last_name;
56  QString new_email;
57  QString new_role;
58 
59  QJsonObject response;
60  QJsonObject data;
61 
62  response.insert("Response", 10);
63 
64  // Extract the data array
65  if (jsonObj.contains("Data"))
66  {
67  QJsonObject dataObj = jsonObj["Data"].toObject();
68 
69  if (dataObj.contains("email"))
70  {
71  email = dataObj.value("email").toString();
72  }
73  if (dataObj.contains("account_number"))
74  {
75  account_number = dataObj.value("account_number").toInt();
76  }
77  if (dataObj.contains("newData"))
78  {
79  QJsonObject newDataObj = dataObj["newData"].toObject();
80 
81  if (newDataObj.contains("first_name"))
82  {
83  new_first_name = newDataObj.value("first_name").toString();
84  }
85  if (newDataObj.contains("last_name"))
86  {
87  new_last_name = newDataObj.value("last_name").toString();
88  }
89  if (newDataObj.contains("email"))
90  {
91  new_email = newDataObj.value("email").toString();
92  }
93  if (newDataObj.contains("role"))
94  {
95  new_role = newDataObj.value("role").toString();
96  }
97  }
98  }
99  else
100  {
101  qCritical() << "Data not found";
102  }
103 
104  do
105  {
106  if (!isDBConnectionValid(dbManager))
107  {
108  return CreateDBConnectionError(response, data);
109  }
110 
111  // Check if the user is an admin
112  DB::DbResult result = dbManager->select("role")->table("users")->where("email =", email)->exec();
113  QJsonObject obj = result.first();
114  QString sneder_role = obj.value("role").toString();
115 
116  if (result.isEmpty())
117  {
118  return CreateErrorResponse(response, data, "you are not registered user!");
119  }
120 
121  if (sneder_role != "admin")
122  {
123  return CreateErrorResponse(response, data, "Unauthorized, Cannot update user");
124  }
125 
126  // Check if the account number is valid
127  result = dbManager->select("*")->table("accounts")->where("account_number =", account_number)->exec();
128  // Get the user id from the account number
129  int user_id = result.first().value("user_id").toInt();
130 
131  if (result.isEmpty())
132  {
133  return CreateErrorResponse(response, data, "Account number does not exist");
134  }
135 
136  // check that new email is not associated with another account or user unless it is the current user
137  result = dbManager->select("*")->table("users")->where("email =", new_email)->exec();
138 
139  int new_user_id = result.first().value("id").toInt();
140 
141  qDebug() << "user_id: " << user_id;
142  qDebug() << "new_user_id: " << new_user_id;
143 
144  if (!result.isEmpty() && result.first().value("id").toInt() != user_id)
145  {
146  return CreateErrorResponse(response, data, "Email is associated with another account");
147  }
148 
149  qDebug() << "Debugging update user";
150  qDebug() << "user_id: " << user_id;
151  qDebug() << "new_email: " << new_email;
152  qDebug() << "new_role: " << new_role;
153  qDebug() << "new_first_name: " << new_first_name;
154  qDebug() << "new_last_name: " << new_last_name;
155 
156  // check for the role of the user
157  result = dbManager->select("role")->table("users")->where("id =", user_id)->exec();
158  QString current_role = result.first().value("role").toString();
159 
160  if (current_role == "user" && new_role == "admin")
161  {
162  // delete the account associated bank account
163  bool success = dbManager->where("account_number = ", account_number)->del("accounts");
164 
165  if (!success)
166  {
167  return CreateErrorResponse(response, data, "Failed to delete account");
168  }
169  }
170 
171  // Update the user details
172  bool success = dbManager->where("id = ", user_id)
173  ->update("users", {{"first_name", new_first_name},
174  {"last_name", new_last_name},
175  {"email", new_email},
176  {"role", new_role}});
177 
178  if (!success)
179  {
180  return CreateErrorResponse(response, data, "Failed to update user");
181  }
182 
183  data.insert("status", int(true));
184  data.insert("message", "User updated successfully");
185 
186  response.insert("Data", data);
187 
188  } while (false);
189 
190  // Convert response to JSON
191  QJsonDocument responseDoc(response);
192  QByteArray responseData = responseDoc.toJson();
193 
194  // Send response
195  qDebug().noquote() << "<-- UpdateUser::Response :\n" << QJsonDocument(response).toJson(QJsonDocument::Indented);
196 
197  return response;
198  }
199 };
200 
201 #endif // UPDATEUSERREQUEST_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
bool update(const QString &table, const QVariantMap &data)
Updates data in a specified table.
Definition: db.cpp:361
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
bool del(const QString &table)
Deletes data from a specified table.
Definition: db.cpp:398
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 UpdateUserRequest class handles user update requests.
Definition: UpdateUserRequest.h:22
UpdateUserRequest()
Constructor for the UpdateUserRequest class.
Definition: UpdateUserRequest.h:32
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the user update request.
Definition: UpdateUserRequest.h:48
Database management classes for handling database connections and operations.