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;
50class book_status_handler {
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(
std::move(ctx)), verifier_(
std::move(verifier)) {}
58 BOOST_LOG_SEV(book_status_handler_lg(), debug)
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
63 error_reply(nats_, msg, ctx_expected.error());
66 const auto& ctx = *ctx_expected;
67 service::book_status_service svc(ctx);
68 get_book_statuses_response resp;
70 resp.book_statuses = svc.list_statuses();
71 BOOST_LOG_SEV(book_status_handler_lg(), debug)
73 }
catch (
const std::exception& e) {
74 BOOST_LOG_SEV(book_status_handler_lg(), error)
75 << msg.
subject <<
" failed: " << e.what();
77 reply(nats_, msg, resp);
81 BOOST_LOG_SEV(book_status_handler_lg(), debug)
83 auto ctx_expected = ores::service::service::make_request_context(
84 ctx_, msg, verifier_);
86 error_reply(nats_, msg, ctx_expected.error());
89 const auto& ctx = *ctx_expected;
90 if (!has_permission(ctx,
"refdata::book_statuses:write")) {
94 service::book_status_service svc(ctx);
95 auto req = decode<save_book_status_request>(msg);
97 BOOST_LOG_SEV(book_status_handler_lg(), warn)
98 <<
"Failed to decode: " << msg.
subject;
102 svc.save_status(req->data);
103 BOOST_LOG_SEV(book_status_handler_lg(), debug)
104 <<
"Completed " << msg.
subject;
105 reply(nats_, msg, save_book_status_response{.success =
true});
106 }
catch (
const std::exception& e) {
107 BOOST_LOG_SEV(book_status_handler_lg(), error)
108 << msg.
subject <<
" failed: " << e.what();
109 reply(nats_, msg, save_book_status_response{
110 .success =
false, .message = e.what()});
115 BOOST_LOG_SEV(book_status_handler_lg(), debug)
117 auto ctx_expected = ores::service::service::make_request_context(
118 ctx_, msg, verifier_);
120 error_reply(nats_, msg, ctx_expected.error());
123 const auto& ctx = *ctx_expected;
124 if (!has_permission(ctx,
"refdata::book_statuses:delete")) {
128 service::book_status_service svc(ctx);
129 auto req = decode<delete_book_status_request>(msg);
131 BOOST_LOG_SEV(book_status_handler_lg(), warn)
132 <<
"Failed to decode: " << msg.
subject;
136 svc.remove_status(req->status);
137 BOOST_LOG_SEV(book_status_handler_lg(), debug)
138 <<
"Completed " << msg.
subject;
140 delete_book_status_response{.success =
true});
141 }
catch (
const std::exception& e) {
142 BOOST_LOG_SEV(book_status_handler_lg(), error)
143 << msg.
subject <<
" failed: " << e.what();
144 reply(nats_, msg, delete_book_status_response{
145 .success =
false, .message = e.what()});
150 BOOST_LOG_SEV(book_status_handler_lg(), debug)
152 auto ctx_expected = ores::service::service::make_request_context(
153 ctx_, msg, verifier_);
155 error_reply(nats_, msg, ctx_expected.error());
158 const auto& ctx = *ctx_expected;
159 service::book_status_service svc(ctx);
160 auto req = decode<get_book_status_history_request>(msg);
162 BOOST_LOG_SEV(book_status_handler_lg(), warn)
163 <<
"Failed to decode: " << msg.
subject;
167 auto h = svc.get_status_history(req->status);
168 BOOST_LOG_SEV(book_status_handler_lg(), debug)
169 <<
"Completed " << msg.
subject;
170 reply(nats_, msg, get_book_status_history_response{
171 .success =
true, .history = std::move(h)});
172 }
catch (
const std::exception& e) {
173 BOOST_LOG_SEV(book_status_handler_lg(), error)
174 << msg.
subject <<
" failed: " << e.what();
175 reply(nats_, msg, get_book_status_history_response{
176 .success =
false, .message = e.what()});
183 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