Skip to content

Commit

Permalink
reimplementation of permission fetch query.
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3mro committed Dec 17, 2024
1 parent 5f9d495 commit 1de3eb9
Show file tree
Hide file tree
Showing 39 changed files with 807 additions and 365 deletions.
10 changes: 10 additions & 0 deletions Todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@
* Implement middleware for appointment search by user_id or entity_id.
* Implement changepassword, lostpassword.
ps aux | awk '{print $6/1024 " MB\t\t" $11}' | sort -n


* add patient -> set time and date for visit schedule ..
* visit return all data ..
* endpoints for recent visits [{name,age,sex,photo}]
* endpoints for queue visits [{name,age,sex,photo}]
* agenda by date

* array for clinic patients, name, id only
* number for clinic patients
4 changes: 2 additions & 2 deletions src/api/v2/routes/clinics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace api
void GetVisits(const drogon::HttpRequestPtr &req, std::function<void(const drogon::HttpResponsePtr &)> &&callback)
{
// auto ctx = createContext(req, Context::Type::READ, "patients");
executeControllerMethod(clinicRegistry, "patients", &ClinicControllerBase::GetVisits, req, std::move(callback),
stoll(req->getParameter("patient_id")), stoll(req->getParameter("clinic_id")));
executeControllerMethod(
clinicRegistry, "patients", &ClinicControllerBase::GetVisits, req, std::move(callback), stoll(req->getParameter("patient_id")));
}

METHOD_LIST_BEGIN
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/clientcontroller/clientcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void ClientController<T>::Create(CALLBACK_&& callback, [[maybe_unused]] const Re
return;
}

