20#ifndef ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_CONTACT_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_CONTACT_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_contact_information_protocol.hpp"
33#include "ores.refdata.core/service/counterparty_contact_information_service.hpp"
35namespace ores::refdata::messaging {
38inline auto& counterparty_contact_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.counterparty_contact_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_contact_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_contact_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_contact_information_service svc(ctx);
71 decode<get_counterparty_contact_informations_request>(msg);
73 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
74 <<
"Failed to decode: " << msg.
subject;
78 boost::uuids::string_generator gen;
80 svc.list_counterparty_contact_informations_by_counterparty(
81 gen(req->counterparty_id));
82 get_counterparty_contact_informations_response resp;
83 resp.contact_informations = std::move(items);
84 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
86 reply(nats_, msg, resp);
87 }
catch (
const std::exception& e) {
88 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
89 << msg.
subject <<
" failed: " << e.what();
91 get_counterparty_contact_informations_response{});
96 [[maybe_unused]]
const auto correlation_id =
97 log_handler_entry(counterparty_contact_handler_lg(), msg);
98 auto ctx_expected = ores::service::service::make_request_context(
99 ctx_, msg, verifier_);
101 error_reply(nats_, msg, ctx_expected.error());
104 const auto& ctx = *ctx_expected;
105 if (!has_permission(ctx,
"refdata::counterparty_contacts:write")) {
109 service::counterparty_contact_information_service svc(ctx);
111 decode<save_counterparty_contact_information_request>(msg);
113 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
114 <<
"Failed to decode: " << msg.
subject;
118 svc.save_counterparty_contact_information(req->data);
119 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
120 <<
"Completed " << msg.
subject;
122 save_counterparty_contact_information_response{
124 }
catch (
const std::exception& e) {
125 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
126 << msg.
subject <<
" failed: " << e.what();
128 save_counterparty_contact_information_response{
129 .success =
false, .message = e.what()});
134 [[maybe_unused]]
const auto correlation_id =
135 log_handler_entry(counterparty_contact_handler_lg(), msg);
136 auto ctx_expected = ores::service::service::make_request_context(
137 ctx_, msg, verifier_);
139 error_reply(nats_, msg, ctx_expected.error());
142 const auto& ctx = *ctx_expected;
143 if (!has_permission(ctx,
"refdata::counterparty_contacts:delete")) {
147 service::counterparty_contact_information_service svc(ctx);
149 decode<delete_counterparty_contact_information_request>(msg);
151 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
152 <<
"Failed to decode: " << msg.
subject;
156 boost::uuids::string_generator gen;
157 for (
const auto& id_str : req->ids)
158 svc.remove_counterparty_contact_information(gen(id_str));
159 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
160 <<
"Completed " << msg.
subject;
162 delete_counterparty_contact_information_response{
164 }
catch (
const std::exception& e) {
165 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
166 << msg.
subject <<
" failed: " << e.what();
168 delete_counterparty_contact_information_response{
169 .success =
false, .message = e.what()});
176 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