20#ifndef ORES_REFDATA_CORE_MESSAGING_PARTY_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PARTY_HANDLER_HPP
24#include <boost/uuid/string_generator.hpp>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.refdata.api/messaging/party_protocol.hpp"
33#include "ores.refdata.core/service/party_service.hpp"
35namespace ores::refdata::messaging {
38inline auto& party_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.party_handler");
45using ores::service::messaging::reply;
46using ores::service::messaging::decode;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::has_permission;
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 BOOST_LOG_SEV(party_handler_lg(), debug)
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
64 error_reply(nats_, msg, ctx_expected.error());
67 const auto& ctx = *ctx_expected;
68 service::party_service svc(ctx);
69 get_parties_response resp;
70 auto req = decode<get_parties_request>(msg);
72 BOOST_LOG_SEV(party_handler_lg(), warn)
73 <<
"Failed to decode: " << msg.
subject;
74 reply(nats_, msg, resp);
78 resp.parties = svc.list_parties(
79 static_cast<std::uint32_t
>(req->offset),
80 static_cast<std::uint32_t
>(req->limit));
81 resp.total_available_count =
82 static_cast<int>(svc.count_parties());
83 BOOST_LOG_SEV(party_handler_lg(), debug)
85 }
catch (
const std::exception& e) {
86 BOOST_LOG_SEV(party_handler_lg(), error)
87 << msg.
subject <<
" failed: " << e.what();
89 reply(nats_, msg, resp);
93 BOOST_LOG_SEV(party_handler_lg(), debug)
95 auto ctx_expected = ores::service::service::make_request_context(
96 ctx_, msg, verifier_);
98 error_reply(nats_, msg, ctx_expected.error());
101 const auto& ctx = *ctx_expected;
102 if (!has_permission(ctx,
"refdata::parties:write")) {
106 service::party_service svc(ctx);
107 auto req = decode<save_party_request>(msg);
109 BOOST_LOG_SEV(party_handler_lg(), warn)
110 <<
"Failed to decode: " << msg.
subject;
114 svc.save_party(req->data);
115 BOOST_LOG_SEV(party_handler_lg(), debug)
116 <<
"Completed " << msg.
subject;
117 reply(nats_, msg, save_party_response{.success =
true});
118 }
catch (
const std::exception& e) {
119 BOOST_LOG_SEV(party_handler_lg(), error)
120 << msg.
subject <<
" failed: " << e.what();
121 reply(nats_, msg, save_party_response{
122 .success =
false, .message = e.what()});
127 BOOST_LOG_SEV(party_handler_lg(), debug)
129 auto ctx_expected = ores::service::service::make_request_context(
130 ctx_, msg, verifier_);
132 error_reply(nats_, msg, ctx_expected.error());
135 const auto& ctx = *ctx_expected;
136 if (!has_permission(ctx,
"refdata::parties:delete")) {
140 service::party_service svc(ctx);
141 auto req = decode<delete_party_request>(msg);
143 BOOST_LOG_SEV(party_handler_lg(), warn)
144 <<
"Failed to decode: " << msg.
subject;
148 boost::uuids::string_generator gen;
149 for (
const auto& id_str : req->ids)
150 svc.remove_party(gen(id_str));
151 BOOST_LOG_SEV(party_handler_lg(), debug)
152 <<
"Completed " << msg.
subject;
153 reply(nats_, msg, delete_party_response{.success =
true});
154 }
catch (
const std::exception& e) {
155 BOOST_LOG_SEV(party_handler_lg(), error)
156 << msg.
subject <<
" failed: " << e.what();
157 reply(nats_, msg, delete_party_response{
158 .success =
false, .message = e.what()});
163 BOOST_LOG_SEV(party_handler_lg(), debug)
165 auto ctx_expected = ores::service::service::make_request_context(
166 ctx_, msg, verifier_);
168 error_reply(nats_, msg, ctx_expected.error());
171 const auto& ctx = *ctx_expected;
172 service::party_service svc(ctx);
173 auto req = decode<get_party_history_request>(msg);
175 BOOST_LOG_SEV(party_handler_lg(), warn)
176 <<
"Failed to decode: " << msg.
subject;
180 boost::uuids::string_generator gen;
181 auto h = svc.get_party_history(gen(req->id));
182 BOOST_LOG_SEV(party_handler_lg(), debug)
183 <<
"Completed " << msg.
subject;
184 reply(nats_, msg, get_party_history_response{
185 .success =
true, .history = std::move(h)});
186 }
catch (
const std::exception& e) {
187 BOOST_LOG_SEV(party_handler_lg(), error)
188 << msg.
subject <<
" failed: " << e.what();
189 reply(nats_, msg, get_party_history_response{
190 .success =
false, .message = e.what()});
197 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73