if (!gateKeeper->canCreate<T>(requester, T::getTableName(), json_data, error))
if (!gateKeeper->canCreate<T>(requester, json_data, error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -63,7 +63,7 @@ void ClientController<T>::Update(CALLBACK_&& callback, const Requester&& request
if (success)
{
T client(client_data);
if (!gateKeeper->canUpdate<T>(requester, T::getTableName(), id.value(), error))
if (!gateKeeper->canUpdate<T>(requester, id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -123,7 +123,7 @@ void ClientController<T>::Suspend(CALLBACK_&& callback, const Requester&& reques
T client(suspendData);
Http::Error error;

if (!gateKeeper->canToggleActive<T>(requester, T::getTableName(), client_id.value(), error))
if (!gateKeeper->canToggleActive<T>(requester, client_id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -153,7 +153,7 @@ void ClientController<T>::Activate(CALLBACK_&& callback, const Requester&& reque
T client(suspendData);

Http::Error error;
if (!gateKeeper->canToggleActive<T>(requester, T::getTableName(), client_id.value(), error))
if (!gateKeeper->canToggleActive<T>(requester, client_id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -188,7 +188,7 @@ void ClientController<T>::GetServices(CALLBACK_&& callback, const Requester&& re
T client(Types::Data_t(client_id.value()));

Http::Error error;
if (!gateKeeper->canGetServices<T>(requester, T::getTableName(), client_id.value(), error))
if (!gateKeeper->canGetServices<T>(requester, client_id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down
116 changes: 17 additions & 99 deletions src/controllers/cliniccontroller/cliniccontroller.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "controllers/cliniccontroller/cliniccontroller.hpp"

#include "controllers/entitycontroller/entitycontroller.hpp"

template <typename T>
template <typename U>
void ClinicController<T>::CreateImpl(CALLBACK_ &&callback, const Requester &&requester, std::string_view data)
Expand Down Expand Up @@ -27,16 +29,16 @@ void ClinicController<T>::CreateImpl(CALLBACK_ &&callback, const Requester &&req
return;
}

Types::Create_t clinic_create_data = Types::Create_t(request_json, id.value());

T entity(clinic_create_data);

if (!gateKeeper->canCreate<T>(requester, T::getTableName(), request_json, error))
if (!gateKeeper->canCreate<T>(requester, request_json, error))
{
callback(error.code, error.message);
return;
}

Types::Create_t clinic_create_data = Types::Create_t(request_json, id.value());

T entity(clinic_create_data);

Controller::Create(entity, std::move(callback));
}
catch (const std::exception &e)
Expand Down Expand Up @@ -88,8 +90,7 @@ void ClinicController<T>::SearchImpl(CALLBACK_ &&callback, [[maybe_unused]] cons

template <typename T>
template <typename U>
void ClinicController<T>::GetVisitsImpl(
CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id)
void ClinicController<T>::GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id)
requires(std::is_same<U, Patient>::value)
{
Http::Error error;
Expand All @@ -102,7 +103,7 @@ void ClinicController<T>::GetVisitsImpl(
}
T entity((Types::Data_t(patient_id.value())));

if (!gateKeeper->canRead<T>(requester, T::getTableName(), clinic_id.value(), error))
if (!gateKeeper->canRead<T>(requester, patient_id.value(), error))
{
callback(error.code, error.message);
return;
Expand All @@ -118,8 +119,8 @@ void ClinicController<T>::GetVisitsImpl(

template <typename T>
template <typename U>
void ClinicController<T>::GetVisitsImpl(CALLBACK_ &&callback, [[maybe_unused]] const Requester &&requester,
[[maybe_unused]] const std::optional<uint64_t> patient_id, [[maybe_unused]] const std::optional<uint64_t> clinic_id)
void ClinicController<T>::GetVisitsImpl(
CALLBACK_ &&callback, [[maybe_unused]] const Requester &&requester, [[maybe_unused]] const std::optional<uint64_t> patient_id)
requires(!std::is_same<U, Patient>::value)
{
callback(api::v2::Http::Status::BAD_REQUEST, fmt::format("GetVisit is NOT implemented for entity type {}", T::getTableName()));
Expand All @@ -134,93 +135,13 @@ void ClinicController<T>::Create(CALLBACK_ &&callback, const Requester &&request
template <typename T>
void ClinicController<T>::Read(CALLBACK_ &&callback, const Requester &&requester, std::string_view data)
{
try
{
jsoncons::json request_j = jsoncons::json::parse(data);
uint64_t id = request_j.at("id").as<uint64_t>();
uint64_t clinic_id = request_j.at("clinic_id").as<uint64_t>();
Http::Error error;

if (!gateKeeper->canRead<T>(requester, T::getTableName(), clinic_id, error))
{
callback(error.code, error.message);
return;
}

std::unordered_set<std::string> schema = request_j.at("schema").as<std::unordered_set<std::string>>();

Validator::Rule rule(Validator::Rule::Action::ASSERT_NOT_PRESENT, {"id", "username", "password", "created_at", "updated_at"});

if (!Validator::validateDatabaseReadSchema(schema, std::format("{}_safe", T::getTableName()), error, rule))
{
callback(error.code, fmt::format("Failed to validate request body, {}.", error.message));
return;
}

T entity((Types::Read_t(schema, id)));

Controller::Read(entity, std::move(callback));
}
catch (const std::exception &e)
{
CRITICALMESSAGERESPONSE
}
EntityController<T>::Read(std::move(callback), std::move(requester), data);
}

template <typename T>
void ClinicController<T>::Update(CALLBACK_ &&callback, const Requester &&requester, std::string_view data, const std::optional<uint64_t> id)
{
try
{
bool success = false;
api::v2::Http::Error error;

if (!id.has_value())
{
callback(api::v2::Http::Status::BAD_REQUEST, "No id provided.");
return;
}

std::optional<jsoncons::json> request_json = jsoncons::json::parse(data);
std::optional<uint64_t> clinic_id = request_json->at("clinic_id").as<uint64_t>();

if (!clinic_id.has_value())
{
callback(api::v2::Http::Status::BAD_REQUEST, "No clinic id provided.");
return;
}

if (!request_json.has_value())
{
callback(api::v2::Http::Status::BAD_REQUEST, "Invalid request body.");
return;
}

Validator::Rule rule(Validator::Rule::Action::NONE, {});
success = Validator::validateDatabaseUpdateSchema(T::getTableName(), request_json, error, rule);

if (!success)
{
callback(error.code, fmt::format("Failed to validate request body, {}.", error.message));
return;
}

if (!gateKeeper->canUpdate<T>(requester, T::getTableName(), clinic_id.value(), error))
{
callback(error.code, error.message);
return;
}

Types::Update_t entity_data = Types::Update_t(request_json.value(), id.value());

T entity(entity_data);

Controller::Update(entity, std::move(callback));
}
catch (const std::exception &e)
{
CRITICALMESSAGERESPONSE
}
EntityController<T>::Update(std::move(callback), std::move(requester), data, id);
}

template <typename T>
Expand All @@ -236,25 +157,22 @@ void ClinicController<T>::Search(CALLBACK_ &&callback, const Requester &&request
}

template <typename T>
void ClinicController<T>::GetVisits(
CALLBACK_ &&callback, const Requester &&requester, const std::optional<uint64_t> patient_id, const std::optional<uint64_t> clinic_id)
void ClinicController<T>::GetVisits(CALLBACK_ &&callback, const Requester &&requester, const std::optional<uint64_t> patient_id)
{
GetVisitsImpl(std::move(callback), std::move(requester), patient_id, clinic_id);
GetVisitsImpl(std::move(callback), std::move(requester), patient_id);
}

#define INSTANTIATE_CLINIC_CONTROLLER(TYPE) \
template void ClinicController<TYPE>::CreateImpl(CALLBACK_ &&callback, const Requester &&requester, std::string_view data); \
template void ClinicController<TYPE>::DeleteImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> id); \
template void ClinicController<TYPE>::SearchImpl(CALLBACK_ &&callback, const Requester &&requester, std::string_view data); \
template void ClinicController<TYPE>::GetVisitsImpl( \
CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id); \
template void ClinicController<TYPE>::GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id); \
template void ClinicController<TYPE>::Create(CALLBACK_ &&callback, const Requester &&requester, std::string_view data); \
template void ClinicController<TYPE>::Read(CALLBACK_ &&callback, const Requester &&requester, std::string_view data); \
template void ClinicController<TYPE>::Update(CALLBACK_ &&callback, const Requester &&requester, std::string_view data, std::optional<uint64_t> id); \
template void ClinicController<TYPE>::Delete(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> id); \
template void ClinicController<TYPE>::Search(CALLBACK_ &&callback, const Requester &&requester, std::string_view data); \
template void ClinicController<TYPE>::GetVisits( \
CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id);
template void ClinicController<TYPE>::GetVisits(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id);

#include "gatekeeper/includes.hpp" // IWYU pragma: keep
// Instantiate for all entity types
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/cliniccontroller/cliniccontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class ClinicController : public EntityController<T>, public ClinicControllerBase
requires(std::is_same<U, Patient>::value);

template <typename U = T>
void GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id)
void GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id)
requires(std::is_same<U, Patient>::value);

template <typename U = T>
void GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id)
void GetVisitsImpl(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id)
requires(!std::is_same<U, Patient>::value);

public:
Expand All @@ -55,5 +55,5 @@ class ClinicController : public EntityController<T>, public ClinicControllerBase
void Update(CALLBACK_ &&callback, const Requester &&requester, std::string_view data, std::optional<uint64_t> id) final;
void Delete(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> id) final;
void Search(CALLBACK_ &&callback, const Requester &&requester, std::string_view data) final;
void GetVisits(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id) final;
void GetVisits(CALLBACK_ &&callback, const Requester &&requester, std::optional<uint64_t> patient_id) final;
};
12 changes: 6 additions & 6 deletions src/controllers/cliniccontroller/cliniccontrollerbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class ClinicControllerBase
virtual ~ClinicControllerBase() = default;

// CRUDS
virtual void Create(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void Read(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void Update(CALLBACK_&& callback, const Requester&& requester, std::string_view data, std::optional<uint64_t> id) = 0;
virtual void Delete(CALLBACK_&& callback, const Requester&& requester, std::optional<uint64_t> id) = 0;
virtual void Search(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void GetVisits(CALLBACK_&& callback, const Requester&& requester, std::optional<uint64_t> patient_id, std::optional<uint64_t> clinic_id) = 0;
virtual void Create(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void Read(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void Update(CALLBACK_&& callback, const Requester&& requester, std::string_view data, std::optional<uint64_t> id) = 0;
virtual void Delete(CALLBACK_&& callback, const Requester&& requester, std::optional<uint64_t> id) = 0;
virtual void Search(CALLBACK_&& callback, const Requester&& requester, std::string_view data) = 0;
virtual void GetVisits(CALLBACK_&& callback, const Requester&& requester, std::optional<uint64_t> patient_id) = 0;
};
6 changes: 1 addition & 5 deletions src/controllers/databasecontroller/databasecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,4 @@ std::optional<std::unordered_set<api::v2::ColumnInfo>> DatabaseController::getTa

std::optional<std::unordered_set<std::string>> DatabaseController::getAllTables() { return executer<std::unordered_set<std::string>>(&Database::getAllTables); }

std::optional<jsoncons::json> DatabaseController::getServicePermissions(const std::string &service_name, uint64_t service_id)
{
std::string query = fmt::format("SELECT owner_id,admin_id,staff FROM {} WHERE id = '{}' LIMIT 1;", service_name, service_id);
return executer<jsoncons::json>(&Database::executeQuery<jsoncons::json, pqxx::nontransaction>, query);
}
// [ ] make entity return the query for getpermission and use T template here.
10 changes: 9 additions & 1 deletion src/controllers/databasecontroller/databasecontroller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
#include <cstdint>
#include <jsoncons/basic_json.hpp>
#include <jsoncons/json.hpp>
#include <optional>

#include "database/database.hpp"
#include "database/databaseconnectionpool.hpp"
#include "utils/message/message.hpp"
class Case;
class Service;
class Appointment;

class DatabaseController
{
Expand All @@ -23,7 +27,11 @@ class DatabaseController
const std::string &tablename); // check if user found and return 0 if not
std::optional<std::unordered_set<api::v2::ColumnInfo>> getTableSchema(const std::string &tableName);
std::optional<std::unordered_set<std::string>> getAllTables();
std::optional<jsoncons::json> getServicePermissions(const std::string &service_name, uint64_t service_id);

std::optional<jsoncons::json> getPermissions(const std::string &query)
{
return executer<jsoncons::json>(&Database::executeQuery<jsoncons::json, pqxx::nontransaction>, query);
}

private:
std::shared_ptr<DatabaseConnectionPool> databaseConnectionPool;
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/entitycontroller/entitycontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ inline void __attribute((always_inline)) EntityController<T>::Create(CALLBACK_ &
return;
}

if (!gateKeeper->canCreate<T>(requester, T::getTableName(), request_j, error))
if (!gateKeeper->canCreate<T>(requester, request_j, error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -59,7 +59,7 @@ inline void __attribute((always_inline)) EntityController<T>::Read(CALLBACK_ &&c
uint64_t id = request_j.at("id").as<uint64_t>();
Http::Error error;

if (!gateKeeper->canRead<T>(requester, T::getTableName(), id, error))
if (!gateKeeper->canRead<T>(requester, id, error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -116,7 +116,7 @@ inline void __attribute((always_inline)) EntityController<T>::Update(
return;
}

if (!gateKeeper->canUpdate<T>(requester, T::getTableName(), id.value(), error))
if (!gateKeeper->canUpdate<T>(requester, id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down Expand Up @@ -147,7 +147,7 @@ inline void __attribute((always_inline)) EntityController<T>::Delete(CALLBACK_ &

Http::Error error;

if (!gateKeeper->canDelete<T>(requester, T::getTableName(), id.value(), error))
if (!gateKeeper->canDelete<T>(requester, id.value(), error))
{
callback(error.code, error.message);
return;
Expand Down
Loading

0 comments on commit 1de3eb9

Please sign in to comment.