20#ifndef ORES_REFDATA_CORE_MESSAGING_CONTACT_TYPE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_CONTACT_TYPE_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/contact_type_protocol.hpp"
32#include "ores.refdata.core/service/contact_type_service.hpp"
34namespace ores::refdata::messaging {
37inline auto& contact_type_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.contact_type_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 contact_type_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(contact_type_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::contact_type_service svc(ctx);
69 get_contact_types_response resp;
71 resp.contact_types = svc.list_types();
72 resp.total_available_count =
73 static_cast<int>(resp.contact_types.size());
74 BOOST_LOG_SEV(contact_type_handler_lg(), debug)
76 }
catch (
const std::exception& e) {
77 BOOST_LOG_SEV(contact_type_handler_lg(), error)
78 << msg.
subject <<
" failed: " << e.what();
80 reply(nats_, msg, resp);
84 [[maybe_unused]]
const auto correlation_id =
85 log_handler_entry(contact_type_handler_lg(), msg);
86 auto ctx_expected = ores::service::service::make_request_context(
87 ctx_, msg, verifier_);
89 error_reply(nats_, msg, ctx_expected.error());
92 const auto& ctx = *ctx_expected;
93 if (!has_permission(ctx,
"refdata::contact_types:write")) {
97 service::contact_type_service svc(ctx);
98 auto req = decode<save_contact_type_request>(msg);
100 BOOST_LOG_SEV(contact_type_handler_lg(), warn)
101 <<
"Failed to decode: " << msg.
subject;
105 svc.save_type(req->data);
106 BOOST_LOG_SEV(contact_type_handler_lg(), debug)
107 <<
"Completed " << msg.
subject;
109 save_contact_type_response{.success =
true});
110 }
catch (
const std::exception& e) {
111 BOOST_LOG_SEV(contact_type_handler_lg(), error)
112 << msg.
subject <<
" failed: " << e.what();
113 reply(nats_, msg, save_contact_type_response{
114 .success =
false, .message = e.what()});
119 [[maybe_unused]]
const auto correlation_id =
120 log_handler_entry(contact_type_handler_lg(), msg);
121 auto ctx_expected = ores::service::service::make_request_context(
122 ctx_, msg, verifier_);
124 error_reply(nats_, msg, ctx_expected.error());
127 const auto& ctx = *ctx_expected;
128 if (!has_permission(ctx,
"refdata::contact_types:delete")) {
132 service::contact_type_service svc(ctx);
133 auto req = decode<delete_contact_type_request>(msg);
135 BOOST_LOG_SEV(contact_type_handler_lg(), warn)
136 <<
"Failed to decode: " << msg.
subject;
140 svc.remove_type(req->type);
141 BOOST_LOG_SEV(contact_type_handler_lg(), debug)
142 <<
"Completed " << msg.
subject;
144 delete_contact_type_response{.success =
true});
145 }
catch (
const std::exception& e) {
146 BOOST_LOG_SEV(contact_type_handler_lg(), error)
147 << msg.
subject <<
" failed: " << e.what();
148 reply(nats_, msg, delete_contact_type_response{
149 .success =
false, .message = e.what()});
154 [[maybe_unused]]
const auto correlation_id =
155 log_handler_entry(contact_type_handler_lg(), msg);
156 auto ctx_expected = ores::service::service::make_request_context(
157 ctx_, msg, verifier_);
159 error_reply(nats_, msg, ctx_expected.error());
162 const auto& ctx = *ctx_expected;
163 service::contact_type_service svc(ctx);
164 auto req = decode<get_contact_type_history_request>(msg);
166 BOOST_LOG_SEV(contact_type_handler_lg(), warn)
167 <<
"Failed to decode: " << msg.
subject;
171 auto h = svc.get_type_history(req->type);
172 BOOST_LOG_SEV(contact_type_handler_lg(), debug)
173 <<
"Completed " << msg.
subject;
174 reply(nats_, msg, get_contact_type_history_response{
175 .success =
true, .history = std::move(h)});
176 }
catch (
const std::exception& e) {
177 BOOST_LOG_SEV(contact_type_handler_lg(), error)
178 << msg.
subject <<
" failed: " << e.what();
179 reply(nats_, msg, get_contact_type_history_response{
180 .success =
false, .message = e.what()});
187 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