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 namespace ores::logging;
51
52class business_unit_type_handler {
53public:
54 business_unit_type_handler(ores::nats::service::client& nats,
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
58
59 void list(ores::nats::message msg) {
60 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
61 << "Handling " << msg.subject;
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
64 if (!ctx_expected) {
65 error_reply(nats_, msg, ctx_expected.error());
66 return;
67 }
68 const auto& ctx = *ctx_expected;
69 repository::business_unit_type_repository repo(ctx);
70 get_business_unit_types_response resp;
71 try {
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)
76 << "Completed " << msg.subject;
77 } catch (const std::exception& e) {
78 BOOST_LOG_SEV(business_unit_type_handler_lg(), error)
79 << msg.subject << " failed: " << e.what();
80 }
81 reply(nats_, msg, resp);
82 }
83
84 void save(ores::nats::message msg) {
85 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
86 << "Handling " << msg.subject;
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, verifier_);
89 if (!ctx_expected) {
90 error_reply(nats_, msg, ctx_expected.error());
91 return;
92 }
93 const auto& ctx = *ctx_expected;
94 if (!has_permission(ctx, "refdata::business_unit_types:write")) {
95 error_reply(nats_, msg, ores::service::error_code::forbidden);
96 return;
97 }
98 repository::business_unit_type_repository repo(ctx);
99 auto req = decode<save_business_unit_type_request>(msg);
100 if (!req) {
101 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
102 << "Failed to decode: " << msg.subject;
103 return;
104 }
105 try {
106 repo.write(req->data);
107 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
108 << "Completed " << msg.subject;
109 reply(nats_, msg,
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()});
116 }
117 }
118
119 void del(ores::nats::message msg) {
120 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
121 << "Handling " << msg.subject;
122 auto ctx_expected = ores::service::service::make_request_context(
123 ctx_, msg, verifier_);
124 if (!ctx_expected) {
125 error_reply(nats_, msg, ctx_expected.error());
126 return;
127 }
128 const auto& ctx = *ctx_expected;
129 if (!has_permission(ctx, "refdata::business_unit_types:delete")) {
130 error_reply(nats_, msg, ores::service::error_code::forbidden);
131 return;
132 }
133 repository::business_unit_type_repository repo(ctx);
134 auto req = decode<delete_business_unit_type_request>(msg);
135 if (!req) {
136 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
137 << "Failed to decode: " << msg.subject;
138 return;
139 }
140 try {
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;
146 reply(nats_, msg,
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()});
153 }
154 }
155
156 void history(ores::nats::message msg) {
157 BOOST_LOG_SEV(business_unit_type_handler_lg(), debug)
158 << "Handling " << msg.subject;
159 auto ctx_expected = ores::service::service::make_request_context(
160 ctx_, msg, verifier_);
161 if (!ctx_expected) {
162 error_reply(nats_, msg, ctx_expected.error());
163 return;
164 }
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);
168 if (!req) {
169 BOOST_LOG_SEV(business_unit_type_handler_lg(), warn)
170 << "Failed to decode: " << msg.subject;
171 return;
172 }
173 try {
174 auto records = repo.read_latest_by_code(req->type);
175 std::vector<ores::refdata::domain::business_unit_type>
176 history;
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()});
188 }
189 }
190
191private:
194 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
195};
196
197} // namespace ores::refdata::messaging
198#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