ORE Studio 0.0.4
Loading...
Searching...
No Matches
currency_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_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_CURRENCY_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_protocol.hpp"
32#include "ores.refdata.api/messaging/currency_history_protocol.hpp"
33#include "ores.refdata.core/service/currency_service.hpp"
34#include "ores.refdata.api/domain/currency_version.hpp"
35
36namespace ores::refdata::messaging {
37
38namespace {
39inline auto& currency_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.refdata.messaging.currency_handler");
42 return instance;
43}
44} // namespace
45
46using ores::service::messaging::reply;
47using ores::service::messaging::decode;
48using ores::service::messaging::error_reply;
49using ores::service::messaging::has_permission;
50using namespace ores::logging;
51
52class currency_handler {
53public:
54 currency_handler(ores::nats::service::client& nats,
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
58
59 void list(ores::nats::message msg) {
60 BOOST_LOG_SEV(currency_handler_lg(), debug)
61 << "Handling " << msg.subject;
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
64 if (!ctx_expected) {
65 error_reply(nats_, msg, ctx_expected.error());
66 return;
67 }
68 const auto& ctx = *ctx_expected;
69 service::currency_service svc(ctx);
70 get_currencies_response resp;
71 auto req = decode<get_currencies_request>(msg);
72 if (!req) {
73 BOOST_LOG_SEV(currency_handler_lg(), warn)
74 << "Failed to decode: " << msg.subject;
75 reply(nats_, msg, resp);
76 return;
77 }
78 try {
79 resp.currencies = svc.list_currencies(
80 static_cast<std::uint32_t>(req->offset),
81 static_cast<std::uint32_t>(req->limit));
82 resp.total_available_count =
83 static_cast<int>(svc.count_currencies());
84 BOOST_LOG_SEV(currency_handler_lg(), debug)
85 << "Completed " << msg.subject;
86 } catch (const std::exception& e) {
87 BOOST_LOG_SEV(currency_handler_lg(), error)
88 << msg.subject << " failed: " << e.what();
89 }
90 reply(nats_, msg, resp);
91 }
92
93 void save(ores::nats::message msg) {
94 BOOST_LOG_SEV(currency_handler_lg(), debug)
95 << "Handling " << msg.subject;
96 auto ctx_expected = ores::service::service::make_request_context(
97 ctx_, msg, verifier_);
98 if (!ctx_expected) {
99 error_reply(nats_, msg, ctx_expected.error());
100 return;
101 }
102 const auto& ctx = *ctx_expected;
103 if (!has_permission(ctx, "refdata::currencies:write")) {
104 error_reply(nats_, msg, ores::service::error_code::forbidden);
105 return;
106 }
107 service::currency_service svc(ctx);
108 auto req = decode<save_currency_request>(msg);
109 if (!req) {
110 BOOST_LOG_SEV(currency_handler_lg(), warn)
111 << "Failed to decode: " << msg.subject;
112 return;
113 }
114 try {
115 svc.save_currency(req->data);
116 BOOST_LOG_SEV(currency_handler_lg(), debug)
117 << "Completed " << msg.subject;
118 reply(nats_, msg, save_currency_response{.success = true});
119 } catch (const std::exception& e) {
120 BOOST_LOG_SEV(currency_handler_lg(), error)
121 << msg.subject << " failed: " << e.what();
122 reply(nats_, msg, save_currency_response{
123 .success = false, .message = e.what()});
124 }
125 }
126
127 void del(ores::nats::message msg) {
128 BOOST_LOG_SEV(currency_handler_lg(), debug)
129 << "Handling " << msg.subject;
130 auto ctx_expected = ores::service::service::make_request_context(
131 ctx_, msg, verifier_);
132 if (!ctx_expected) {
133 error_reply(nats_, msg, ctx_expected.error());
134 return;
135 }
136 const auto& ctx = *ctx_expected;
137 if (!has_permission(ctx, "refdata::currencies:delete")) {
138 error_reply(nats_, msg, ores::service::error_code::forbidden);
139 return;
140 }
141 service::currency_service svc(ctx);
142 auto req = decode<delete_currency_request>(msg);
143 if (!req) {
144 BOOST_LOG_SEV(currency_handler_lg(), warn)
145 << "Failed to decode: " << msg.subject;
146 return;
147 }
148 try {
149 svc.delete_currencies(req->iso_codes);
150 BOOST_LOG_SEV(currency_handler_lg(), debug)
151 << "Completed " << msg.subject;
152 reply(nats_, msg, delete_currency_response{.success = true});
153 } catch (const std::exception& e) {
154 BOOST_LOG_SEV(currency_handler_lg(), error)
155 << msg.subject << " failed: " << e.what();
156 reply(nats_, msg, delete_currency_response{
157 .success = false, .message = e.what()});
158 }
159 }
160
161 void history(ores::nats::message msg) {
162 BOOST_LOG_SEV(currency_handler_lg(), debug)
163 << "Handling " << msg.subject;
164 auto ctx_expected = ores::service::service::make_request_context(
165 ctx_, msg, verifier_);
166 if (!ctx_expected) {
167 error_reply(nats_, msg, ctx_expected.error());
168 return;
169 }
170 const auto& ctx = *ctx_expected;
171 service::currency_service svc(ctx);
172 auto req = decode<get_currency_history_request>(msg);
173 if (!req) {
174 BOOST_LOG_SEV(currency_handler_lg(), warn)
175 << "Failed to decode: " << msg.subject;
176 return;
177 }
178 try {
179 auto h = svc.get_currency_history(req->iso_code);
180 currency_version_history cvh;
181 for (const auto& c : h) {
183 cv.data = c;
184 cvh.versions.push_back(std::move(cv));
185 }
186 BOOST_LOG_SEV(currency_handler_lg(), debug)
187 << "Completed " << msg.subject;
188 reply(nats_, msg, get_currency_history_response{
189 .success = true, .history = std::move(cvh)});
190 } catch (const std::exception& e) {
191 BOOST_LOG_SEV(currency_handler_lg(), error)
192 << msg.subject << " failed: " << e.what();
193 reply(nats_, msg, get_currency_history_response{
194 .success = false, .message = e.what()});
195 }
196 }
197
198private:
201 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
202};
203
204} // namespace ores::refdata::messaging
205#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
Represents a specific version of a currency with metadata.
Definition currency_version.hpp:32
currency data
The currency data at this version.
Definition currency_version.hpp:36