-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Serveis-Neby/develop
Merge develop to main
- Loading branch information
Showing
54 changed files
with
4,525 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# Backend | ||
|
||
[TEST](test/TEST_README.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <crow.h> | ||
#include <models/notification_model.h> | ||
#include <models/service_model.h> | ||
#include <models/user_model.h> | ||
#include <utils/common.h> | ||
#include <utils/utils.h> | ||
#include <format> | ||
#include <memory> | ||
#include <pqxx/pqxx> | ||
#include <string> | ||
|
||
class NotificationController { | ||
public: | ||
static void create_notification(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& service_id); | ||
static void handle_notification(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& notification_id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <condition_variable> | ||
#include <iostream> | ||
#include <mutex> | ||
#include <pqxx/pqxx> | ||
#include <queue> | ||
|
||
class ConnectionPool { | ||
public: | ||
static ConnectionPool& getInstance(const std::string& connectionString, int poolSize) { | ||
static ConnectionPool instance(connectionString, poolSize); | ||
return instance; | ||
} | ||
|
||
ConnectionPool(const ConnectionPool&) = delete; | ||
ConnectionPool& operator=(const ConnectionPool&) = delete; | ||
|
||
std::shared_ptr<pqxx::connection> getConnection(); | ||
|
||
void releaseConnection(std::shared_ptr<pqxx::connection> conn); | ||
|
||
private: | ||
ConnectionPool(const std::string& connectionString, int poolSize); | ||
|
||
~ConnectionPool() ; | ||
|
||
std::shared_ptr<pqxx::connection> createConnection(); | ||
|
||
private: | ||
std::string connectionString_; | ||
int poolSize_; | ||
std::deque<std::shared_ptr<pqxx::connection>> connections_; | ||
std::mutex mutex_; | ||
std::condition_variable condition_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,18 @@ | ||
#pragma once | ||
|
||
#include <crow.h> | ||
#include <db/connection_pool.h> | ||
#include <jwt-cpp/jwt.h> | ||
#include <utils/auth.h> | ||
#include <utils/common.h> | ||
#include <utils/utils.h> | ||
#include <memory> | ||
|
||
struct VerifyJWT : crow::ILocalMiddleware { | ||
struct context {}; | ||
|
||
void before_handle(crow::request& req, crow::response& res, context& ctx) { | ||
std::string token = get_token_cookie(req); | ||
|
||
if (!validate_token(token)) { | ||
handle_error(res, "invalid token", 401); | ||
return; | ||
} | ||
|
||
auto decoded = jwt::decode(token); | ||
|
||
std::string id; | ||
std::string type; | ||
|
||
// Acceder al payload del token decodificado | ||
for (auto& e : decoded.get_payload_json()) { | ||
if (e.first == "id") { | ||
id = e.second.get<std::string>(); | ||
} else if (e.first == "type") { | ||
type = e.second.get<std::string>(); | ||
} | ||
} | ||
|
||
if (req.body == "") { | ||
crow::json::wvalue body; | ||
|
||
body["id"] = id; | ||
body["isAdmin"] = (type == "admin"); | ||
|
||
req.body = body.dump(); | ||
} else { | ||
crow::json::wvalue body = crow::json::load(req.body); | ||
|
||
body["id"] = id; | ||
body["isAdmin"] = (type == "admin"); | ||
|
||
req.body = body.dump(); | ||
} | ||
} | ||
|
||
void after_handle(crow::request& req, crow::response& res, context& ctx) {} | ||
void before_handle(crow::request& req, crow::response& res, context& ctx); | ||
void after_handle(crow::request& req, crow::response& res, context& ctx); | ||
}; | ||
|
||
using NebyApp = crow::App<VerifyJWT>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
#pragma once | ||
|
||
#include <utils/common.h> | ||
#include <utils/errors.h> | ||
#include <memory> | ||
|
||
class NotificationModel { | ||
private: | ||
std::string _id; | ||
std::string _sender_id; | ||
std::string _receiver_id; | ||
std::string _service_id; | ||
std::string _status; | ||
std::string _created_at; | ||
std::string _updated_at; | ||
|
||
public: | ||
NotificationModel(std::string id, std::string sender_id, std::string receiver_id, std::string service_id, std::string status); | ||
NotificationModel(std::string id, std::string sender_id, std::string service_id, std::string status, std::string created_at, std::string updated_at); | ||
|
||
std::string get_id(); | ||
std::string get_sender_id(); | ||
std::string get_receiver_id(); | ||
std::string get_service_id(); | ||
std::string get_status(); | ||
std::string get_id() const; | ||
std::string get_sender_id() const; | ||
std::string get_service_id() const; | ||
std::string get_status() const; | ||
std::string get_created_at() const; | ||
std::string get_updated_at() const; | ||
|
||
static std::unique_ptr<NotificationModel> create_notification(pqxx::connection& db, const std::string& sender_id, const std::string& service_id, const std::string& status = NotificationStatus::PENDING, bool throw_when_null = false); | ||
|
||
// * if the requester has already requested the service before, it returns true, otherwise false | ||
static bool is_requested(pqxx::connection& db, const std::string& sender_id); | ||
|
||
static std::unique_ptr<NotificationModel> handle_notification_status(pqxx::connection& db, const std::string& status, const std::string& notification_id, bool throw_when_null = false); | ||
|
||
static bool refused_notifications(pqxx::connection& db, const std::string& service_id, const std::string& notification_id); | ||
|
||
static std::unique_ptr<NotificationModel> get_notification_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <controllers/notification_controller.h> | ||
#include <crow.h> | ||
#include <db/connection_pool.h> | ||
#include <middlewares/verify_jwt.h> | ||
#include <db/connection_pool.h> | ||
#include <utils/common.h> | ||
#include <pqxx/pqxx> | ||
|
||
void initialize_notifications_routes(NebyApp& app); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
#pragma once | ||
|
||
#include <crow.h> | ||
#include <regex> | ||
|
||
bool validate_required_body_fields(const crow::json::rvalue &body, const std::vector<std::string> &required_fields, crow::response &res); | ||
|
||
bool isValidUUID(const std::string &uuid); |
Oops, something went wrong.