8 #ifndef CREATENEWUSERREQUEST_H
9 #define CREATENEWUSERREQUEST_H
13 #include <QRandomGenerator>
50 QJsonObject
execute(
const QJsonObject& jsonObj, QMutex& m)
override
52 QMutexLocker locker(&m);
59 QString new_password{};
61 double initial_balance{};
66 response.insert(
"Response", 8);
69 if (jsonObj.contains(
"Data"))
71 QJsonObject dataObj = jsonObj[
"Data"].toObject();
73 if (dataObj.contains(
"email"))
75 email = dataObj.value(
"email").toString();
78 if (dataObj.contains(
"newUser"))
80 QJsonObject newUserObj = dataObj[
"newUser"].toObject();
82 if (newUserObj.contains(
"first_name"))
84 first_name = newUserObj.value(
"first_name").toString();
86 if (newUserObj.contains(
"last_name"))
88 last_name = newUserObj.value(
"last_name").toString();
90 if (newUserObj.contains(
"email"))
92 new_email = newUserObj.value(
"email").toString();
94 if (newUserObj.contains(
"password"))
96 new_password = newUserObj.value(
"password").toString();
98 if (newUserObj.contains(
"role"))
100 role = newUserObj.value(
"role").toString();
102 if (newUserObj.contains(
"initial_balance"))
104 initial_balance = newUserObj.value(
"initial_balance").toDouble();
110 qCritical() <<
"Data not found";
120 qDebug() <<
"First Name: " << first_name;
121 qDebug() <<
"Last Name: " << last_name;
122 qDebug() <<
"Email: " << new_email;
123 qDebug() <<
"Password: " << new_password;
124 qDebug() <<
"Role: " << role;
125 qDebug() <<
"Initial Balance: " << initial_balance;
136 QJsonObject obj = result.
first();
139 if (result.
first().value(
"role").toString() !=
"admin")
142 "Unauthorized, Cannot create new user. User is not an admin");
145 if (new_email.isEmpty() || new_password.isEmpty() || first_name.isEmpty() || last_name.isEmpty() ||
152 if (role !=
"admin" && role !=
"user")
158 if (role ==
"admin" && initial_balance != 0)
160 return CreateErrorResponse(response, data,
"Admin can't have account and initial balance");
172 bool success = dbManager->
insert(
"users", {{
"email", new_email},
173 {
"password", new_password},
174 {
"first_name", first_name},
175 {
"last_name", last_name},
182 if (role ==
"admin" && initial_balance == 0)
184 qDebug() <<
"New admin created successfully";
186 data.insert(
"status",
int(
true));
187 data.insert(
"message",
"New admin created successfully");
189 response.insert(
"Data", data);
196 QVariant lastId = result.
first().value(
"id");
201 int account_number = 0;
206 account_number = QRandomGenerator::global()->bounded(100000, 999999);
207 result = dbManager->
select(
"*")->
table(
"accounts")->
where(
"account_number =", account_number)->
exec();
210 success = dbManager->
insert(
212 {{
"account_number", account_number}, {
"user_id", lastId.toInt()}, {
"balance", initial_balance}});
219 data.insert(
"status",
int(
true));
220 data.insert(
"message",
"New user created successfully");
222 response.insert(
"Data", data);
227 QJsonDocument responseDoc(response);
228 QByteArray responseData = responseDoc.toJson();
231 qDebug().noquote() <<
"<-- CreateNewUser::Response :\n" << responseDoc.toJson(QJsonDocument::Indented);
This file contains the declaration of the Request class, which is an abstract base class for handling...
The CreateNewUserRequest class handles the creation of new users.
Definition: CreateNewUserRequest.h:23
QJsonObject execute(const QJsonObject &jsonObj, QMutex &m) override
Executes the request to create a new user.
Definition: CreateNewUserRequest.h:50
CreateNewUserRequest()
Constructor for the CreateNewUserRequest class.
Definition: CreateNewUserRequest.h:33
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
bool insert(const QString &table, const QVariantMap &data)
Inserts data into a specified table.
Definition: db.cpp:327
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
Database management classes for handling database connections and operations.