20#ifndef ORES_DQ_CORE_MESSAGING_CODING_SCHEME_HANDLER_HPP
21#define ORES_DQ_CORE_MESSAGING_CODING_SCHEME_HANDLER_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.dq.api/messaging/coding_scheme_protocol.hpp"
32#include "ores.dq.core/service/coding_scheme_service.hpp"
33#include "ores.logging/make_logger.hpp"
35namespace ores::dq::messaging {
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;
45inline auto& coding_scheme_handler_lg() {
46 static auto instance = ores::logging::make_logger(
"ores.dq.messaging.coding_scheme_handler");
51class coding_scheme_handler {
53 coding_scheme_handler(
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
64 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
66 decode<get_coding_scheme_authority_types_request>(msg);
68 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
71 auto ctx_expected = ores::service::service::make_request_context(
72 ctx_, msg, verifier_);
74 error_reply(nats_, msg, ctx_expected.error());
77 const auto& ctx = *ctx_expected;
78 service::coding_scheme_service svc(ctx);
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);
95 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
97 decode<save_coding_scheme_authority_type_request>(msg);
99 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
102 auto ctx_expected = ores::service::service::make_request_context(
103 ctx_, msg, verifier_);
105 error_reply(nats_, msg, ctx_expected.error());
108 const auto& ctx = *ctx_expected;
109 if (!has_permission(ctx,
"dq::coding_scheme_authority_types:write")) {
113 service::coding_scheme_service svc(ctx);
115 stamp(req->data, ctx);
116 svc.save_authority_type(req->data);
117 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Completed " << msg.
subject;
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();
123 save_coding_scheme_authority_type_response{
false, e.what()});
128 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
130 decode<delete_coding_scheme_authority_type_request>(msg);
132 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
135 auto ctx_expected = ores::service::service::make_request_context(
136 ctx_, msg, verifier_);
138 error_reply(nats_, msg, ctx_expected.error());
141 const auto& ctx = *ctx_expected;
142 if (!has_permission(ctx,
"dq::coding_scheme_authority_types:delete")) {
146 service::coding_scheme_service svc(ctx);
148 svc.remove_authority_types(req->types);
149 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Completed " << msg.
subject;
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();
155 delete_coding_scheme_authority_type_response{
false, e.what()});
160 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
162 decode<get_coding_scheme_authority_type_history_request>(msg);
164 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
167 auto ctx_expected = ores::service::service::make_request_context(
168 ctx_, msg, verifier_);
170 error_reply(nats_, msg, ctx_expected.error());
173 const auto& ctx = *ctx_expected;
174 service::coding_scheme_service svc(ctx);
176 const auto history = svc.get_authority_type_history(req->type);
177 get_coding_scheme_authority_type_history_response resp;
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);
196 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
197 auto req = decode<get_coding_schemes_request>(msg);
199 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
202 auto ctx_expected = ores::service::service::make_request_context(
203 ctx_, msg, verifier_);
205 error_reply(nats_, msg, ctx_expected.error());
208 const auto& ctx = *ctx_expected;
209 service::coding_scheme_service svc(ctx);
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);
229 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
230 auto req = decode<save_coding_scheme_request>(msg);
232 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
235 auto ctx_expected = ores::service::service::make_request_context(
236 ctx_, msg, verifier_);
238 error_reply(nats_, msg, ctx_expected.error());
241 const auto& ctx = *ctx_expected;
242 if (!has_permission(ctx,
"dq::coding_schemes:write")) {
246 service::coding_scheme_service svc(ctx);
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()});
259 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
260 auto req = decode<delete_coding_scheme_request>(msg);
262 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
265 auto ctx_expected = ores::service::service::make_request_context(
266 ctx_, msg, verifier_);
268 error_reply(nats_, msg, ctx_expected.error());
271 const auto& ctx = *ctx_expected;
272 if (!has_permission(ctx,
"dq::coding_schemes:delete")) {
276 service::coding_scheme_service svc(ctx);
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()});
288 BOOST_LOG_SEV(coding_scheme_handler_lg(), debug) <<
"Handling " << msg.
subject;
289 auto req = decode<get_coding_scheme_history_request>(msg);
291 BOOST_LOG_SEV(coding_scheme_handler_lg(), warn) <<
"Failed to decode: " << msg.
subject;
294 auto ctx_expected = ores::service::service::make_request_context(
295 ctx_, msg, verifier_);
297 error_reply(nats_, msg, ctx_expected.error());
300 const auto& ctx = *ctx_expected;
301 service::coding_scheme_service svc(ctx);
303 const auto hist = svc.get_coding_scheme_history(req->code);
304 get_coding_scheme_history_response resp;
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);
322 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