20#ifndef ORES_REFDATA_CORE_MESSAGING_PARTY_STATUS_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PARTY_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/party_status_protocol.hpp"
32#include "ores.refdata.core/service/party_status_service.hpp"
34namespace ores::refdata::messaging {
37inline auto& party_status_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.party_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 party_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(party_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::party_status_service svc(ctx);
68 get_party_statuses_response resp;
70 resp.party_statuses = svc.list_statuses();
71 resp.total_available_count =
72 static_cast<int>(resp.party_statuses.size());
73 BOOST_LOG_SEV(party_status_handler_lg(), debug)
75 }
catch (
const std::exception& e) {
76 BOOST_LOG_SEV(party_status_handler_lg(), error)
77 << msg.
subject <<
" failed: " << e.what();
79 reply(nats_, msg, resp);
83 BOOST_LOG_SEV(party_status_handler_lg(), debug)
85 auto ctx_expected = ores::service::service::make_request_context(
86 ctx_, msg, verifier_);
88 error_reply(nats_, msg, ctx_expected.error());
91 const auto& ctx = *ctx_expected;
92 if (!has_permission(ctx,
"refdata::party_statuses:write")) {
96 service::party_status_service svc(ctx);
97 auto req = decode<save_party_status_request>(msg);
99 BOOST_LOG_SEV(party_status_handler_lg(), warn)
100 <<
"Failed to decode: " << msg.
subject;
104 svc.save_status(req->data);
105 BOOST_LOG_SEV(party_status_handler_lg(), debug)
106 <<
"Completed " << msg.
subject;
107 reply(nats_, msg, save_party_status_response{.success =
true});
108 }
catch (
const std::exception& e) {
109 BOOST_LOG_SEV(party_status_handler_lg(), error)
110 << msg.
subject <<
" failed: " << e.what();
111 reply(nats_, msg, save_party_status_response{
112 .success =
false, .message = e.what()});
117 BOOST_LOG_SEV(party_status_handler_lg(), debug)
119 auto ctx_expected = ores::service::service::make_request_context(
120 ctx_, msg, verifier_);
122 error_reply(nats_, msg, ctx_expected.error());
125 const auto& ctx = *ctx_expected;
126 if (!has_permission(ctx,
"refdata::party_statuses:delete")) {
130 service::party_status_service svc(ctx);
131 auto req = decode<delete_party_status_request>(msg);
133 BOOST_LOG_SEV(party_status_handler_lg(), warn)
134 <<
"Failed to decode: " << msg.
subject;
138 svc.remove_status(req->status);
139 BOOST_LOG_SEV(party_status_handler_lg(), debug)
140 <<
"Completed " << msg.
subject;
142 delete_party_status_response{.success =
true});
143 }
catch (
const std::exception& e) {
144 BOOST_LOG_SEV(party_status_handler_lg(), error)
145 << msg.
subject <<
" failed: " << e.what();
146 reply(nats_, msg, delete_party_status_response{
147 .success =
false, .message = e.what()});
152 BOOST_LOG_SEV(party_status_handler_lg(), debug)
154 auto ctx_expected = ores::service::service::make_request_context(
155 ctx_, msg, verifier_);
157 error_reply(nats_, msg, ctx_expected.error());
160 const auto& ctx = *ctx_expected;
161 service::party_status_service svc(ctx);
162 auto req = decode<get_party_status_history_request>(msg);
164 BOOST_LOG_SEV(party_status_handler_lg(), warn)
165 <<
"Failed to decode: " << msg.
subject;
169 auto h = svc.get_status_history(req->status);
170 BOOST_LOG_SEV(party_status_handler_lg(), debug)
171 <<
"Completed " << msg.
subject;
172 reply(nats_, msg, get_party_status_history_response{
173 .success =
true, .history = std::move(h)});
174 }
catch (
const std::exception& e) {
175 BOOST_LOG_SEV(party_status_handler_lg(), error)
176 << msg.
subject <<
" failed: " << e.what();
177 reply(nats_, msg, get_party_status_history_response{
178 .success =
false, .message = e.what()});
185 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