ORE Studio 0.0.4
Loading...
Searching...
No Matches
party_type_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_TYPE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PARTY_TYPE_HANDLER_HPP
22
23#include <optional>
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_type_protocol.hpp"
32#include "ores.refdata.core/service/party_type_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& party_type_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.party_type_handler");
40 return instance;
41}
42} // namespace
43
44using ores::service::messaging::reply;
45using ores::service::messaging::decode;
46using ores::service::messaging::error_reply;
47using ores::service::messaging::has_permission;
48using namespace ores::logging;
49
50class party_type_handler {
51public:
52 party_type_handler(ores::nats::service::client& nats,
54 std::optional<ores::security::jwt::jwt_authenticator> verifier)
55 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
56
57 void list(ores::nats::message msg) {
58 BOOST_LOG_SEV(party_type_handler_lg(), debug)
59 << "Handling " << msg.subject;
60 auto ctx_expected = ores::service::service::make_request_context(
61 ctx_, msg, verifier_);
62 if (!ctx_expected) {
63 error_reply(nats_, msg, ctx_expected.error());
64 return;
65 }
66 const auto& ctx = *ctx_expected;
67 service::party_type_service svc(ctx);
68 get_party_types_response resp;
69 try {
70 resp.party_types = svc.list_types();
71 resp.total_available_count =
72 static_cast<int>(resp.party_types.size());
73 BOOST_LOG_SEV(party_type_handler_lg(), debug)
74 << "Completed " << msg.subject;
75 } catch (const std::exception& e) {
76 BOOST_LOG_SEV(party_type_handler_lg(), error)
77 << msg.subject << " failed: " << e.what();
78 }
79 reply(nats_, msg, resp);
80 }
81
82 void save(ores::nats::message msg) {
83 BOOST_LOG_SEV(party_type_handler_lg(), debug)
84 << "Handling " << msg.subject;
85 auto ctx_expected = ores::service::service::make_request_context(
86 ctx_, msg, verifier_);
87 if (!ctx_expected) {
88 error_reply(nats_, msg, ctx_expected.error());
89 return;
90 }
91 const auto& ctx = *ctx_expected;
92 if (!has_permission(ctx, "refdata::party_types:write")) {
93 error_reply(nats_, msg, ores::service::error_code::forbidden);
94 return;
95 }
96 service::party_type_service svc(ctx);
97 auto req = decode<save_party_type_request>(msg);
98 if (!req) {
99 BOOST_LOG_SEV(party_type_handler_lg(), warn)
100 << "Failed to decode: " << msg.subject;
101 return;
102 }
103 try {
104 svc.save_type(req->data);
105 BOOST_LOG_SEV(party_type_handler_lg(), debug)
106 << "Completed " << msg.subject;
107 reply(nats_, msg, save_party_type_response{.success = true});
108 } catch (const std::exception& e) {
109 BOOST_LOG_SEV(party_type_handler_lg(), error)
110 << msg.subject << " failed: " << e.what();
111 reply(nats_, msg, save_party_type_response{
112 .success = false, .message = e.what()});
113 }
114 }
115
116 void del(ores::nats::message msg) {
117 BOOST_LOG_SEV(party_type_handler_lg(), debug)
118 << "Handling " << msg.subject;
119 auto ctx_expected = ores::service::service::make_request_context(
120 ctx_, msg, verifier_);
121 if (!ctx_expected) {
122 error_reply(nats_, msg, ctx_expected.error());
123 return;
124 }
125 const auto& ctx = *ctx_expected;
126 if (!has_permission(ctx, "refdata::party_types:delete")) {
127 error_reply(nats_, msg, ores::service::error_code::forbidden);
128 return;
129 }
130 service::party_type_service svc(ctx);
131 auto req = decode<delete_party_type_request>(msg);
132 if (!req) {
133 BOOST_LOG_SEV(party_type_handler_lg(), warn)
134 << "Failed to decode: " << msg.subject;
135 return;
136 }
137 try {
138 svc.remove_type(req->type);
139 BOOST_LOG_SEV(party_type_handler_lg(), debug)
140 << "Completed " << msg.subject;
141 reply(nats_, msg, delete_party_type_response{.success = true});
142 } catch (const std::exception& e) {
143 BOOST_LOG_SEV(party_type_handler_lg(), error)
144 << msg.subject << " failed: " << e.what();
145 reply(nats_, msg, delete_party_type_response{
146 .success = false, .message = e.what()});
147 }
148 }
149
150 void history(ores::nats::message msg) {
151 BOOST_LOG_SEV(party_type_handler_lg(), debug)
152 << "Handling " << msg.subject;
153 auto ctx_expected = ores::service::service::make_request_context(
154 ctx_, msg, verifier_);
155 if (!ctx_expected) {
156 error_reply(nats_, msg, ctx_expected.error());
157 return;
158 }
159 const auto& ctx = *ctx_expected;
160 service::party_type_service svc(ctx);
161 auto req = decode<get_party_type_history_request>(msg);
162 if (!req) {
163 BOOST_LOG_SEV(party_type_handler_lg(), warn)
164 << "Failed to decode: " << msg.subject;
165 return;
166 }
167 try {
168 auto h = svc.get_type_history(req->type);
169 BOOST_LOG_SEV(party_type_handler_lg(), debug)
170 << "Completed " << msg.subject;
171 reply(nats_, msg, get_party_type_history_response{
172 .success = true, .history = std::move(h)});
173 } catch (const std::exception& e) {
174 BOOST_LOG_SEV(party_type_handler_lg(), error)
175 << msg.subject << " failed: " << e.what();
176 reply(nats_, msg, get_party_type_history_response{
177 .success = false, .message = e.what()});
178 }
179 }
180
181private:
184 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
185};
186
187} // namespace ores::refdata::messaging
188#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