Banking_System_Server  1.0.0
Qt-based banking app for user/admin account management, transactions, secure server communication via PostgreSQL/Supabase.
DeleteUserRequest.h
Go to the documentation of this file.
1 
8 #ifndef DELETEUSERREQUEST_H
9 #define DELETEUSERREQUEST_H
10 
11 #include "Request.h"
12 #include "db.h"
13 
21 class DeleteUserRequest : public Request
22 {
23 private:
24  DB::DatabaseManager* dbManager = nullptr;
25 
26 public:
32  DeleteUserRequest() : dbManager(DB::DatabaseManager::createInstance())
33  {
34  // Log to database log table (if needed)
35  }
36 
49  QJsonObject execute(const QJsonObject& jsonObj, QMutex& m) override
50  {
51  QMutexLocker locker(&m); // Lock the mutex for the duration of this function
52 
53  QString admin_email;
54  int account_number;
55 
56  QJsonObject response;
57  QJsonObject data;
58 
59  response.insert("Response", 9);
60 
61  // Extract the data array
62  if (jsonObj.contains("Data"))
63  {
64  QJsonObject dataObj = jsonObj["Data"].toObject();
65 
66  if (dataObj.contains("email"))
67  {
68  admin_email = dataObj.value("email").toString();
69  }
70  if (dataObj.contains("account_number"))
71  {
72  account_number = dataObj.value("account_number").toInt();
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  // Check if the user is an admin
88  DB::DbResult result = dbManager->select("*")->table("users")->where("email =", admin_email)->exec();
89 
90  if (result.isEmpty())
91  {
92  return CreateErrorResponse(response, data, "you are not registered user!");
93  }
94 
95  QJsonObject obj = result.first();
96 
97  if (obj.value("role").toString() != "admin")
98  {
99  return CreateErrorResponse(response, data, "Unauthorized, Cannot delete user.");
100  }
101 
102  // Check if the account number is valid
103  result = dbManager->select("*")->table("accounts")->where("account_number =", account_number)->exec();
104 
105  if (result.isEmpty())
106  {
107  return CreateErrorResponse(response, data, "Account number does not exist");
108  }
109 
110  // Get the user id from the account number
111  int user_id = result.first().value("user_id").toInt();
112 
113  bool success = dbManager->where("account_number = ", account_number)->del("accounts");
114 
115  if (!success)
116  {
117  return CreateErrorResponse(response, data, "Failed to delete account");
118  }
119 
120  success = dbManager->where("id = ", user_id)->del("users");
121 
122  if (!success)
123  {
124  return CreateErrorResponse(response, data, "Failed to delete user");
125  }
126 
127  data.insert("status", int(true));
128  data.insert("message", "User deleted successfully");
129 
130  response.insert("Data", data);
131 
132  } while (false);
133 
134  // Send response
135  qDebug().noquote() << "<-- DeleteUser::Response :\n" << QJsonDocument(response).toJson(QJsonDocument::Indented);
136 
137  return response;
138  }
139 };
140 
141 #endif // DELETEUSERREQUEST_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
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 DeleteUserRequest class handles the deletion of users.
Definition: DeleteUserRequest.h:22
DeleteUserRequest()
Constructor for the DeleteUserRequest class.
Definition: DeleteUserRequest.h:32
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to delete a user.
Definition: DeleteUserRequest.h:49
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.