ORE Studio 0.0.4
Loading...
Searching...
No Matches
currency_market_tier_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_CURRENCY_MARKET_TIER_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_CURRENCY_MARKET_TIER_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.refdata.api/messaging/currency_market_tier_protocol.hpp"
32#include "ores.refdata.core/service/currency_market_tier_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& currency_market_tier_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.currency_market_tier_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 ores::service::messaging::log_handler_entry;
49using namespace ores::logging;
50
51class currency_market_tier_handler {
52public:
53 currency_market_tier_handler(ores::nats::service::client& nats,
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
57
58 void list(ores::nats::message msg) {
59 [[maybe_unused]] const auto correlation_id =
60 log_handler_entry(currency_market_tier_handler_lg(), msg);
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
63 if (!ctx_expected) {
64 error_reply(nats_, msg, ctx_expected.error());
65 return;
66 }
67 const auto& ctx = *ctx_expected;
68 service::currency_market_tier_service svc(ctx);
69 get_currency_market_tiers_response resp;
70 auto req = decode<get_currency_market_tiers_request>(msg);
71 if (!req) {
72 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
73 << "Failed to decode: " << msg.subject;
74 reply(nats_, msg, resp);
75 return;
76 }
77 try {
78 resp.currency_market_tiers = svc.list_types();
79 resp.total_available_count =
80 static_cast<int>(resp.currency_market_tiers.size());
81 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
82 << "Completed " << msg.subject;
83 } catch (const std::exception& e) {
84 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
85 << msg.subject << " failed: " << e.what();
86 }
87 reply(nats_, msg, resp);
88 }
89
90 void save(ores::nats::message msg) {
91 [[maybe_unused]] const auto correlation_id =
92 log_handler_entry(currency_market_tier_handler_lg(), msg);
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, "refdata::currency_market_tiers:write")) {
101 error_reply(nats_, msg, ores::service::error_code::forbidden);
102 return;
103 }
104 service::currency_market_tier_service svc(ctx);
105 auto req = decode<save_currency_market_tier_request>(msg);
106 if (!req) {
107 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
108 << "Failed to decode: " << msg.subject;
109 return;
110 }
111 try {
112 svc.save_type(req->data);
113 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
114 << "Completed " << msg.subject;
115 reply(nats_, msg,
116 save_currency_market_tier_response{.success = true});
117 } catch (const std::exception& e) {
118 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
119 << msg.subject << " failed: " << e.what();
120 reply(nats_, msg, save_currency_market_tier_response{
121 .success = false, .message = e.what()});
122 }
123 }
124
125 void remove(ores::nats::message msg) {
126 [[maybe_unused]] const auto correlation_id =
127 log_handler_entry(currency_market_tier_handler_lg(), msg);
128 auto ctx_expected = ores::service::service::make_request_context(
129 ctx_, msg, verifier_);
130 if (!ctx_expected) {
131 error_reply(nats_, msg, ctx_expected.error());
132 return;
133 }
134 const auto& ctx = *ctx_expected;
135 if (!has_permission(ctx, "refdata::currency_market_tiers:delete")) {
136 error_reply(nats_, msg, ores::service::error_code::forbidden);
137 return;
138 }
139 service::currency_market_tier_service svc(ctx);
140 auto req = decode<delete_currency_market_tier_request>(msg);
141 if (!req) {
142 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
143 << "Failed to decode: " << msg.subject;
144 return;
145 }
146 try {
147 svc.remove_type(req->tier);
148 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
149 << "Completed " << msg.subject;
150 reply(nats_, msg,
151 delete_currency_market_tier_response{.success = true});
152 } catch (const std::exception& e) {
153 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
154 << msg.subject << " failed: " << e.what();
155 reply(nats_, msg, delete_currency_market_tier_response{
156 .success = false, .message = e.what()});
157 }
158 }
159
160 void history(ores::nats::message msg) {
161 [[maybe_unused]] const auto correlation_id =
162 log_handler_entry(currency_market_tier_handler_lg(), msg);
163 auto ctx_expected = ores::service::service::make_request_context(
164 ctx_, msg, verifier_);
165 if (!ctx_expected) {
166 error_reply(nats_, msg, ctx_expected.error());
167 return;
168 }
169 const auto& ctx = *ctx_expected;
170 service::currency_market_tier_service svc(ctx);
171 auto req = decode<get_currency_market_tier_history_request>(msg);
172 if (!req) {
173 BOOST_LOG_SEV(currency_market_tier_handler_lg(), warn)
174 << "Failed to decode: " << msg.subject;
175 return;
176 }
177 try {
178 auto h = svc.get_type_history(req->tier);
179 BOOST_LOG_SEV(currency_market_tier_handler_lg(), debug)
180 << "Completed " << msg.subject;
181 reply(nats_, msg, get_currency_market_tier_history_response{
182 .success = true, .history = std::move(h)});
183 } catch (const std::exception& e) {
184 BOOST_LOG_SEV(currency_market_tier_handler_lg(), error)
185 << msg.subject << " failed: " << e.what();
186 reply(nats_, msg, get_currency_market_tier_history_response{
187 .success = false, .message = e.what()});
188 }
189 }
190
191private:
194 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
195};
196
197} // namespace ores::refdata::messaging
198#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