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;
51class counterparty_contact_handler {
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 BOOST_LOG_SEV(counterparty_contact_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::counterparty_contact_information_service svc(ctx);
70 decode<get_counterparty_contact_informations_request>(msg);
72 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
73 <<
"Failed to decode: " << msg.
subject;
77 boost::uuids::string_generator gen;
79 svc.list_counterparty_contact_informations_by_counterparty(
80 gen(req->counterparty_id));
81 get_counterparty_contact_informations_response resp;
82 resp.contact_informations = std::move(items);
83 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
85 reply(nats_, msg, resp);
86 }
catch (
const std::exception& e) {
87 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
88 << msg.
subject <<
" failed: " << e.what();
90 get_counterparty_contact_informations_response{});
95 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
97 auto ctx_expected = ores::service::service::make_request_context(
98 ctx_, msg, verifier_);
100 error_reply(nats_, msg, ctx_expected.error());
103 const auto& ctx = *ctx_expected;
104 if (!has_permission(ctx,
"refdata::counterparty_contacts:write")) {
108 service::counterparty_contact_information_service svc(ctx);
110 decode<save_counterparty_contact_information_request>(msg);
112 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
113 <<
"Failed to decode: " << msg.
subject;
117 svc.save_counterparty_contact_information(req->data);
118 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
119 <<
"Completed " << msg.
subject;
121 save_counterparty_contact_information_response{
123 }
catch (
const std::exception& e) {
124 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
125 << msg.
subject <<
" failed: " << e.what();
127 save_counterparty_contact_information_response{
128 .success =
false, .message = e.what()});
133 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
135 auto ctx_expected = ores::service::service::make_request_context(
136 ctx_, msg, verifier_);
138 error_reply(nats_, msg, ctx_expected.error());
141 const auto& ctx = *ctx_expected;
142 if (!has_permission(ctx,
"refdata::counterparty_contacts:delete")) {
146 service::counterparty_contact_information_service svc(ctx);
148 decode<delete_counterparty_contact_information_request>(msg);
150 BOOST_LOG_SEV(counterparty_contact_handler_lg(), warn)
151 <<
"Failed to decode: " << msg.
subject;
155 boost::uuids::string_generator gen;
156 for (
const auto& id_str : req->ids)
157 svc.remove_counterparty_contact_information(gen(id_str));
158 BOOST_LOG_SEV(counterparty_contact_handler_lg(), debug)
159 <<
"Completed " << msg.
subject;
161 delete_counterparty_contact_information_response{
163 }
catch (
const std::exception& e) {
164 BOOST_LOG_SEV(counterparty_contact_handler_lg(), error)
165 << msg.
subject <<
" failed: " << e.what();
167 delete_counterparty_contact_information_response{
168 .success =
false, .message = e.what()});
175 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