ORE Studio 0.0.4
Loading...
Searching...
No Matches
coding_scheme_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_DQ_CORE_MESSAGING_CODING_SCHEME_HANDLER_HPP
21#define ORES_DQ_CORE_MESSAGING_CODING_SCHEME_HANDLER_HPP
22
23#include <optional>
24#include <stdexcept>
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.dq.api/messaging/coding_scheme_protocol.hpp"
32#include "ores.dq.core/service/coding_scheme_service.hpp"
33#include "ores.logging/make_logger.hpp"
34
35namespace ores::dq::messaging {
36
37using ores::service::messaging::reply;
38using ores::service::messaging::decode;
39using ores::service::messaging::stamp;
40using ores::service::messaging::error_reply;
41using ores::service::messaging::has_permission;
42using namespace ores::logging;
43
44namespace {
45inline auto& coding_scheme_handler_lg() {
46 static auto instance = ores::logging::make_logger("ores.dq.messaging.coding_scheme_handler");
47 return instance;
48}
49} // namespace
50
51class coding_scheme_handler {
52public:
53 coding_scheme_handler(
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
58
59 // =========================================================================
60 // Coding Scheme Authority Types
61 // =========================================================================
62
63 void list_authority_types(ores::nats::message msg) {
64 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
65 auto req =
66 decode<get_coding_scheme_authority_types_request>(msg);
67 if (!req) {
68 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
69 return;
70 }
71 auto ctx_expected = ores::service::service::make_request_context(
72 ctx_, msg, verifier_);
73 if (!ctx_expected) {
74 error_reply(nats_, msg, ctx_expected.error());
75 return;
76 }
77 const auto& ctx = *ctx_expected;
78 service::coding_scheme_service svc(ctx);
79 try {
80 const auto items = svc.list_authority_types();
81 get_coding_scheme_authority_types_response resp;
82 resp.coding_scheme_authority_types = items;
83 resp.total_available_count = static_cast<int>(items.size());
84 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
85 reply(nats_, msg, resp);
86 } catch (const std::exception& e) {
87 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
88 get_coding_scheme_authority_types_response resp;
89 resp.total_available_count = 0;
90 reply(nats_, msg, resp);
91 }
92 }
93
94 void save_authority_type(ores::nats::message msg) {
95 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
96 auto req =
97 decode<save_coding_scheme_authority_type_request>(msg);
98 if (!req) {
99 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
100 return;
101 }
102 auto ctx_expected = ores::service::service::make_request_context(
103 ctx_, msg, verifier_);
104 if (!ctx_expected) {
105 error_reply(nats_, msg, ctx_expected.error());
106 return;
107 }
108 const auto& ctx = *ctx_expected;
109 if (!has_permission(ctx, "dq::coding_scheme_authority_types:write")) {
110 error_reply(nats_, msg, ores::service::error_code::forbidden);
111 return;
112 }
113 service::coding_scheme_service svc(ctx);
114 try {
115 stamp(req->data, ctx);
116 svc.save_authority_type(req->data);
117 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
118 reply(nats_, msg,
119 save_coding_scheme_authority_type_response{true, {}});
120 } catch (const std::exception& e) {
121 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
122 reply(nats_, msg,
123 save_coding_scheme_authority_type_response{false, e.what()});
124 }
125 }
126
127 void delete_authority_types(ores::nats::message msg) {
128 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
129 auto req =
130 decode<delete_coding_scheme_authority_type_request>(msg);
131 if (!req) {
132 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
133 return;
134 }
135 auto ctx_expected = ores::service::service::make_request_context(
136 ctx_, msg, verifier_);
137 if (!ctx_expected) {
138 error_reply(nats_, msg, ctx_expected.error());
139 return;
140 }
141 const auto& ctx = *ctx_expected;
142 if (!has_permission(ctx, "dq::coding_scheme_authority_types:delete")) {
143 error_reply(nats_, msg, ores::service::error_code::forbidden);
144 return;
145 }
146 service::coding_scheme_service svc(ctx);
147 try {
148 svc.remove_authority_types(req->types);
149 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
150 reply(nats_, msg,
151 delete_coding_scheme_authority_type_response{true, {}});
152 } catch (const std::exception& e) {
153 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
154 reply(nats_, msg,
155 delete_coding_scheme_authority_type_response{false, e.what()});
156 }
157 }
158
159 void authority_type_history(ores::nats::message msg) {
160 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
161 auto req =
162 decode<get_coding_scheme_authority_type_history_request>(msg);
163 if (!req) {
164 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
165 return;
166 }
167 auto ctx_expected = ores::service::service::make_request_context(
168 ctx_, msg, verifier_);
169 if (!ctx_expected) {
170 error_reply(nats_, msg, ctx_expected.error());
171 return;
172 }
173 const auto& ctx = *ctx_expected;
174 service::coding_scheme_service svc(ctx);
175 try {
176 const auto history = svc.get_authority_type_history(req->type);
177 get_coding_scheme_authority_type_history_response resp;
178 resp.success = true;
179 resp.history = history;
180 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
181 reply(nats_, msg, resp);
182 } catch (const std::exception& e) {
183 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
184 get_coding_scheme_authority_type_history_response resp;
185 resp.success = false;
186 resp.message = e.what();
187 reply(nats_, msg, resp);
188 }
189 }
190
191 // =========================================================================
192 // Coding Schemes
193 // =========================================================================
194
195 void list(ores::nats::message msg) {
196 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
197 auto req = decode<get_coding_schemes_request>(msg);
198 if (!req) {
199 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
200 return;
201 }
202 auto ctx_expected = ores::service::service::make_request_context(
203 ctx_, msg, verifier_);
204 if (!ctx_expected) {
205 error_reply(nats_, msg, ctx_expected.error());
206 return;
207 }
208 const auto& ctx = *ctx_expected;
209 service::coding_scheme_service svc(ctx);
210 try {
211 const auto items = svc.list_coding_schemes(
212 static_cast<std::uint32_t>(req->offset),
213 static_cast<std::uint32_t>(req->limit));
214 const auto count = svc.get_coding_scheme_count();
215 get_coding_schemes_response resp;
216 resp.coding_schemes = items;
217 resp.total_available_count = static_cast<int>(count);
218 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
219 reply(nats_, msg, resp);
220 } catch (const std::exception& e) {
221 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
222 get_coding_schemes_response resp;
223 resp.total_available_count = 0;
224 reply(nats_, msg, resp);
225 }
226 }
227
228 void save(ores::nats::message msg) {
229 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
230 auto req = decode<save_coding_scheme_request>(msg);
231 if (!req) {
232 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
233 return;
234 }
235 auto ctx_expected = ores::service::service::make_request_context(
236 ctx_, msg, verifier_);
237 if (!ctx_expected) {
238 error_reply(nats_, msg, ctx_expected.error());
239 return;
240 }
241 const auto& ctx = *ctx_expected;
242 if (!has_permission(ctx, "dq::coding_schemes:write")) {
243 error_reply(nats_, msg, ores::service::error_code::forbidden);
244 return;
245 }
246 service::coding_scheme_service svc(ctx);
247 try {
248 stamp(req->data, ctx);
249 svc.save_coding_scheme(req->data);
250 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
251 reply(nats_, msg, save_coding_scheme_response{true, {}});
252 } catch (const std::exception& e) {
253 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
254 reply(nats_, msg, save_coding_scheme_response{false, e.what()});
255 }
256 }
257
258 void remove(ores::nats::message msg) {
259 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
260 auto req = decode<delete_coding_scheme_request>(msg);
261 if (!req) {
262 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
263 return;
264 }
265 auto ctx_expected = ores::service::service::make_request_context(
266 ctx_, msg, verifier_);
267 if (!ctx_expected) {
268 error_reply(nats_, msg, ctx_expected.error());
269 return;
270 }
271 const auto& ctx = *ctx_expected;
272 if (!has_permission(ctx, "dq::coding_schemes:delete")) {
273 error_reply(nats_, msg, ores::service::error_code::forbidden);
274 return;
275 }
276 service::coding_scheme_service svc(ctx);
277 try {
278 svc.remove_coding_schemes(req->codes);
279 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
280 reply(nats_, msg, delete_coding_scheme_response{true, {}});
281 } catch (const std::exception& e) {
282 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
283 reply(nats_, msg, delete_coding_scheme_response{false, e.what()});
284 }
285 }
286
287 void history(ores::nats::message msg) {
288 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Handling " << msg.subject;
289 auto req = decode<get_coding_scheme_history_request>(msg);
290 if (!req) {
291 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) << "Failed to decode: " << msg.subject;
292 return;
293 }
294 auto ctx_expected = ores::service::service::make_request_context(
295 ctx_, msg, verifier_);
296 if (!ctx_expected) {
297 error_reply(nats_, msg, ctx_expected.error());
298 return;
299 }
300 const auto& ctx = *ctx_expected;
301 service::coding_scheme_service svc(ctx);
302 try {
303 const auto hist = svc.get_coding_scheme_history(req->code);
304 get_coding_scheme_history_response resp;
305 resp.success = true;
306 resp.history = hist;
307 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) << "Completed " << msg.subject;
308 reply(nats_, msg, resp);
309 } catch (const std::exception& e) {
310 BOOST_LOG_SEV(coding_scheme_handler_lg(), error) << msg.subject << " failed: " << e.what();
311 get_coding_scheme_history_response resp;
312 resp.success = false;
313 resp.message = e.what();
314 reply(nats_, msg, resp);
315 }
316 }
317
318private:
319
322 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
323};
324
325} // namespace ores::dq::messaging
326
327#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