ORE Studio 0.0.4
Loading...
Searching...
No Matches
account_party_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_IAM_MESSAGING_ACCOUNT_PARTY_HANDLER_HPP
21#define ORES_IAM_MESSAGING_ACCOUNT_PARTY_HANDLER_HPP
22
23#include <stdexcept>
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.iam.api/messaging/account_party_protocol.hpp"
33#include "ores.iam.core/service/account_party_service.hpp"
34
35namespace ores::iam::messaging {
36
37namespace {
38
39inline auto& account_party_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.iam.messaging.account_party_handler");
42 return instance;
43}
44
45} // namespace
46
47using ores::service::messaging::reply;
48using ores::service::messaging::decode;
49using ores::service::messaging::stamp;
50using ores::service::messaging::error_reply;
51using ores::service::messaging::has_permission;
52
53class account_party_handler {
54public:
55 account_party_handler(ores::nats::service::client& nats,
58 : nats_(nats), ctx_(std::move(ctx)), signer_(std::move(signer)) {}
59
60 void list(ores::nats::message msg) {
61 using namespace ores::logging;
62 BOOST_LOG_SEV(account_party_handler_lg(), debug)
63 << "Handling " << msg.subject;
64 try {
65 service::account_party_service svc(ctx_);
66 auto aps = svc.list_account_parties();
67 get_account_parties_response resp;
68 resp.total_available_count =
69 static_cast<int>(aps.size());
70 resp.account_parties = std::move(aps);
71 BOOST_LOG_SEV(account_party_handler_lg(), debug)
72 << "Completed " << msg.subject;
73 reply(nats_, msg, resp);
74 } catch (const std::exception& e) {
75 BOOST_LOG_SEV(account_party_handler_lg(), error)
76 << msg.subject << " failed: " << e.what();
77 reply(nats_, msg, get_account_parties_response{});
78 }
79 }
80
81 void by_account(ores::nats::message msg) {
82 using namespace ores::logging;
83 BOOST_LOG_SEV(account_party_handler_lg(), debug)
84 << "Handling " << msg.subject;
85 auto req = decode<get_account_parties_by_account_request>(msg);
86 if (!req) {
87 BOOST_LOG_SEV(account_party_handler_lg(), warn)
88 << "Failed to decode: " << msg.subject;
89 return;
90 }
91 try {
92 service::account_party_service svc(ctx_);
93 boost::uuids::string_generator sg;
94 auto aps = svc.list_account_parties_by_account(
95 sg(req->account_id));
96 get_account_parties_by_account_response resp;
97 resp.account_parties = std::move(aps);
98 BOOST_LOG_SEV(account_party_handler_lg(), debug)
99 << "Completed " << msg.subject;
100 reply(nats_, msg, resp);
101 } catch (const std::exception& e) {
102 BOOST_LOG_SEV(account_party_handler_lg(), error)
103 << msg.subject << " failed: " << e.what();
104 reply(nats_, msg,
105 get_account_parties_by_account_response{});
106 }
107 }
108
109 void save(ores::nats::message msg) {
110 using namespace ores::logging;
111 BOOST_LOG_SEV(account_party_handler_lg(), debug)
112 << "Handling " << msg.subject;
113 auto req = decode<save_account_party_request>(msg);
114 if (!req) {
115 BOOST_LOG_SEV(account_party_handler_lg(), warn)
116 << "Failed to decode: " << msg.subject;
117 return;
118 }
119 try {
120 auto ctx_expected = ores::service::service::make_request_context(
121 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
122 if (!ctx_expected) {
123 error_reply(nats_, msg, ctx_expected.error());
124 return;
125 }
126 const auto& ctx = *ctx_expected;
127 if (!has_permission(ctx, "iam::accounts:update")) {
128 error_reply(nats_, msg, ores::service::error_code::forbidden);
129 return;
130 }
131 service::account_party_service svc(ctx);
132 for (auto ap : req->account_parties) {
133 stamp(ap, ctx);
134 svc.save_account_party(ap);
135 }
136 BOOST_LOG_SEV(account_party_handler_lg(), debug)
137 << "Completed " << msg.subject;
138 reply(nats_, msg,
139 save_account_party_response{.success = true});
140 } catch (const std::exception& e) {
141 BOOST_LOG_SEV(account_party_handler_lg(), error)
142 << msg.subject << " failed: " << e.what();
143 reply(nats_, msg, save_account_party_response{
144 .success = false, .message = e.what()});
145 }
146 }
147
148 void del(ores::nats::message msg) {
149 using namespace ores::logging;
150 BOOST_LOG_SEV(account_party_handler_lg(), debug)
151 << "Handling " << msg.subject;
152 auto req = decode<delete_account_party_request>(msg);
153 if (!req) {
154 BOOST_LOG_SEV(account_party_handler_lg(), warn)
155 << "Failed to decode: " << msg.subject;
156 return;
157 }
158 auto ctx_expected = ores::service::service::make_request_context(
159 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
160 if (!ctx_expected) {
161 error_reply(nats_, msg, ctx_expected.error());
162 return;
163 }
164 const auto& ctx = *ctx_expected;
165 if (!has_permission(ctx, "iam::accounts:update")) {
166 error_reply(nats_, msg, ores::service::error_code::forbidden);
167 return;
168 }
169 try {
170 service::account_party_service svc(ctx);
171 boost::uuids::string_generator sg;
172 for (const auto& key : req->keys)
173 svc.remove_account_party(
174 sg(key.account_id), sg(key.party_id));
175 BOOST_LOG_SEV(account_party_handler_lg(), debug)
176 << "Completed " << msg.subject;
177 reply(nats_, msg,
178 delete_account_party_response{.success = true});
179 } catch (const std::exception& e) {
180 BOOST_LOG_SEV(account_party_handler_lg(), error)
181 << msg.subject << " failed: " << e.what();
182 reply(nats_, msg, delete_account_party_response{
183 .success = false, .message = e.what()});
184 }
185 }
186
187private:
191};
192
193} // namespace ores::iam::messaging
194#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
JWT authentication primitive.
Definition jwt_authenticator.hpp:45