20#ifndef ORES_TRADING_MESSAGING_COMPOSITE_INSTRUMENT_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_COMPOSITE_INSTRUMENT_HANDLER_HPP
24#include "ores.logging/make_logger.hpp"
25#include "ores.nats/domain/message.hpp"
26#include "ores.nats/service/client.hpp"
27#include "ores.database/domain/context.hpp"
28#include "ores.security/jwt/jwt_authenticator.hpp"
29#include "ores.service/messaging/handler_helpers.hpp"
30#include "ores.service/service/request_context.hpp"
31#include "ores.trading.api/messaging/instrument_protocol.hpp"
32#include "ores.trading.core/service/composite_instrument_service.hpp"
34namespace ores::trading::messaging {
37inline auto& composite_instrument_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.trading.messaging.composite_instrument_handler");
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
50class composite_instrument_handler {
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
58 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
63 error_reply(nats_, msg, ctx_expected.error());
66 const auto& ctx = *ctx_expected;
67 service::composite_instrument_service svc(ctx);
68 get_composite_instruments_response resp;
70 if (
auto req = decode<get_composite_instruments_request>(msg)) {
72 static_cast<std::uint32_t
>(req->offset);
74 static_cast<std::uint32_t
>(req->limit);
75 resp.instruments = svc.list_composite_instruments(offset, limit);
76 resp.total_available_count =
77 static_cast<int>(svc.count_composite_instruments());
79 }
catch (
const std::exception& e) {
80 BOOST_LOG_SEV(composite_instrument_handler_lg(), error)
81 << msg.
subject <<
" failed: " << e.what();
83 resp.message = e.what();
85 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
87 reply(nats_, msg, resp);
91 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
93 auto ctx_expected = ores::service::service::make_request_context(
94 ctx_, msg, verifier_);
96 error_reply(nats_, msg, ctx_expected.error());
99 const auto& ctx = *ctx_expected;
100 if (!has_permission(ctx,
"trading::instruments:write")) {
104 service::composite_instrument_service svc(ctx);
105 if (
auto req = decode<save_composite_instrument_request>(msg)) {
107 svc.save_composite_instrument(req->data, req->legs);
108 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
109 <<
"Completed " << msg.
subject;
110 reply(nats_, msg, save_composite_instrument_response{.success =
true});
111 }
catch (
const std::exception& e) {
112 BOOST_LOG_SEV(composite_instrument_handler_lg(), error)
113 << msg.
subject <<
" failed: " << e.what();
114 reply(nats_, msg, save_composite_instrument_response{
115 .success =
false, .message = e.what()});
118 BOOST_LOG_SEV(composite_instrument_handler_lg(), warn)
119 <<
"Failed to decode: " << msg.
subject;
124 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
126 auto ctx_expected = ores::service::service::make_request_context(
127 ctx_, msg, verifier_);
129 error_reply(nats_, msg, ctx_expected.error());
132 const auto& ctx = *ctx_expected;
133 if (!has_permission(ctx,
"trading::instruments:delete")) {
137 service::composite_instrument_service svc(ctx);
138 if (
auto req = decode<delete_composite_instrument_request>(msg)) {
139 delete_composite_instrument_response resp;
141 for (
const auto&
id : req->ids) {
143 svc.remove_composite_instrument(
id);
144 resp.results.push_back({id, {
true,
""}});
145 }
catch (
const std::exception& e) {
146 BOOST_LOG_SEV(composite_instrument_handler_lg(), error)
147 <<
"Failed to delete composite_instrument " <<
id
149 resp.results.push_back({id, {
false, e.what()}});
150 resp.success =
false;
151 resp.message =
"One or more deletions failed";
154 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
155 <<
"Completed " << msg.
subject;
156 reply(nats_, msg, resp);
158 BOOST_LOG_SEV(composite_instrument_handler_lg(), warn)
159 <<
"Failed to decode: " << msg.
subject;
164 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
166 auto ctx_expected = ores::service::service::make_request_context(
167 ctx_, msg, verifier_);
169 error_reply(nats_, msg, ctx_expected.error());
172 const auto& ctx = *ctx_expected;
173 service::composite_instrument_service svc(ctx);
174 if (
auto req = decode<get_composite_instrument_legs_request>(msg)) {
176 auto legs = svc.get_legs(req->instrument_id);
177 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
178 <<
"Completed " << msg.
subject;
179 reply(nats_, msg, get_composite_instrument_legs_response{
180 .legs = std::move(legs), .success =
true});
181 }
catch (
const std::exception& e) {
182 BOOST_LOG_SEV(composite_instrument_handler_lg(), error)
183 << msg.
subject <<
" failed: " << e.what();
184 reply(nats_, msg, get_composite_instrument_legs_response{
185 .success =
false, .message = e.what()});
188 BOOST_LOG_SEV(composite_instrument_handler_lg(), warn)
189 <<
"Failed to decode: " << msg.
subject;
194 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
196 auto ctx_expected = ores::service::service::make_request_context(
197 ctx_, msg, verifier_);
199 error_reply(nats_, msg, ctx_expected.error());
202 const auto& ctx = *ctx_expected;
203 service::composite_instrument_service svc(ctx);
204 if (
auto req = decode<get_composite_instrument_history_request>(msg)) {
206 auto versions = svc.get_composite_instrument_history(req->id);
207 BOOST_LOG_SEV(composite_instrument_handler_lg(), debug)
208 <<
"Completed " << msg.
subject;
209 reply(nats_, msg, get_composite_instrument_history_response{
211 .history = std::move(versions)});
212 }
catch (
const std::exception& e) {
213 BOOST_LOG_SEV(composite_instrument_handler_lg(), error)
214 << msg.
subject <<
" failed: " << e.what();
215 reply(nats_, msg, get_composite_instrument_history_response{
216 .success =
false, .message = e.what()});
219 BOOST_LOG_SEV(composite_instrument_handler_lg(), warn)
220 <<
"Failed to decode: " << msg.
subject;
227 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