20#ifndef ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_HANDLER_HPP
24#include <boost/uuid/string_generator.hpp>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.refdata.api/messaging/business_unit_protocol.hpp"
33#include "ores.refdata.core/service/business_unit_service.hpp"
35namespace ores::refdata::messaging {
38inline auto& business_unit_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.business_unit_handler");
45using ores::service::messaging::reply;
46using ores::service::messaging::decode;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::has_permission;
51class business_unit_handler {
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
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::business_unit_service svc(ctx);
69 get_business_units_response resp;
71 resp.business_units = svc.list_business_units();
72 resp.total_available_count =
73 static_cast<int>(resp.business_units.size());
74 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
76 }
catch (
const std::exception& e) {
77 BOOST_LOG_SEV(business_unit_handler_lg(), error)
78 << msg.
subject <<
" failed: " << e.what();
80 reply(nats_, msg, resp);
84 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
86 auto ctx_expected = ores::service::service::make_request_context(
87 ctx_, msg, verifier_);
89 error_reply(nats_, msg, ctx_expected.error());
92 const auto& ctx = *ctx_expected;
93 if (!has_permission(ctx,
"refdata::business_units:write")) {
97 service::business_unit_service svc(ctx);
98 auto req = decode<save_business_unit_request>(msg);
100 BOOST_LOG_SEV(business_unit_handler_lg(), warn)
101 <<
"Failed to decode: " << msg.
subject;
105 svc.save_business_unit(req->data);
106 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
107 <<
"Completed " << msg.
subject;
109 save_business_unit_response{.success =
true});
110 }
catch (
const std::exception& e) {
111 BOOST_LOG_SEV(business_unit_handler_lg(), error)
112 << msg.
subject <<
" failed: " << e.what();
113 reply(nats_, msg, save_business_unit_response{
114 .success =
false, .message = e.what()});
119 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
121 auto ctx_expected = ores::service::service::make_request_context(
122 ctx_, msg, verifier_);
124 error_reply(nats_, msg, ctx_expected.error());
127 const auto& ctx = *ctx_expected;
128 if (!has_permission(ctx,
"refdata::business_units:delete")) {
132 service::business_unit_service svc(ctx);
133 auto req = decode<delete_business_unit_request>(msg);
135 BOOST_LOG_SEV(business_unit_handler_lg(), warn)
136 <<
"Failed to decode: " << msg.
subject;
140 boost::uuids::string_generator gen;
141 for (
const auto& id_str : req->ids)
142 svc.remove_business_unit(gen(id_str));
143 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
144 <<
"Completed " << msg.
subject;
146 delete_business_unit_response{.success =
true});
147 }
catch (
const std::exception& e) {
148 BOOST_LOG_SEV(business_unit_handler_lg(), error)
149 << msg.
subject <<
" failed: " << e.what();
150 reply(nats_, msg, delete_business_unit_response{
151 .success =
false, .message = e.what()});
156 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
158 auto ctx_expected = ores::service::service::make_request_context(
159 ctx_, msg, verifier_);
161 error_reply(nats_, msg, ctx_expected.error());
164 const auto& ctx = *ctx_expected;
165 service::business_unit_service svc(ctx);
166 auto req = decode<get_business_unit_history_request>(msg);
168 BOOST_LOG_SEV(business_unit_handler_lg(), warn)
169 <<
"Failed to decode: " << msg.
subject;
173 boost::uuids::string_generator gen;
174 auto h = svc.get_business_unit_history(gen(req->id));
175 BOOST_LOG_SEV(business_unit_handler_lg(), debug)
176 <<
"Completed " << msg.
subject;
177 reply(nats_, msg, get_business_unit_history_response{
178 .success =
true, .history = std::move(h)});
179 }
catch (
const std::exception& e) {
180 BOOST_LOG_SEV(business_unit_handler_lg(), error)
181 << msg.
subject <<
" failed: " << e.what();
182 reply(nats_, msg, get_business_unit_history_response{
183 .success =
false, .message = e.what()});
190 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