ORE Studio 0.0.4
Loading...
Searching...
No Matches
counterparty_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_COUNTERPARTY_HANDLER_HPP
22
23#include <optional>
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_protocol.hpp"
33#include "ores.refdata.core/service/counterparty_service.hpp"
34
35namespace ores::refdata::messaging {
36
37namespace {
38inline auto& counterparty_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.counterparty_handler");
41 return instance;
42}
43} // namespace
44
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;
50using namespace ores::logging;
51
52class counterparty_handler {
53public:
54 counterparty_handler(ores::nats::service::client& nats,
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
58
59 void list(ores::nats::message msg) {
60 [[maybe_unused]] const auto correlation_id =
61 log_handler_entry(counterparty_handler_lg(), msg);
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
64 if (!ctx_expected) {
65 error_reply(nats_, msg, ctx_expected.error());
66 return;
67 }
68 const auto& ctx = *ctx_expected;
69 service::counterparty_service svc(ctx);
70 get_counterparties_response resp;
71 auto req = decode<get_counterparties_request>(msg);
72 if (!req) {
73 BOOST_LOG_SEV(counterparty_handler_lg(), warn)
74 << "Failed to decode: " << msg.subject;
75 reply(nats_, msg, resp);
76 return;
77 }
78 try {
79 resp.counterparties = svc.list_counterparties(
80 static_cast<std::uint32_t>(req->offset),
81 static_cast<std::uint32_t>(req->limit));
82 resp.total_available_count =
83 static_cast<int>(svc.count_counterparties());
84 BOOST_LOG_SEV(counterparty_handler_lg(), debug)
85 << "Completed " << msg.subject;
86 } catch (const std::exception& e) {
87 BOOST_LOG_SEV(counterparty_handler_lg(), error)
88 << msg.subject << " failed: " << e.what();
89 }
90 reply(nats_, msg, resp);
91 }
92
93 void save(ores::nats::message msg) {
94 [[maybe_unused]] const auto correlation_id =
95 log_handler_entry(counterparty_handler_lg(), msg);
96 auto ctx_expected = ores::service::service::make_request_context(
97 ctx_, msg, verifier_);
98 if (!ctx_expected) {
99 error_reply(nats_, msg, ctx_expected.error());
100 return;
101 }
102 const auto& ctx = *ctx_expected;
103 if (!has_permission(ctx, "refdata::counterparties:write")) {
104 error_reply(nats_, msg, ores::service::error_code::forbidden);
105 return;
106 }
107 service::counterparty_service svc(ctx);
108 auto req = decode<save_counterparty_request>(msg);
109 if (!req) {
110 BOOST_LOG_SEV(counterparty_handler_lg(), warn)
111 << "Failed to decode: " << msg.subject;
112 return;
113 }
114 try {
115 svc.save_counterparty(req->data);
116 BOOST_LOG_SEV(counterparty_handler_lg(), debug)
117 << "Completed " << msg.subject;
118 reply(nats_, msg,
119 save_counterparty_response{.success = true});
120 } catch (const std::exception& e) {
121 BOOST_LOG_SEV(counterparty_handler_lg(), error)
122 << msg.subject << " failed: " << e.what();
123 reply(nats_, msg, save_counterparty_response{
124 .success = false, .message = e.what()});
125 }
126 }
127
128 void remove(ores::nats::message msg) {
129 [[maybe_unused]] const auto correlation_id =
130 log_handler_entry(counterparty_handler_lg(), msg);
131 auto ctx_expected = ores::service::service::make_request_context(
132 ctx_, msg, verifier_);
133 if (!ctx_expected) {
134 error_reply(nats_, msg, ctx_expected.error());
135 return;
136 }
137 const auto& ctx = *ctx_expected;
138 if (!has_permission(ctx, "refdata::counterparties:delete")) {
139 error_reply(nats_, msg, ores::service::error_code::forbidden);
140 return;
141 }
142 service::counterparty_service svc(ctx);
143 auto req = decode<delete_counterparty_request>(msg);
144 if (!req) {
145 BOOST_LOG_SEV(counterparty_handler_lg(), warn)
146 << "Failed to decode: " << msg.subject;
147 return;
148 }
149 try {
150 boost::uuids::string_generator gen;
151 for (const auto& id_str : req->ids)
152 svc.remove_counterparty(gen(id_str));
153 BOOST_LOG_SEV(counterparty_handler_lg(), debug)
154 << "Completed " << msg.subject;
155 reply(nats_, msg,
156 delete_counterparty_response{.success = true});
157 } catch (const std::exception& e) {
158 BOOST_LOG_SEV(counterparty_handler_lg(), error)
159 << msg.subject << " failed: " << e.what();
160 reply(nats_, msg, delete_counterparty_response{
161 .success = false, .message = e.what()});
162 }
163 }
164
165 void history(ores::nats::message msg) {
166 [[maybe_unused]] const auto correlation_id =
167 log_handler_entry(counterparty_handler_lg(), msg);
168 auto ctx_expected = ores::service::service::make_request_context(
169 ctx_, msg, verifier_);
170 if (!ctx_expected) {
171 error_reply(nats_, msg, ctx_expected.error());
172 return;
173 }
174 const auto& ctx = *ctx_expected;
175 service::counterparty_service svc(ctx);
176 auto req = decode<get_counterparty_history_request>(msg);
177 if (!req) {
178 BOOST_LOG_SEV(counterparty_handler_lg(), warn)
179 << "Failed to decode: " << msg.subject;
180 return;
181 }
182 try {
183 boost::uuids::string_generator gen;
184 auto h = svc.get_counterparty_history(gen(req->id));
185 BOOST_LOG_SEV(counterparty_handler_lg(), debug)
186 << "Completed " << msg.subject;
187 reply(nats_, msg, get_counterparty_history_response{
188 .success = true, .history = std::move(h)});
189 } catch (const std::exception& e) {
190 BOOST_LOG_SEV(counterparty_handler_lg(), error)
191 << msg.subject << " failed: " << e.what();
192 reply(nats_, msg, get_counterparty_history_response{
193 .success = false, .message = e.what()});
194 }
195 }
196
197private:
200 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
201};
202
203} // namespace ores::refdata::messaging
204#endif
STL namespace.
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