ORE Studio 0.0.4
Loading...
Searching...
No Matches
business_centre_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_CENTRE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BUSINESS_CENTRE_HANDLER_HPP
22
23#include <optional>
24#include "ores.logging/make_logger.hpp"
25#include "ores.nats/domain/message.hpp"
26#include "ores.nats/service/client.hpp"
27#include "ores.database/domain/context.hpp"
28#include "ores.security/jwt/jwt_authenticator.hpp"
29#include "ores.service/messaging/handler_helpers.hpp"
30#include "ores.service/service/request_context.hpp"
31#include "ores.refdata.api/messaging/business_centre_protocol.hpp"
32#include "ores.refdata.core/service/business_centre_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& business_centre_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.business_centre_handler");
40 return instance;
41}
42} // namespace
43
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
48using ores::service::messaging::log_handler_entry;
49using namespace ores::logging;
50
51class business_centre_handler {
52public:
53 business_centre_handler(ores::nats::service::client& nats,
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
57
58 void list(ores::nats::message msg) {
59 [[maybe_unused]] const auto correlation_id =
60 log_handler_entry(business_centre_handler_lg(), msg);
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
63 if (!ctx_expected) {
64 error_reply(nats_, msg, ctx_expected.error());
65 return;
66 }
67 const auto& ctx = *ctx_expected;
68 service::business_centre_service svc(ctx);
69 get_business_centres_response resp;
70 auto req = decode<get_business_centres_request>(msg);
71 if (!req) {
72 BOOST_LOG_SEV(business_centre_handler_lg(), warn)
73 << "Failed to decode: " << msg.subject;
74 reply(nats_, msg, resp);
75 return;
76 }
77 try {
78 resp.business_centres = svc.list_business_centres(
79 static_cast<std::uint32_t>(req->offset),
80 static_cast<std::uint32_t>(req->limit));
81 resp.total_available_count =
82 static_cast<int>(svc.count_business_centres());
83 BOOST_LOG_SEV(business_centre_handler_lg(), debug)
84 << "Completed " << msg.subject;
85 } catch (const std::exception& e) {
86 BOOST_LOG_SEV(business_centre_handler_lg(), error)
87 << msg.subject << " failed: " << e.what();
88 }
89 reply(nats_, msg, resp);
90 }
91
92 void save(ores::nats::message msg) {
93 [[maybe_unused]] const auto correlation_id =
94 log_handler_entry(business_centre_handler_lg(), msg);
95 auto ctx_expected = ores::service::service::make_request_context(
96 ctx_, msg, verifier_);
97 if (!ctx_expected) {
98 error_reply(nats_, msg, ctx_expected.error());
99 return;
100 }
101 const auto& ctx = *ctx_expected;
102 if (!has_permission(ctx, "refdata::business_centres:write")) {
103 error_reply(nats_, msg, ores::service::error_code::forbidden);
104 return;
105 }
106 service::business_centre_service svc(ctx);
107 auto req = decode<save_business_centre_request>(msg);
108 if (!req) {
109 BOOST_LOG_SEV(business_centre_handler_lg(), warn)
110 << "Failed to decode: " << msg.subject;
111 return;
112 }
113 try {
114 svc.save_business_centre(req->data);
115 BOOST_LOG_SEV(business_centre_handler_lg(), debug)
116 << "Completed " << msg.subject;
117 reply(nats_, msg,
118 save_business_centre_response{.success = true});
119 } catch (const std::exception& e) {
120 BOOST_LOG_SEV(business_centre_handler_lg(), error)
121 << msg.subject << " failed: " << e.what();
122 reply(nats_, msg, save_business_centre_response{
123 .success = false, .message = e.what()});
124 }
125 }
126
127 void remove(ores::nats::message msg) {
128 [[maybe_unused]] const auto correlation_id =
129 log_handler_entry(business_centre_handler_lg(), msg);
130 auto ctx_expected = ores::service::service::make_request_context(
131 ctx_, msg, verifier_);
132 if (!ctx_expected) {
133 error_reply(nats_, msg, ctx_expected.error());
134 return;
135 }
136 const auto& ctx = *ctx_expected;
137 if (!has_permission(ctx, "refdata::business_centres:delete")) {
138 error_reply(nats_, msg, ores::service::error_code::forbidden);
139 return;
140 }
141 service::business_centre_service svc(ctx);
142 auto req = decode<delete_business_centre_request>(msg);
143 if (!req) {
144 BOOST_LOG_SEV(business_centre_handler_lg(), warn)
145 << "Failed to decode: " << msg.subject;
146 return;
147 }
148 try {
149 svc.delete_business_centres(req->codes);
150 BOOST_LOG_SEV(business_centre_handler_lg(), debug)
151 << "Completed " << msg.subject;
152 reply(nats_, msg,
153 delete_business_centre_response{.success = true});
154 } catch (const std::exception& e) {
155 BOOST_LOG_SEV(business_centre_handler_lg(), error)
156 << msg.subject << " failed: " << e.what();
157 reply(nats_, msg, delete_business_centre_response{
158 .success = false, .message = e.what()});
159 }
160 }
161
162 void history(ores::nats::message msg) {
163 [[maybe_unused]] const auto correlation_id =
164 log_handler_entry(business_centre_handler_lg(), msg);
165 auto ctx_expected = ores::service::service::make_request_context(
166 ctx_, msg, verifier_);
167 if (!ctx_expected) {
168 error_reply(nats_, msg, ctx_expected.error());
169 return;
170 }
171 const auto& ctx = *ctx_expected;
172 service::business_centre_service svc(ctx);
173 auto req = decode<get_business_centre_history_request>(msg);
174 if (!req) {
175 BOOST_LOG_SEV(business_centre_handler_lg(), warn)
176 << "Failed to decode: " << msg.subject;
177 return;
178 }
179 try {
180 auto h = svc.get_business_centre_history(req->code);
181 BOOST_LOG_SEV(business_centre_handler_lg(), debug)
182 << "Completed " << msg.subject;
183 reply(nats_, msg, get_business_centre_history_response{
184 .success = true, .history = std::move(h)});
185 } catch (const std::exception& e) {
186 BOOST_LOG_SEV(business_centre_handler_lg(), error)
187 << msg.subject << " failed: " << e.what();
188 reply(nats_, msg, get_business_centre_history_response{
189 .success = false, .message = e.what()});
190 }
191 }
192
193private:
196 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
197};
198
199} // namespace ores::refdata::messaging
200#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