ORE Studio 0.0.4
Loading...
Searching...
No Matches
portfolio_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_PORTFOLIO_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_PORTFOLIO_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/portfolio_protocol.hpp"
33#include "ores.refdata.core/service/portfolio_service.hpp"
34
35namespace ores::refdata::messaging {
36
37namespace {
38inline auto& portfolio_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.refdata.messaging.portfolio_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 portfolio_handler {
52public:
53 portfolio_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(portfolio_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::portfolio_service svc(ctx);
69 get_portfolios_response resp;
70 try {
71 resp.portfolios = svc.list_portfolios();
72 resp.total_available_count =
73 static_cast<int>(resp.portfolios.size());
74 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
75 << "Completed " << msg.subject;
76 } catch (const std::exception& e) {
77 BOOST_LOG_SEV(portfolio_handler_lg(), error)
78 << msg.subject << " failed: " << e.what();
79 }
80 reply(nats_, msg, resp);
81 }
82
83 void save(ores::nats::message msg) {
84 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
85 << "Handling " << msg.subject;
86 auto ctx_expected = ores::service::service::make_request_context(
87 ctx_, msg, verifier_);
88 if (!ctx_expected) {
89 error_reply(nats_, msg, ctx_expected.error());
90 return;
91 }
92 const auto& ctx = *ctx_expected;
93 if (!has_permission(ctx, "refdata::portfolios:write")) {
94 error_reply(nats_, msg, ores::service::error_code::forbidden);
95 return;
96 }
97 service::portfolio_service svc(ctx);
98 auto req = decode<save_portfolio_request>(msg);
99 if (!req) {
100 BOOST_LOG_SEV(portfolio_handler_lg(), warn)
101 << "Failed to decode: " << msg.subject;
102 return;
103 }
104 try {
105 svc.save_portfolio(req->data);
106 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
107 << "Completed " << msg.subject;
108 reply(nats_, msg, save_portfolio_response{.success = true});
109 } catch (const std::exception& e) {
110 BOOST_LOG_SEV(portfolio_handler_lg(), error)
111 << msg.subject << " failed: " << e.what();
112 reply(nats_, msg, save_portfolio_response{
113 .success = false, .message = e.what()});
114 }
115 }
116
117 void del(ores::nats::message msg) {
118 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
119 << "Handling " << msg.subject;
120 auto ctx_expected = ores::service::service::make_request_context(
121 ctx_, msg, verifier_);
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, "refdata::portfolios:delete")) {
128 error_reply(nats_, msg, ores::service::error_code::forbidden);
129 return;
130 }
131 service::portfolio_service svc(ctx);
132 auto req = decode<delete_portfolio_request>(msg);
133 if (!req) {
134 BOOST_LOG_SEV(portfolio_handler_lg(), warn)
135 << "Failed to decode: " << msg.subject;
136 return;
137 }
138 try {
139 boost::uuids::string_generator gen;
140 for (const auto& id_str : req->ids)
141 svc.remove_portfolio(gen(id_str));
142 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
143 << "Completed " << msg.subject;
144 reply(nats_, msg, delete_portfolio_response{.success = true});
145 } catch (const std::exception& e) {
146 BOOST_LOG_SEV(portfolio_handler_lg(), error)
147 << msg.subject << " failed: " << e.what();
148 reply(nats_, msg, delete_portfolio_response{
149 .success = false, .message = e.what()});
150 }
151 }
152
153 void history(ores::nats::message msg) {
154 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
155 << "Handling " << msg.subject;
156 auto ctx_expected = ores::service::service::make_request_context(
157 ctx_, msg, verifier_);
158 if (!ctx_expected) {
159 error_reply(nats_, msg, ctx_expected.error());
160 return;
161 }
162 const auto& ctx = *ctx_expected;
163 service::portfolio_service svc(ctx);
164 auto req = decode<get_portfolio_history_request>(msg);
165 if (!req) {
166 BOOST_LOG_SEV(portfolio_handler_lg(), warn)
167 << "Failed to decode: " << msg.subject;
168 return;
169 }
170 try {
171 boost::uuids::string_generator gen;
172 auto h = svc.get_portfolio_history(gen(req->id));
173 BOOST_LOG_SEV(portfolio_handler_lg(), debug)
174 << "Completed " << msg.subject;
175 reply(nats_, msg, get_portfolio_history_response{
176 .success = true, .history = std::move(h)});
177 } catch (const std::exception& e) {
178 BOOST_LOG_SEV(portfolio_handler_lg(), error)
179 << msg.subject << " failed: " << e.what();
180 reply(nats_, msg, get_portfolio_history_response{
181 .success = false, .message = e.what()});
182 }
183 }
184
185private:
188 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
189};
190
191} // namespace ores::refdata::messaging
192#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