20#ifndef ORES_TRADING_MESSAGING_INSTRUMENT_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_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/instrument_service.hpp"
34namespace ores::trading::messaging {
37inline auto& instrument_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.trading.messaging.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 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(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::instrument_service svc(ctx);
68 get_instruments_response resp;
70 if (
auto req = decode<get_instruments_request>(msg)) {
72 static_cast<std::uint32_t
>(req->offset);
74 static_cast<std::uint32_t
>(req->limit);
75 resp.instruments = svc.list_instruments(offset, limit);
76 resp.total_available_count =
77 static_cast<int>(svc.count_instruments());
79 }
catch (
const std::exception& e) {
80 BOOST_LOG_SEV(instrument_handler_lg(), error)
81 << msg.
subject <<
" failed: " << e.what();
83 resp.message = e.what();
85 BOOST_LOG_SEV(instrument_handler_lg(), debug)
87 reply(nats_, msg, resp);
91 BOOST_LOG_SEV(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::instrument_service svc(ctx);
105 if (
auto req = decode<save_instrument_request>(msg)) {
107 svc.save_instrument(req->data, req->legs);
108 BOOST_LOG_SEV(instrument_handler_lg(), debug)
109 <<
"Completed " << msg.
subject;
110 reply(nats_, msg, save_instrument_response{.success =
true});
111 }
catch (
const std::exception& e) {
112 BOOST_LOG_SEV(instrument_handler_lg(), error)
113 << msg.
subject <<
" failed: " << e.what();
114 reply(nats_, msg, save_instrument_response{
115 .success =
false, .message = e.what()});
118 BOOST_LOG_SEV(instrument_handler_lg(), warn)
119 <<
"Failed to decode: " << msg.
subject;
124 BOOST_LOG_SEV(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::instrument_service svc(ctx);
138 if (
auto req = decode<delete_instrument_request>(msg)) {
140 for (
const auto&
id : req->ids)
141 svc.remove_instrument(id);
142 BOOST_LOG_SEV(instrument_handler_lg(), debug)
143 <<
"Completed " << msg.
subject;
144 reply(nats_, msg, delete_instrument_response{.success =
true});
145 }
catch (
const std::exception& e) {
146 BOOST_LOG_SEV(instrument_handler_lg(), error)
147 << msg.
subject <<
" failed: " << e.what();
148 reply(nats_, msg, delete_instrument_response{
149 .success =
false, .message = e.what()});
152 BOOST_LOG_SEV(instrument_handler_lg(), warn)
153 <<
"Failed to decode: " << msg.
subject;
158 BOOST_LOG_SEV(instrument_handler_lg(), debug)
160 auto ctx_expected = ores::service::service::make_request_context(
161 ctx_, msg, verifier_);
163 error_reply(nats_, msg, ctx_expected.error());
166 const auto& ctx = *ctx_expected;
167 service::instrument_service svc(ctx);
168 if (
auto req = decode<get_instrument_history_request>(msg)) {
170 auto versions = svc.get_instrument_history(req->id);
171 BOOST_LOG_SEV(instrument_handler_lg(), debug)
172 <<
"Completed " << msg.
subject;
173 reply(nats_, msg, get_instrument_history_response{
175 .history = std::move(versions)});
176 }
catch (
const std::exception& e) {
177 BOOST_LOG_SEV(instrument_handler_lg(), error)
178 << msg.
subject <<
" failed: " << e.what();
179 reply(nats_, msg, get_instrument_history_response{
180 .success =
false, .message = e.what()});
183 BOOST_LOG_SEV(instrument_handler_lg(), warn)
184 <<
"Failed to decode: " << msg.
subject;
189 BOOST_LOG_SEV(instrument_handler_lg(), debug)
191 auto ctx_expected = ores::service::service::make_request_context(
192 ctx_, msg, verifier_);
194 error_reply(nats_, msg, ctx_expected.error());
197 const auto& ctx = *ctx_expected;
198 service::instrument_service svc(ctx);
199 get_swap_legs_response resp;
201 if (
auto req = decode<get_swap_legs_request>(msg)) {
202 resp.legs = svc.get_legs(req->instrument_id);
204 }
catch (
const std::exception& e) {
205 BOOST_LOG_SEV(instrument_handler_lg(), error)
206 << msg.
subject <<
" failed: " << e.what();
207 resp.success =
false;
208 resp.message = e.what();
210 BOOST_LOG_SEV(instrument_handler_lg(), debug)
211 <<
"Completed " << msg.
subject;
212 reply(nats_, msg, resp);
218 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