20#ifndef ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_IDENTIFIER_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_IDENTIFIER_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/counterparty_identifier_protocol.hpp"
33#include "ores.refdata.core/service/counterparty_identifier_service.hpp"
35namespace ores::refdata::messaging {
38inline auto& counterparty_identifier_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.counterparty_identifier_handler");
45using ores::service::messaging::reply;
46using ores::service::messaging::decode;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::has_permission;
49using ores::service::messaging::log_handler_entry;
52class counterparty_identifier_handler {
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
60 [[maybe_unused]]
const auto correlation_id =
61 log_handler_entry(counterparty_identifier_handler_lg(), msg);
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
65 error_reply(nats_, msg, ctx_expected.error());
68 const auto& ctx = *ctx_expected;
69 service::counterparty_identifier_service svc(ctx);
70 auto req = decode<get_counterparty_identifiers_request>(msg);
72 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), warn)
73 <<
"Failed to decode: " << msg.
subject;
77 boost::uuids::string_generator gen;
79 svc.list_counterparty_identifiers_by_counterparty(
80 gen(req->counterparty_id));
81 get_counterparty_identifiers_response resp;
82 resp.identifiers = std::move(items);
83 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), debug)
85 reply(nats_, msg, resp);
86 }
catch (
const std::exception& e) {
87 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), error)
88 << msg.
subject <<
" failed: " << e.what();
89 reply(nats_, msg, get_counterparty_identifiers_response{});
94 [[maybe_unused]]
const auto correlation_id =
95 log_handler_entry(counterparty_identifier_handler_lg(), msg);
96 auto ctx_expected = ores::service::service::make_request_context(
97 ctx_, msg, verifier_);
99 error_reply(nats_, msg, ctx_expected.error());
102 const auto& ctx = *ctx_expected;
103 if (!has_permission(ctx,
"refdata::counterparty_identifiers:write")) {
107 service::counterparty_identifier_service svc(ctx);
108 auto req = decode<save_counterparty_identifier_request>(msg);
110 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), warn)
111 <<
"Failed to decode: " << msg.
subject;
115 svc.save_counterparty_identifier(req->data);
116 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), debug)
117 <<
"Completed " << msg.
subject;
119 save_counterparty_identifier_response{.success =
true});
120 }
catch (
const std::exception& e) {
121 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), error)
122 << msg.
subject <<
" failed: " << e.what();
123 reply(nats_, msg, save_counterparty_identifier_response{
124 .success =
false, .message = e.what()});
129 [[maybe_unused]]
const auto correlation_id =
130 log_handler_entry(counterparty_identifier_handler_lg(), msg);
131 auto ctx_expected = ores::service::service::make_request_context(
132 ctx_, msg, verifier_);
134 error_reply(nats_, msg, ctx_expected.error());
137 const auto& ctx = *ctx_expected;
138 if (!has_permission(ctx,
"refdata::counterparty_identifiers:delete")) {
142 service::counterparty_identifier_service svc(ctx);
143 auto req = decode<delete_counterparty_identifier_request>(msg);
145 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), warn)
146 <<
"Failed to decode: " << msg.
subject;
150 boost::uuids::string_generator gen;
151 for (
const auto& id_str : req->ids)
152 svc.remove_counterparty_identifier(gen(id_str));
153 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), debug)
154 <<
"Completed " << msg.
subject;
156 delete_counterparty_identifier_response{.success =
true});
157 }
catch (
const std::exception& e) {
158 BOOST_LOG_SEV(counterparty_identifier_handler_lg(), error)
159 << msg.
subject <<
" failed: " << e.what();
160 reply(nats_, msg, delete_counterparty_identifier_response{
161 .success =
false, .message = e.what()});
168 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