ORE Studio 0.0.4
Loading...
Searching...
No Matches
party_identifier_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_IDENTIFIER_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PARTY_IDENTIFIER_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_identifier_protocol.hpp"
33#include "ores.refdata.core/service/party_identifier_service.hpp"
34
35namespace ores::refdata::messaging {
36
37namespace {
38inline auto& party_identifier_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.party_identifier_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 namespace ores::logging;
50
51class party_identifier_handler {
52public:
53 party_identifier_handler(ores::nats::service::client& nats,
55 std::optional<ores::security::jwt::jwt_authenticator> verifier)
56 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
57
58 void list(ores::nats::message msg) {
59 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
60 << "Handling " << msg.subject;
61 auto ctx_expected = ores::service::service::make_request_context(
62 ctx_, msg, verifier_);
63 if (!ctx_expected) {
64 error_reply(nats_, msg, ctx_expected.error());
65 return;
66 }
67 const auto& ctx = *ctx_expected;
68 service::party_identifier_service svc(ctx);
69 auto req = decode<get_party_identifiers_request>(msg);
70 if (!req) {
71 BOOST_LOG_SEV(party_identifier_handler_lg(), warn)
72 << "Failed to decode: " << msg.subject;
73 return;
74 }
75 try {
76 boost::uuids::string_generator gen;
77 auto items = svc.list_party_identifiers_by_party(
78 gen(req->party_id));
79 get_party_identifiers_response resp;
80 resp.identifiers = std::move(items);
81 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
82 << "Completed " << msg.subject;
83 reply(nats_, msg, resp);
84 } catch (const std::exception& e) {
85 BOOST_LOG_SEV(party_identifier_handler_lg(), error)
86 << msg.subject << " failed: " << e.what();
87 reply(nats_, msg, get_party_identifiers_response{});
88 }
89 }
90
91 void save(ores::nats::message msg) {
92 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
93 << "Handling " << msg.subject;
94 auto ctx_expected = ores::service::service::make_request_context(
95 ctx_, msg, verifier_);
96 if (!ctx_expected) {
97 error_reply(nats_, msg, ctx_expected.error());
98 return;
99 }
100 const auto& ctx = *ctx_expected;
101 if (!has_permission(ctx, "refdata::party_identifiers:write")) {
102 error_reply(nats_, msg, ores::service::error_code::forbidden);
103 return;
104 }
105 service::party_identifier_service svc(ctx);
106 auto req = decode<save_party_identifier_request>(msg);
107 if (!req) {
108 BOOST_LOG_SEV(party_identifier_handler_lg(), warn)
109 << "Failed to decode: " << msg.subject;
110 return;
111 }
112 try {
113 svc.save_party_identifier(req->data);
114 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
115 << "Completed " << msg.subject;
116 reply(nats_, msg,
117 save_party_identifier_response{.success = true});
118 } catch (const std::exception& e) {
119 BOOST_LOG_SEV(party_identifier_handler_lg(), error)
120 << msg.subject << " failed: " << e.what();
121 reply(nats_, msg, save_party_identifier_response{
122 .success = false, .message = e.what()});
123 }
124 }
125
126 void del(ores::nats::message msg) {
127 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
128 << "Handling " << msg.subject;
129 auto ctx_expected = ores::service::service::make_request_context(
130 ctx_, msg, verifier_);
131 if (!ctx_expected) {
132 error_reply(nats_, msg, ctx_expected.error());
133 return;
134 }
135 const auto& ctx = *ctx_expected;
136 if (!has_permission(ctx, "refdata::party_identifiers:delete")) {
137 error_reply(nats_, msg, ores::service::error_code::forbidden);
138 return;
139 }
140 service::party_identifier_service svc(ctx);
141 auto req = decode<delete_party_identifier_request>(msg);
142 if (!req) {
143 BOOST_LOG_SEV(party_identifier_handler_lg(), warn)
144 << "Failed to decode: " << msg.subject;
145 return;
146 }
147 try {
148 boost::uuids::string_generator gen;
149 for (const auto& id_str : req->ids)
150 svc.remove_party_identifier(gen(id_str));
151 BOOST_LOG_SEV(party_identifier_handler_lg(), debug)
152 << "Completed " << msg.subject;
153 reply(nats_, msg,
154 delete_party_identifier_response{.success = true});
155 } catch (const std::exception& e) {
156 BOOST_LOG_SEV(party_identifier_handler_lg(), error)
157 << msg.subject << " failed: " << e.what();
158 reply(nats_, msg, delete_party_identifier_response{
159 .success = false, .message = e.what()});
160 }
161 }
162
163private:
166 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
167};
168
169} // namespace ores::refdata::messaging
170#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