20#ifndef ORES_REFDATA_CORE_MESSAGING_CURRENCY_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_CURRENCY_HANDLER_HPP
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"
36namespace ores::refdata::messaging {
39inline auto& currency_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.refdata.messaging.currency_handler");
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;
53class currency_handler {
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
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_);
66 error_reply(nats_, msg, ctx_expected.error());
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);
74 BOOST_LOG_SEV(currency_handler_lg(), warn)
75 <<
"Failed to decode: " << msg.
subject;
76 reply(nats_, msg, resp);
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)
87 }
catch (
const std::exception& e) {
88 BOOST_LOG_SEV(currency_handler_lg(), error)
89 << msg.
subject <<
" failed: " << e.what();
91 reply(nats_, msg, resp);
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_);
100 error_reply(nats_, msg, ctx_expected.error());
103 const auto& ctx = *ctx_expected;
104 if (!has_permission(ctx,
"refdata::currencies:write")) {
108 service::currency_service svc(ctx);
109 auto req = decode<save_currency_request>(msg);
111 BOOST_LOG_SEV(currency_handler_lg(), warn)
112 <<
"Failed to decode: " << msg.
subject;
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()});
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_);
134 error_reply(nats_, msg, ctx_expected.error());
137 const auto& ctx = *ctx_expected;
138 if (!has_permission(ctx,
"refdata::currencies:delete")) {
142 service::currency_service svc(ctx);
143 auto req = decode<delete_currency_request>(msg);
145 BOOST_LOG_SEV(currency_handler_lg(), warn)
146 <<
"Failed to decode: " << msg.
subject;
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()});
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_);
168 error_reply(nats_, msg, ctx_expected.error());
171 const auto& ctx = *ctx_expected;
172 service::currency_service svc(ctx);
173 auto req = decode<get_currency_history_request>(msg);
175 BOOST_LOG_SEV(currency_handler_lg(), warn)
176 <<
"Failed to decode: " << msg.
subject;
180 auto h = svc.get_currency_history(req->iso_code);
181 currency_version_history cvh;
182 for (
const auto& c : h) {
185 cvh.versions.push_back(std::move(cv));
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()});
202 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
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