ORE Studio 0.0.4
Loading...
Searching...
No Matches
equity_instrument_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_TRADING_MESSAGING_EQUITY_INSTRUMENT_HANDLER_HPP
21#define ORES_TRADING_MESSAGING_EQUITY_INSTRUMENT_HANDLER_HPP
22
23#include <optional>
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/equity_instrument_service.hpp"
33
34namespace ores::trading::messaging {
35
36namespace {
37inline auto& equity_instrument_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.trading.messaging.equity_instrument_handler");
40 return instance;
41}
42} // namespace
43
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
48using namespace ores::logging;
49
50class equity_instrument_handler {
51public:
52 equity_instrument_handler(ores::nats::service::client& nats,
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
56
57 void list(ores::nats::message msg) {
58 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
59 << "Handling " << msg.subject;
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
62 if (!ctx_expected) {
63 error_reply(nats_, msg, ctx_expected.error());
64 return;
65 }
66 const auto& ctx = *ctx_expected;
67 service::equity_instrument_service svc(ctx);
68 get_equity_instruments_response resp;
69 try {
70 if (auto req = decode<get_equity_instruments_request>(msg)) {
71 const auto offset =
72 static_cast<std::uint32_t>(req->offset);
73 const auto limit =
74 static_cast<std::uint32_t>(req->limit);
75 resp.instruments = svc.list_equity_instruments(offset, limit);
76 resp.total_available_count =
77 static_cast<int>(svc.count_equity_instruments());
78 }
79 } catch (const std::exception& e) {
80 BOOST_LOG_SEV(equity_instrument_handler_lg(), error)
81 << msg.subject << " failed: " << e.what();
82 resp.success = false;
83 resp.message = e.what();
84 }
85 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
86 << "Completed " << msg.subject;
87 reply(nats_, msg, resp);
88 }
89
90 void save(ores::nats::message msg) {
91 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
92 << "Handling " << msg.subject;
93 auto ctx_expected = ores::service::service::make_request_context(
94 ctx_, msg, verifier_);
95 if (!ctx_expected) {
96 error_reply(nats_, msg, ctx_expected.error());
97 return;
98 }
99 const auto& ctx = *ctx_expected;
100 if (!has_permission(ctx, "trading::instruments:write")) {
101 error_reply(nats_, msg, ores::service::error_code::forbidden);
102 return;
103 }
104 service::equity_instrument_service svc(ctx);
105 if (auto req = decode<save_equity_instrument_request>(msg)) {
106 try {
107 svc.save_equity_instrument(req->data);
108 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
109 << "Completed " << msg.subject;
110 reply(nats_, msg, save_equity_instrument_response{.success = true});
111 } catch (const std::exception& e) {
112 BOOST_LOG_SEV(equity_instrument_handler_lg(), error)
113 << msg.subject << " failed: " << e.what();
114 reply(nats_, msg, save_equity_instrument_response{
115 .success = false, .message = e.what()});
116 }
117 } else {
118 BOOST_LOG_SEV(equity_instrument_handler_lg(), warn)
119 << "Failed to decode: " << msg.subject;
120 }
121 }
122
123 void remove(ores::nats::message msg) {
124 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
125 << "Handling " << msg.subject;
126 auto ctx_expected = ores::service::service::make_request_context(
127 ctx_, msg, verifier_);
128 if (!ctx_expected) {
129 error_reply(nats_, msg, ctx_expected.error());
130 return;
131 }
132 const auto& ctx = *ctx_expected;
133 if (!has_permission(ctx, "trading::instruments:delete")) {
134 error_reply(nats_, msg, ores::service::error_code::forbidden);
135 return;
136 }
137 service::equity_instrument_service svc(ctx);
138 if (auto req = decode<delete_equity_instrument_request>(msg)) {
139 delete_equity_instrument_response resp;
140 resp.success = true;
141 for (const auto& id : req->ids) {
142 try {
143 svc.remove_equity_instrument(id);
144 resp.results.push_back({id, {true, ""}});
145 } catch (const std::exception& e) {
146 BOOST_LOG_SEV(equity_instrument_handler_lg(), error)
147 << "Failed to delete equity_instrument " << id
148 << ": " << e.what();
149 resp.results.push_back({id, {false, e.what()}});
150 resp.success = false;
151 resp.message = "One or more deletions failed";
152 }
153 }
154 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
155 << "Completed " << msg.subject;
156 reply(nats_, msg, resp);
157 } else {
158 BOOST_LOG_SEV(equity_instrument_handler_lg(), warn)
159 << "Failed to decode: " << msg.subject;
160 }
161 }
162
163 void history(ores::nats::message msg) {
164 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
165 << "Handling " << msg.subject;
166 auto ctx_expected = ores::service::service::make_request_context(
167 ctx_, msg, verifier_);
168 if (!ctx_expected) {
169 error_reply(nats_, msg, ctx_expected.error());
170 return;
171 }
172 const auto& ctx = *ctx_expected;
173 service::equity_instrument_service svc(ctx);
174 if (auto req = decode<get_equity_instrument_history_request>(msg)) {
175 try {
176 auto versions = svc.get_equity_instrument_history(req->id);
177 BOOST_LOG_SEV(equity_instrument_handler_lg(), debug)
178 << "Completed " << msg.subject;
179 reply(nats_, msg, get_equity_instrument_history_response{
180 .success = true,
181 .history = std::move(versions)});
182 } catch (const std::exception& e) {
183 BOOST_LOG_SEV(equity_instrument_handler_lg(), error)
184 << msg.subject << " failed: " << e.what();
185 reply(nats_, msg, get_equity_instrument_history_response{
186 .success = false, .message = e.what()});
187 }
188 } else {
189 BOOST_LOG_SEV(equity_instrument_handler_lg(), warn)
190 << "Failed to decode: " << msg.subject;
191 }
192 }
193
194private:
197 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
198};
199
200} // namespace ores::trading::messaging
201
202#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