20#ifndef ORES_REFDATA_CORE_MESSAGING_COUNTRY_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_COUNTRY_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/country_protocol.hpp"
32#include "ores.refdata.core/service/country_service.hpp"
34namespace ores::refdata::messaging {
37inline auto& country_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.country_handler");
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;
51class country_handler {
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 [[maybe_unused]]
const auto correlation_id =
60 log_handler_entry(country_handler_lg(), msg);
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
64 error_reply(nats_, msg, ctx_expected.error());
67 const auto& ctx = *ctx_expected;
68 service::country_service svc(ctx);
69 get_countries_response resp;
70 auto req = decode<get_countries_request>(msg);
72 BOOST_LOG_SEV(country_handler_lg(), warn)
73 <<
"Failed to decode: " << msg.
subject;
74 reply(nats_, msg, resp);
78 resp.countries = svc.list_countries(
79 static_cast<std::uint32_t
>(req->offset),
80 static_cast<std::uint32_t
>(req->limit));
81 resp.total_available_count =
82 static_cast<int>(svc.count_countries());
83 BOOST_LOG_SEV(country_handler_lg(), debug)
85 }
catch (
const std::exception& e) {
86 BOOST_LOG_SEV(country_handler_lg(), error)
87 << msg.
subject <<
" failed: " << e.what();
89 reply(nats_, msg, resp);
93 [[maybe_unused]]
const auto correlation_id =
94 log_handler_entry(country_handler_lg(), msg);
95 auto ctx_expected = ores::service::service::make_request_context(
96 ctx_, msg, verifier_);
98 error_reply(nats_, msg, ctx_expected.error());
101 const auto& ctx = *ctx_expected;
102 if (!has_permission(ctx,
"refdata::countries:write")) {
106 service::country_service svc(ctx);
107 auto req = decode<save_country_request>(msg);
109 BOOST_LOG_SEV(country_handler_lg(), warn)
110 <<
"Failed to decode: " << msg.
subject;
114 svc.save_country(req->data);
115 BOOST_LOG_SEV(country_handler_lg(), debug)
116 <<
"Completed " << msg.
subject;
117 reply(nats_, msg, save_country_response{.success =
true});
118 }
catch (
const std::exception& e) {
119 BOOST_LOG_SEV(country_handler_lg(), error)
120 << msg.
subject <<
" failed: " << e.what();
121 reply(nats_, msg, save_country_response{
122 .success =
false, .message = e.what()});
127 [[maybe_unused]]
const auto correlation_id =
128 log_handler_entry(country_handler_lg(), msg);
129 auto ctx_expected = ores::service::service::make_request_context(
130 ctx_, msg, verifier_);
132 error_reply(nats_, msg, ctx_expected.error());
135 const auto& ctx = *ctx_expected;
136 if (!has_permission(ctx,
"refdata::countries:delete")) {
140 service::country_service svc(ctx);
141 auto req = decode<delete_country_request>(msg);
143 BOOST_LOG_SEV(country_handler_lg(), warn)
144 <<
"Failed to decode: " << msg.
subject;
148 svc.delete_countries(req->alpha2_codes);
149 BOOST_LOG_SEV(country_handler_lg(), debug)
150 <<
"Completed " << msg.
subject;
151 reply(nats_, msg, delete_country_response{.success =
true});
152 }
catch (
const std::exception& e) {
153 BOOST_LOG_SEV(country_handler_lg(), error)
154 << msg.
subject <<
" failed: " << e.what();
155 reply(nats_, msg, delete_country_response{
156 .success =
false, .message = e.what()});
161 [[maybe_unused]]
const auto correlation_id =
162 log_handler_entry(country_handler_lg(), msg);
163 auto ctx_expected = ores::service::service::make_request_context(
164 ctx_, msg, verifier_);
166 error_reply(nats_, msg, ctx_expected.error());
169 const auto& ctx = *ctx_expected;
170 service::country_service svc(ctx);
171 auto req = decode<get_country_history_request>(msg);
173 BOOST_LOG_SEV(country_handler_lg(), warn)
174 <<
"Failed to decode: " << msg.
subject;
178 auto h = svc.get_country_history(req->alpha2_code);
179 BOOST_LOG_SEV(country_handler_lg(), debug)
180 <<
"Completed " << msg.
subject;
181 reply(nats_, msg, get_country_history_response{
182 .success =
true, .history = std::move(h)});
183 }
catch (
const std::exception& e) {
184 BOOST_LOG_SEV(country_handler_lg(), error)
185 << msg.
subject <<
" failed: " << e.what();
186 reply(nats_, msg, get_country_history_response{
187 .success =
false, .message = e.what()});
194 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