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;
52class currency_handler {
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
60 BOOST_LOG_SEV(currency_handler_lg(), debug)
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
65 error_reply(nats_, msg, ctx_expected.error());
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);
73 BOOST_LOG_SEV(currency_handler_lg(), warn)
74 <<
"Failed to decode: " << msg.
subject;
75 reply(nats_, msg, resp);
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)
86 }
catch (
const std::exception& e) {
87 BOOST_LOG_SEV(currency_handler_lg(), error)
88 << msg.
subject <<
" failed: " << e.what();
90 reply(nats_, msg, resp);
94 BOOST_LOG_SEV(currency_handler_lg(), debug)
96 auto ctx_expected = ores::service::service::make_request_context(
97 ctx_, msg, verifier_);
99 error_reply(nats_, msg, ctx_expected.error());
102 const auto& ctx = *ctx_expected;
103 if (!has_permission(ctx,
"refdata::currencies:write")) {
107 service::currency_service svc(ctx);
108 auto req = decode<save_currency_request>(msg);
110 BOOST_LOG_SEV(currency_handler_lg(), warn)
111 <<
"Failed to decode: " << msg.
subject;
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()});
128 BOOST_LOG_SEV(currency_handler_lg(), debug)
130 auto ctx_expected = ores::service::service::make_request_context(
131 ctx_, msg, verifier_);
133 error_reply(nats_, msg, ctx_expected.error());
136 const auto& ctx = *ctx_expected;
137 if (!has_permission(ctx,
"refdata::currencies:delete")) {
141 service::currency_service svc(ctx);
142 auto req = decode<delete_currency_request>(msg);
144 BOOST_LOG_SEV(currency_handler_lg(), warn)
145 <<
"Failed to decode: " << msg.
subject;
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()});
162 BOOST_LOG_SEV(currency_handler_lg(), debug)
164 auto ctx_expected = ores::service::service::make_request_context(
165 ctx_, msg, verifier_);
167 error_reply(nats_, msg, ctx_expected.error());
170 const auto& ctx = *ctx_expected;
171 service::currency_service svc(ctx);
172 auto req = decode<get_currency_history_request>(msg);
174 BOOST_LOG_SEV(currency_handler_lg(), warn)
175 <<
"Failed to decode: " << msg.
subject;
179 auto h = svc.get_currency_history(req->iso_code);
180 currency_version_history cvh;
181 for (
const auto& c : h) {
184 cvh.versions.push_back(std::move(cv));
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()});
201 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