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;
52class business_unit_type_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(business_unit_type_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 repository::business_unit_type_repository repo(ctx);
70 get_business_unit_types_response resp;
72 resp.business_unit_types = repo.read_latest();
73 resp.total_available_count =
74 static_cast<int>(resp.business_unit_types.size());
75 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
77 }
catch (
const std::exception& e) {
78 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
79 << msg.
subject <<
" failed: " << e.what();
81 reply(nats_, msg, resp);
85 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, verifier_);
90 error_reply(nats_, msg, ctx_expected.error());
93 const auto& ctx = *ctx_expected;
94 if (!has_permission(ctx,
"refdata::business_unit_types:write")) {
98 repository::business_unit_type_repository repo(ctx);
99 auto req = decode<save_business_unit_type_request>(msg);
101 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
102 <<
"Failed to decode: " << msg.
subject;
106 repo.write(req->data);
107 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
108 <<
"Completed " << msg.
subject;
110 save_business_unit_type_response{.success =
true});
111 }
catch (
const std::exception& e) {
112 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
113 << msg.
subject <<
" failed: " << e.what();
114 reply(nats_, msg, save_business_unit_type_response{
115 .success =
false, .message = e.what()});
120 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
122 auto ctx_expected = ores::service::service::make_request_context(
123 ctx_, msg, verifier_);
125 error_reply(nats_, msg, ctx_expected.error());
128 const auto& ctx = *ctx_expected;
129 if (!has_permission(ctx,
"refdata::business_unit_types:delete")) {
133 repository::business_unit_type_repository repo(ctx);
134 auto req = decode<delete_business_unit_type_request>(msg);
136 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
137 <<
"Failed to decode: " << msg.
subject;
141 auto records = repo.read_latest_by_code(req->type);
142 if (!records.empty())
143 repo.remove(records.front().id);
144 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
145 <<
"Completed " << msg.
subject;
147 delete_business_unit_type_response{.success =
true});
148 }
catch (
const std::exception& e) {
149 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
150 << msg.
subject <<
" failed: " << e.what();
151 reply(nats_, msg, delete_business_unit_type_response{
152 .success =
false, .message = e.what()});
157 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
159 auto ctx_expected = ores::service::service::make_request_context(
160 ctx_, msg, verifier_);
162 error_reply(nats_, msg, ctx_expected.error());
165 const auto& ctx = *ctx_expected;
166 repository::business_unit_type_repository repo(ctx);
167 auto req = decode<get_business_unit_type_history_request>(msg);
169 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
170 <<
"Failed to decode: " << msg.
subject;
174 auto records = repo.read_latest_by_code(req->type);
175 std::vector<ores::refdata::domain::business_unit_type>
177 if (!records.empty())
178 history = repo.read_all(records.front().id);
179 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
180 <<
"Completed " << msg.
subject;
181 reply(nats_, msg, get_business_unit_type_history_response{
182 .success =
true, .history = std::move(history)});
183 }
catch (
const std::exception& e) {
184 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
185 << msg.
subject <<
" failed: " << e.what();
186 reply(nats_, msg, get_business_unit_type_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