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 ores::service::messaging::log_handler_entry;
51using namespace ores::logging;
52
53class currency_handler {
54public:
55 currency_handler(ores::nats::service::client& nats,
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
59
60 void list(ores::nats::message msg) {
61 [[maybe_unused]] const auto correlation_id =
62 log_handler_entry(currency_handler_lg(), msg);
63 auto ctx_expected = ores::service::service::make_request_context(
64 ctx_, msg, verifier_);
65 if (!ctx_expected) {
66 error_reply(nats_, msg, ctx_expected.error());
67 return;
68 }
69 const auto& ctx = *ctx_expected;
70 service::currency_service svc(ctx);
71 get_currencies_response resp;
72 auto req = decode<get_currencies_request>(msg);
73 if (!req) {
74 BOOST_LOG_SEV(currency_handler_lg(), warn)
75 << "Failed to decode: " << msg.subject;
76 reply(nats_, msg, resp);
77 return;
78 }
79 try {
80 resp.currencies = svc.list_currencies(
81 static_cast<std::uint32_t>(req->offset),
82 static_cast<std::uint32_t>(req->limit));
83 resp.total_available_count =
84 static_cast<int>(svc.count_currencies());
85 BOOST_LOG_SEV(currency_handler_lg(), debug)
86 << "Completed " << msg.subject;
87 } catch (const std::exception& e) {
88 BOOST_LOG_SEV(currency_handler_lg(), error)
89 << msg.subject << " failed: " << e.what();
90 }
91 reply(nats_, msg, resp);
92 }
93
94 void save(ores::nats::message msg) {
95 [[maybe_unused]] const auto correlation_id =
96 log_handler_entry(currency_handler_lg(), msg);
97 auto ctx_expected = ores::service::service::make_request_context(
98 ctx_, msg, verifier_);
99 if (!ctx_expected) {
100 error_reply(nats_, msg, ctx_expected.error());
101 return;
102 }
103 const auto& ctx = *ctx_expected;
104 if (!has_permission(ctx, "refdata::currencies:write")) {
105 error_reply(nats_, msg, ores::service::error_code::forbidden);
106 return;
107 }
108 service::currency_service svc(ctx);
109 auto req = decode<save_currency_request>(msg);
110 if (!req) {
111 BOOST_LOG_SEV(currency_handler_lg(), warn)
112 << "Failed to decode: " << msg.subject;
113 return;
114 }
115 try {
116 svc.save_currency(req->data);
117 BOOST_LOG_SEV(currency_handler_lg(), debug)
118 << "Completed " << msg.subject;
119 reply(nats_, msg, save_currency_response{.success = true});
120 } catch (const std::exception& e) {
121 BOOST_LOG_SEV(currency_handler_lg(), error)
122 << msg.subject << " failed: " << e.what();
123 reply(nats_, msg, save_currency_response{
124 .success = false, .message = e.what()});
125 }
126 }
127
128 void remove(ores::nats::message msg) {
129 [[maybe_unused]] const auto correlation_id =
130 log_handler_entry(currency_handler_lg(), msg);
131 auto ctx_expected = ores::service::service::make_request_context(
132 ctx_, msg, verifier_);
133 if (!ctx_expected) {
134 error_reply(nats_, msg, ctx_expected.error());
135 return;
136 }
137 const auto& ctx = *ctx_expected;
138 if (!has_permission(ctx, "refdata::currencies:delete")) {
139 error_reply(nats_, msg, ores::service::error_code::forbidden);
140 return;
141 }
142 service::currency_service svc(ctx);
143 auto req = decode<delete_currency_request>(msg);
144 if (!req) {
145 BOOST_LOG_SEV(currency_handler_lg(), warn)
146 << "Failed to decode: " << msg.subject;
147 return;
148 }
149 try {
150 svc.delete_currencies(req->iso_codes);
151 BOOST_LOG_SEV(currency_handler_lg(), debug)
152 << "Completed " << msg.subject;
153 reply(nats_, msg, delete_currency_response{.success = true});
154 } catch (const std::exception& e) {
155 BOOST_LOG_SEV(currency_handler_lg(), error)
156 << msg.subject << " failed: " << e.what();
157 reply(nats_, msg, delete_currency_response{
158 .success = false, .message = e.what()});
159 }
160 }
161
162 void history(ores::nats::message msg) {
163 [[maybe_unused]] const auto correlation_id =
164 log_handler_entry(currency_handler_lg(), msg);
165 auto ctx_expected = ores::service::service::make_request_context(
166 ctx_, msg, verifier_);
167 if (!ctx_expected) {
168 error_reply(nats_, msg, ctx_expected.error());
169 return;
170 }
171 const auto& ctx = *ctx_expected;
172 service::currency_service svc(ctx);
173 auto req = decode<get_currency_history_request>(msg);
174 if (!req) {
175 BOOST_LOG_SEV(currency_handler_lg(), warn)
176 << "Failed to decode: " << msg.subject;
177 return;
178 }
179 try {
180 auto h = svc.get_currency_history(req->iso_code);
181 currency_version_history cvh;
182 for (const auto& c : h) {
184 cv.data = c;
185 cvh.versions.push_back(std::move(cv));
186 }
187 BOOST_LOG_SEV(currency_handler_lg(), debug)
188 << "Completed " << msg.subject;
189 reply(nats_, msg, get_currency_history_response{
190 .success = true, .history = std::move(cvh)});
191 } catch (const std::exception& e) {
192 BOOST_LOG_SEV(currency_handler_lg(), error)
193 << msg.subject << " failed: " << e.what();
194 reply(nats_, msg, get_currency_history_response{
195 .success = false, .message = e.what()});
196 }
197 }
198
199private:
202 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
203};
204
205} // namespace ores::refdata::messaging
206#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