20#ifndef ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_TYPE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_TYPE_HANDLER_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_type_protocol.hpp"
33#include "ores.refdata.core/repository/business_unit_type_repository.hpp"
34#include "ores.refdata.api/domain/business_unit_type.hpp"
36namespace ores::refdata::messaging {
39inline auto& business_unit_type_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.refdata.messaging.business_unit_type_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 business_unit_type_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(business_unit_type_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 repository::business_unit_type_repository repo(ctx);
71 get_business_unit_types_response resp;
73 resp.business_unit_types = repo.read_latest();
74 resp.total_available_count =
75 static_cast<int>(resp.business_unit_types.size());
76 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
78 }
catch (
const std::exception& e) {
79 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
80 << msg.
subject <<
" failed: " << e.what();
82 reply(nats_, msg, resp);
86 [[maybe_unused]]
const auto correlation_id =
87 log_handler_entry(business_unit_type_handler_lg(), msg);
88 auto ctx_expected = ores::service::service::make_request_context(
89 ctx_, msg, verifier_);
91 error_reply(nats_, msg, ctx_expected.error());
94 const auto& ctx = *ctx_expected;
95 if (!has_permission(ctx,
"refdata::business_unit_types:write")) {
99 repository::business_unit_type_repository repo(ctx);
100 auto req = decode<save_business_unit_type_request>(msg);
102 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
103 <<
"Failed to decode: " << msg.
subject;
107 repo.write(req->data);
108 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
109 <<
"Completed " << msg.
subject;
111 save_business_unit_type_response{.success =
true});
112 }
catch (
const std::exception& e) {
113 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
114 << msg.
subject <<
" failed: " << e.what();
115 reply(nats_, msg, save_business_unit_type_response{
116 .success =
false, .message = e.what()});
121 [[maybe_unused]]
const auto correlation_id =
122 log_handler_entry(business_unit_type_handler_lg(), msg);
123 auto ctx_expected = ores::service::service::make_request_context(
124 ctx_, msg, verifier_);
126 error_reply(nats_, msg, ctx_expected.error());
129 const auto& ctx = *ctx_expected;
130 if (!has_permission(ctx,
"refdata::business_unit_types:delete")) {
134 repository::business_unit_type_repository repo(ctx);
135 auto req = decode<delete_business_unit_type_request>(msg);
137 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
138 <<
"Failed to decode: " << msg.
subject;
142 auto records = repo.read_latest_by_code(req->type);
143 if (!records.empty())
144 repo.remove(records.front().id);
145 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
146 <<
"Completed " << msg.
subject;
148 delete_business_unit_type_response{.success =
true});
149 }
catch (
const std::exception& e) {
150 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
151 << msg.
subject <<
" failed: " << e.what();
152 reply(nats_, msg, delete_business_unit_type_response{
153 .success =
false, .message = e.what()});
158 [[maybe_unused]]
const auto correlation_id =
159 log_handler_entry(business_unit_type_handler_lg(), msg);
160 auto ctx_expected = ores::service::service::make_request_context(
161 ctx_, msg, verifier_);
163 error_reply(nats_, msg, ctx_expected.error());
166 const auto& ctx = *ctx_expected;
167 repository::business_unit_type_repository repo(ctx);
168 auto req = decode<get_business_unit_type_history_request>(msg);
170 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
171 <<
"Failed to decode: " << msg.
subject;
175 auto records = repo.read_latest_by_code(req->type);
176 std::vector<ores::refdata::domain::business_unit_type>
178 if (!records.empty())
179 history = repo.read_all(records.front().id);
180 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
181 <<
"Completed " << msg.
subject;
182 reply(nats_, msg, get_business_unit_type_history_response{
183 .success =
true, .history = std::move(history)});
184 }
catch (
const std::exception& e) {
185 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
186 << msg.
subject <<
" failed: " << e.what();
187 reply(nats_, msg, get_business_unit_type_history_response{
188 .success =
false, .message = e.what()});
195 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