ORE Studio 0.0.4
Loading...
Searching...
No Matches
business_unit_type_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_TYPE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BUSINESS_UNIT_TYPE_HANDLER_HPP
22
23#include <optional>
24#include <vector>
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"
35
36namespace ores::refdata::messaging {
37
38namespace {
39inline auto& business_unit_type_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.refdata.messaging.business_unit_type_handler");
42 return instance;
43}
44} // namespace
45
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;
51using namespace ores::logging;
52
53class business_unit_type_handler {
54public:
55 business_unit_type_handler(ores::nats::service::client& nats,
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
59
60 void list(ores::nats::message msg) {
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_);
65 if (!ctx_expected) {
66 error_reply(nats_, msg, ctx_expected.error());
67 return;
68 }
69 const auto& ctx = *ctx_expected;
70 repository::business_unit_type_repository repo(ctx);
71 get_business_unit_types_response resp;
72 try {
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)
77 << "Completed " << msg.subject;
78 } catch (const std::exception& e) {
79 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
80 << msg.subject << " failed: " << e.what();
81 }
82 reply(nats_, msg, resp);
83 }
84
85 void save(ores::nats::message msg) {
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_);
90 if (!ctx_expected) {
91 error_reply(nats_, msg, ctx_expected.error());
92 return;
93 }
94 const auto& ctx = *ctx_expected;
95 if (!has_permission(ctx, "refdata::business_unit_types:write")) {
96 error_reply(nats_, msg, ores::service::error_code::forbidden);
97 return;
98 }
99 repository::business_unit_type_repository repo(ctx);
100 auto req = decode<save_business_unit_type_request>(msg);
101 if (!req) {
102 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
103 << "Failed to decode: " << msg.subject;
104 return;
105 }
106 try {
107 repo.write(req->data);
108 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
109 << "Completed " << msg.subject;
110 reply(nats_, msg,
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()});
117 }
118 }
119
120 void remove(ores::nats::message msg) {
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_);
125 if (!ctx_expected) {
126 error_reply(nats_, msg, ctx_expected.error());
127 return;
128 }
129 const auto& ctx = *ctx_expected;
130 if (!has_permission(ctx, "refdata::business_unit_types:delete")) {
131 error_reply(nats_, msg, ores::service::error_code::forbidden);
132 return;
133 }
134 repository::business_unit_type_repository repo(ctx);
135 auto req = decode<delete_business_unit_type_request>(msg);
136 if (!req) {
137 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
138 << "Failed to decode: " << msg.subject;
139 return;
140 }
141 try {
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;
147 reply(nats_, msg,
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()});
154 }
155 }
156
157 void history(ores::nats::message msg) {
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_);
162 if (!ctx_expected) {
163 error_reply(nats_, msg, ctx_expected.error());
164 return;
165 }
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);
169 if (!req) {
170 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
171 << "Failed to decode: " << msg.subject;
172 return;
173 }
174 try {
175 auto records = repo.read_latest_by_code(req->type);
176 std::vector<ores::refdata::domain::business_unit_type>
177 history;
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()});
189 }
190 }
191
192private:
195 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
196};
197
198} // namespace ores::refdata::messaging
199#endif
STL namespace.
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