20#ifndef ORES_TRADING_MESSAGING_TRADE_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_TRADE_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.trading.api/messaging/trade_protocol.hpp"
33#include "ores.trading.core/service/activity_type_service.hpp"
34#include "ores.trading.core/service/trade_service.hpp"
35#include "ores.utility/uuid/tenant_id.hpp"
37namespace ores::trading::messaging {
40inline auto& trade_handler_lg() {
41 static auto instance = ores::logging::make_logger(
42 "ores.trading.messaging.trade_handler");
47using ores::service::messaging::reply;
48using ores::service::messaging::decode;
49using ores::service::messaging::error_reply;
50using ores::service::messaging::has_permission;
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
61 BOOST_LOG_SEV(trade_handler_lg(), debug)
63 auto req_ctx_expected = ores::service::service::make_request_context(
64 ctx_, msg, verifier_);
65 if (!req_ctx_expected) {
66 error_reply(nats_, msg, req_ctx_expected.error());
69 const auto& req_ctx = *req_ctx_expected;
72 const auto sys_ctx = req_ctx.with_tenant(
74 service::activity_type_service svc(sys_ctx);
75 get_activity_types_response resp;
77 resp.activity_types = svc.list_types();
79 BOOST_LOG_SEV(trade_handler_lg(), debug)
81 reply(nats_, msg, resp);
85 BOOST_LOG_SEV(trade_handler_lg(), debug)
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, verifier_);
90 error_reply(nats_, msg, ctx_expected.error());
93 const auto& ctx = *ctx_expected;
94 service::trade_service svc(ctx);
95 get_trades_response resp;
97 if (
auto req = decode<get_trades_request>(msg)) {
99 static_cast<std::uint32_t
>(req->offset);
101 static_cast<std::uint32_t
>(req->limit);
102 if (!req->book_id.empty()) {
103 boost::uuids::string_generator gen;
104 const auto book_uuid = gen(req->book_id);
105 resp.trades = svc.list_trades_filtered(
107 std::optional<boost::uuids::uuid>(book_uuid),
109 resp.total_available_count =
110 static_cast<int>(svc.count_trades_filtered(
111 std::optional<boost::uuids::uuid>(book_uuid),
114 resp.trades = svc.list_trades(offset, limit);
115 resp.total_available_count =
116 static_cast<int>(svc.count_trades());
120 BOOST_LOG_SEV(trade_handler_lg(), debug)
121 <<
"Completed " << msg.
subject;
122 reply(nats_, msg, resp);
126 BOOST_LOG_SEV(trade_handler_lg(), debug)
128 auto ctx_expected = ores::service::service::make_request_context(
129 ctx_, msg, verifier_);
131 error_reply(nats_, msg, ctx_expected.error());
134 const auto& ctx = *ctx_expected;
135 if (!has_permission(ctx,
"trading::trades:write")) {
139 service::trade_service svc(ctx);
140 if (
auto req = decode<save_trade_request>(msg)) {
142 svc.save_trades(req->trades);
143 BOOST_LOG_SEV(trade_handler_lg(), debug)
144 <<
"Completed " << msg.
subject;
145 reply(nats_, msg, save_trade_response{.success =
true});
146 }
catch (
const std::exception& e) {
147 BOOST_LOG_SEV(trade_handler_lg(), error)
148 << msg.
subject <<
" failed: " << e.what();
149 reply(nats_, msg, save_trade_response{
150 .success =
false, .message = e.what()});
153 BOOST_LOG_SEV(trade_handler_lg(), warn)
154 <<
"Failed to decode: " << msg.
subject;
159 BOOST_LOG_SEV(trade_handler_lg(), debug)
161 auto ctx_expected = ores::service::service::make_request_context(
162 ctx_, msg, verifier_);
164 error_reply(nats_, msg, ctx_expected.error());
167 const auto& ctx = *ctx_expected;
168 if (!has_permission(ctx,
"trading::trades:delete")) {
172 service::trade_service svc(ctx);
173 if (
auto req = decode<delete_trade_request>(msg)) {
175 for (
const auto&
id : req->ids)
176 svc.remove_trade(id);
177 BOOST_LOG_SEV(trade_handler_lg(), debug)
178 <<
"Completed " << msg.
subject;
179 reply(nats_, msg, delete_trade_response{.success =
true});
180 }
catch (
const std::exception& e) {
181 BOOST_LOG_SEV(trade_handler_lg(), error)
182 << msg.
subject <<
" failed: " << e.what();
183 reply(nats_, msg, delete_trade_response{
184 .success =
false, .message = e.what()});
187 BOOST_LOG_SEV(trade_handler_lg(), warn)
188 <<
"Failed to decode: " << msg.
subject;
193 BOOST_LOG_SEV(trade_handler_lg(), debug)
195 auto ctx_expected = ores::service::service::make_request_context(
196 ctx_, msg, verifier_);
198 error_reply(nats_, msg, ctx_expected.error());
201 const auto& ctx = *ctx_expected;
202 service::trade_service svc(ctx);
203 if (
auto req = decode<get_trade_history_request>(msg)) {
205 auto versions = svc.get_trade_history(req->id);
206 BOOST_LOG_SEV(trade_handler_lg(), debug)
207 <<
"Completed " << msg.
subject;
208 reply(nats_, msg, get_trade_history_response{
210 .versions = std::move(versions)});
211 }
catch (
const std::exception& e) {
212 BOOST_LOG_SEV(trade_handler_lg(), error)
213 << msg.
subject <<
" failed: " << e.what();
214 reply(nats_, msg, get_trade_history_response{
215 .success =
false, .message = e.what()});
218 BOOST_LOG_SEV(trade_handler_lg(), warn)
219 <<
"Failed to decode: " << msg.
subject;
226 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
static tenant_id system()
Creates a tenant_id representing the system tenant.
Definition tenant_id.cpp:41