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;
50class country_handler {
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
58 BOOST_LOG_SEV(country_handler_lg(), debug)
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
63 error_reply(nats_, msg, ctx_expected.error());
66 const auto& ctx = *ctx_expected;
67 service::country_service svc(ctx);
68 get_countries_response resp;
69 auto req = decode<get_countries_request>(msg);
71 BOOST_LOG_SEV(country_handler_lg(), warn)
72 <<
"Failed to decode: " << msg.
subject;
73 reply(nats_, msg, resp);
77 resp.countries = svc.list_countries(
78 static_cast<std::uint32_t
>(req->offset),
79 static_cast<std::uint32_t
>(req->limit));
80 resp.total_available_count =
81 static_cast<int>(svc.count_countries());
82 BOOST_LOG_SEV(country_handler_lg(), debug)
84 }
catch (
const std::exception& e) {
85 BOOST_LOG_SEV(country_handler_lg(), error)
86 << msg.
subject <<
" failed: " << e.what();
88 reply(nats_, msg, resp);
92 BOOST_LOG_SEV(country_handler_lg(), debug)
94 auto ctx_expected = ores::service::service::make_request_context(
95 ctx_, msg, verifier_);
97 error_reply(nats_, msg, ctx_expected.error());
100 const auto& ctx = *ctx_expected;
101 if (!has_permission(ctx,
"refdata::countries:write")) {
105 service::country_service svc(ctx);
106 auto req = decode<save_country_request>(msg);
108 BOOST_LOG_SEV(country_handler_lg(), warn)
109 <<
"Failed to decode: " << msg.
subject;
113 svc.save_country(req->data);
114 BOOST_LOG_SEV(country_handler_lg(), debug)
115 <<
"Completed " << msg.
subject;
116 reply(nats_, msg, save_country_response{.success =
true});
117 }
catch (
const std::exception& e) {
118 BOOST_LOG_SEV(country_handler_lg(), error)
119 << msg.
subject <<
" failed: " << e.what();
120 reply(nats_, msg, save_country_response{
121 .success =
false, .message = e.what()});
126 BOOST_LOG_SEV(country_handler_lg(), debug)
128 auto ctx_expected = ores::service::service::make_request_context(
129 ctx_, msg, verifier_);
131 error_reply(nats_, msg, ctx_expected.error());
134 const auto& ctx = *ctx_expected;
135 if (!has_permission(ctx,
"refdata::countries:delete")) {
139 service::country_service svc(ctx);
140 auto req = decode<delete_country_request>(msg);
142 BOOST_LOG_SEV(country_handler_lg(), warn)
143 <<
"Failed to decode: " << msg.
subject;
147 svc.delete_countries(req->alpha2_codes);
148 BOOST_LOG_SEV(country_handler_lg(), debug)
149 <<
"Completed " << msg.
subject;
150 reply(nats_, msg, delete_country_response{.success =
true});
151 }
catch (
const std::exception& e) {
152 BOOST_LOG_SEV(country_handler_lg(), error)
153 << msg.
subject <<
" failed: " << e.what();
154 reply(nats_, msg, delete_country_response{
155 .success =
false, .message = e.what()});
160 BOOST_LOG_SEV(country_handler_lg(), debug)
162 auto ctx_expected = ores::service::service::make_request_context(
163 ctx_, msg, verifier_);
165 error_reply(nats_, msg, ctx_expected.error());
168 const auto& ctx = *ctx_expected;
169 service::country_service svc(ctx);
170 auto req = decode<get_country_history_request>(msg);
172 BOOST_LOG_SEV(country_handler_lg(), warn)
173 <<
"Failed to decode: " << msg.
subject;
177 auto h = svc.get_country_history(req->alpha2_code);
178 BOOST_LOG_SEV(country_handler_lg(), debug)
179 <<
"Completed " << msg.
subject;
180 reply(nats_, msg, get_country_history_response{
181 .success =
true, .history = std::move(h)});
182 }
catch (
const std::exception& e) {
183 BOOST_LOG_SEV(country_handler_lg(), error)
184 << msg.
subject <<
" failed: " << e.what();
185 reply(nats_, msg, get_country_history_response{
186 .success =
false, .message = e.what()});
193 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