20#ifndef ORES_IAM_MESSAGING_ACCOUNT_PARTY_HANDLER_HPP
21#define ORES_IAM_MESSAGING_ACCOUNT_PARTY_HANDLER_HPP
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"
35namespace ores::iam::messaging {
39inline auto& account_party_handler_lg() {
40 static auto instance = ores::logging::make_logger(
41 "ores.iam.messaging.account_party_handler");
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;
53class account_party_handler {
58 : nats_(nats), ctx_(
std::move(ctx)), signer_(
std::move(signer)) {}
62 BOOST_LOG_SEV(account_party_handler_lg(), debug)
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)
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{});
83 BOOST_LOG_SEV(account_party_handler_lg(), debug)
85 auto req = decode<get_account_parties_by_account_request>(msg);
87 BOOST_LOG_SEV(account_party_handler_lg(), warn)
88 <<
"Failed to decode: " << msg.
subject;
92 service::account_party_service svc(ctx_);
93 boost::uuids::string_generator sg;
94 auto aps = svc.list_account_parties_by_account(
96 get_account_parties_by_account_response resp;
97 resp.account_parties = std::move(aps);
98 BOOST_LOG_SEV(account_party_handler_lg(), debug)
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();
105 get_account_parties_by_account_response{});
111 BOOST_LOG_SEV(account_party_handler_lg(), debug)
113 auto req = decode<save_account_party_request>(msg);
115 BOOST_LOG_SEV(account_party_handler_lg(), warn)
116 <<
"Failed to decode: " << msg.
subject;
120 auto ctx_expected = ores::service::service::make_request_context(
121 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
123 error_reply(nats_, msg, ctx_expected.error());
126 const auto& ctx = *ctx_expected;
127 if (!has_permission(ctx,
"iam::accounts:update")) {
131 service::account_party_service svc(ctx);
132 for (
auto ap : req->account_parties) {
134 svc.save_account_party(ap);
136 BOOST_LOG_SEV(account_party_handler_lg(), debug)
137 <<
"Completed " << msg.
subject;
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()});
150 BOOST_LOG_SEV(account_party_handler_lg(), debug)
152 auto req = decode<delete_account_party_request>(msg);
154 BOOST_LOG_SEV(account_party_handler_lg(), warn)
155 <<
"Failed to decode: " << msg.
subject;
158 auto ctx_expected = ores::service::service::make_request_context(
159 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
161 error_reply(nats_, msg, ctx_expected.error());
164 const auto& ctx = *ctx_expected;
165 if (!has_permission(ctx,
"iam::accounts:update")) {
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;
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()});
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