20#ifndef ORES_REFDATA_CORE_MESSAGING_BOOK_STATUS_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_BOOK_STATUS_HANDLER_HPP
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/book_status_protocol.hpp"
32#include "ores.refdata.core/service/book_status_service.hpp"
34namespace ores::refdata::messaging {
37inline auto& book_status_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.book_status_handler");
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;
51class book_status_handler {
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
59 [[maybe_unused]]
const auto correlation_id =
60 log_handler_entry(book_status_handler_lg(), msg);
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
64 error_reply(nats_, msg, ctx_expected.error());
67 const auto& ctx = *ctx_expected;
68 service::book_status_service svc(ctx);
69 get_book_statuses_response resp;
71 resp.book_statuses = svc.list_statuses();
72 BOOST_LOG_SEV(book_status_handler_lg(), debug)
74 }
catch (
const std::exception& e) {
75 BOOST_LOG_SEV(book_status_handler_lg(), error)
76 << msg.
subject <<
" failed: " << e.what();
78 reply(nats_, msg, resp);
82 [[maybe_unused]]
const auto correlation_id =
83 log_handler_entry(book_status_handler_lg(), msg);
84 auto ctx_expected = ores::service::service::make_request_context(
85 ctx_, msg, verifier_);
87 error_reply(nats_, msg, ctx_expected.error());
90 const auto& ctx = *ctx_expected;
91 if (!has_permission(ctx,
"refdata::book_statuses:write")) {
95 service::book_status_service svc(ctx);
96 auto req = decode<save_book_status_request>(msg);
98 BOOST_LOG_SEV(book_status_handler_lg(), warn)
99 <<
"Failed to decode: " << msg.
subject;
103 svc.save_status(req->data);
104 BOOST_LOG_SEV(book_status_handler_lg(), debug)
105 <<
"Completed " << msg.
subject;
106 reply(nats_, msg, save_book_status_response{.success =
true});
107 }
catch (
const std::exception& e) {
108 BOOST_LOG_SEV(book_status_handler_lg(), error)
109 << msg.
subject <<
" failed: " << e.what();
110 reply(nats_, msg, save_book_status_response{
111 .success =
false, .message = e.what()});
116 [[maybe_unused]]
const auto correlation_id =
117 log_handler_entry(book_status_handler_lg(), msg);
118 auto ctx_expected = ores::service::service::make_request_context(
119 ctx_, msg, verifier_);
121 error_reply(nats_, msg, ctx_expected.error());
124 const auto& ctx = *ctx_expected;
125 if (!has_permission(ctx,
"refdata::book_statuses:delete")) {
129 service::book_status_service svc(ctx);
130 auto req = decode<delete_book_status_request>(msg);
132 BOOST_LOG_SEV(book_status_handler_lg(), warn)
133 <<
"Failed to decode: " << msg.
subject;
137 svc.remove_status(req->status);
138 BOOST_LOG_SEV(book_status_handler_lg(), debug)
139 <<
"Completed " << msg.
subject;
141 delete_book_status_response{.success =
true});
142 }
catch (
const std::exception& e) {
143 BOOST_LOG_SEV(book_status_handler_lg(), error)
144 << msg.
subject <<
" failed: " << e.what();
145 reply(nats_, msg, delete_book_status_response{
146 .success =
false, .message = e.what()});
151 [[maybe_unused]]
const auto correlation_id =
152 log_handler_entry(book_status_handler_lg(), msg);
153 auto ctx_expected = ores::service::service::make_request_context(
154 ctx_, msg, verifier_);
156 error_reply(nats_, msg, ctx_expected.error());
159 const auto& ctx = *ctx_expected;
160 service::book_status_service svc(ctx);
161 auto req = decode<get_book_status_history_request>(msg);
163 BOOST_LOG_SEV(book_status_handler_lg(), warn)
164 <<
"Failed to decode: " << msg.
subject;
168 auto h = svc.get_status_history(req->status);
169 BOOST_LOG_SEV(book_status_handler_lg(), debug)
170 <<
"Completed " << msg.
subject;
171 reply(nats_, msg, get_book_status_history_response{
172 .success =
true, .history = std::move(h)});
173 }
catch (
const std::exception& e) {
174 BOOST_LOG_SEV(book_status_handler_lg(), error)
175 << msg.
subject <<
" failed: " << e.what();
176 reply(nats_, msg, get_book_status_history_response{
177 .success =
false, .message = e.what()});
184 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