ORE Studio 0.0.4
Loading...
Searching...
No Matches
party_contact_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_REFDATA_CORE_MESSAGING_PARTY_CONTACT_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PARTY_CONTACT_HANDLER_HPP
22
23#include <optional>
24#include <boost/uuid/string_generator.hpp>
25#include "ores.logging/make_logger.hpp"
26#include "ores.nats/domain/message.hpp"
27#include "ores.nats/service/client.hpp"
28#include "ores.database/domain/context.hpp"
29#include "ores.security/jwt/jwt_authenticator.hpp"
30#include "ores.service/messaging/handler_helpers.hpp"
31#include "ores.service/service/request_context.hpp"
32#include "ores.refdata.api/messaging/party_contact_information_protocol.hpp"
33#include "ores.refdata.core/service/party_contact_information_service.hpp"
34
35namespace ores::refdata::messaging {
36
37namespace {
38inline auto& party_contact_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.party_contact_handler");
41 return instance;
42}
43} // namespace
44
45using ores::service::messaging::reply;
46using ores::service::messaging::decode;
47using ores::service::messaging::error_reply;
48using ores::service::messaging::has_permission;
49using ores::service::messaging::log_handler_entry;
50using namespace ores::logging;
51
52class party_contact_handler {
53public:
54 party_contact_handler(ores::nats::service::client& nats,
56 std::optional<ores::security::jwt::jwt_authenticator> verifier)
57 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
58
59 void list(ores::nats::message msg) {
60 [[maybe_unused]] const auto correlation_id =
61 log_handler_entry(party_contact_handler_lg(), msg);
62 auto ctx_expected = ores::service::service::make_request_context(
63 ctx_, msg, verifier_);
64 if (!ctx_expected) {
65 error_reply(nats_, msg, ctx_expected.error());
66 return;
67 }
68 const auto& ctx = *ctx_expected;
69 service::party_contact_information_service svc(ctx);
70 auto req = decode<get_party_contact_informations_request>(msg);
71 if (!req) {
72 BOOST_LOG_SEV(party_contact_handler_lg(), warn)
73 << "Failed to decode: " << msg.subject;
74 return;
75 }
76 try {
77 boost::uuids::string_generator gen;
78 auto items =
79 svc.list_party_contact_informations_by_party(
80 gen(req->party_id));
81 get_party_contact_informations_response resp;
82 resp.contact_informations = std::move(items);
83 BOOST_LOG_SEV(party_contact_handler_lg(), debug)
84 << "Completed " << msg.subject;
85 reply(nats_, msg, resp);
86 } catch (const std::exception& e) {
87 BOOST_LOG_SEV(party_contact_handler_lg(), error)
88 << msg.subject << " failed: " << e.what();
89 reply(nats_, msg,
90 get_party_contact_informations_response{});
91 }
92 }
93
94 void save(ores::nats::message msg) {
95 [[maybe_unused]] const auto correlation_id =
96 log_handler_entry(party_contact_handler_lg(), msg);
97 auto ctx_expected = ores::service::service::make_request_context(
98 ctx_, msg, verifier_);
99 if (!ctx_expected) {
100 error_reply(nats_, msg, ctx_expected.error());
101 return;
102 }
103 const auto& ctx = *ctx_expected;
104 if (!has_permission(ctx, "refdata::party_contacts:write")) {
105 error_reply(nats_, msg, ores::service::error_code::forbidden);
106 return;
107 }
108 service::party_contact_information_service svc(ctx);
109 auto req = decode<save_party_contact_information_request>(msg);
110 if (!req) {
111 BOOST_LOG_SEV(party_contact_handler_lg(), warn)
112 << "Failed to decode: " << msg.subject;
113 return;
114 }
115 try {
116 svc.save_party_contact_information(req->data);
117 BOOST_LOG_SEV(party_contact_handler_lg(), debug)
118 << "Completed " << msg.subject;
119 reply(nats_, msg,
120 save_party_contact_information_response{.success = true});
121 } catch (const std::exception& e) {
122 BOOST_LOG_SEV(party_contact_handler_lg(), error)
123 << msg.subject << " failed: " << e.what();
124 reply(nats_, msg, save_party_contact_information_response{
125 .success = false, .message = e.what()});
126 }
127 }
128
129 void remove(ores::nats::message msg) {
130 [[maybe_unused]] const auto correlation_id =
131 log_handler_entry(party_contact_handler_lg(), msg);
132 auto ctx_expected = ores::service::service::make_request_context(
133 ctx_, msg, verifier_);
134 if (!ctx_expected) {
135 error_reply(nats_, msg, ctx_expected.error());
136 return;
137 }
138 const auto& ctx = *ctx_expected;
139 if (!has_permission(ctx, "refdata::party_contacts:delete")) {
140 error_reply(nats_, msg, ores::service::error_code::forbidden);
141 return;
142 }
143 service::party_contact_information_service svc(ctx);
144 auto req = decode<delete_party_contact_information_request>(msg);
145 if (!req) {
146 BOOST_LOG_SEV(party_contact_handler_lg(), warn)
147 << "Failed to decode: " << msg.subject;
148 return;
149 }
150 try {
151 boost::uuids::string_generator gen;
152 for (const auto& id_str : req->ids)
153 svc.remove_party_contact_information(gen(id_str));
154 BOOST_LOG_SEV(party_contact_handler_lg(), debug)
155 << "Completed " << msg.subject;
156 reply(nats_, msg,
157 delete_party_contact_information_response{.success = true});
158 } catch (const std::exception& e) {
159 BOOST_LOG_SEV(party_contact_handler_lg(), error)
160 << msg.subject << " failed: " << e.what();
161 reply(nats_, msg,
162 delete_party_contact_information_response{
163 .success = false, .message = e.what()});
164 }
165 }
166
167private:
170 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
171};
172
173} // namespace ores::refdata::messaging
174#endif
STL namespace.
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