ORE Studio 0.0.4
Loading...
Searching...
No Matches
fra_convention_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_MESSAGING_FRA_CONVENTION_HANDLER_HPP
21#define ORES_REFDATA_MESSAGING_FRA_CONVENTION_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/fra_convention_protocol.hpp"
32#include "ores.refdata.core/service/fra_convention_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& fra_convention_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.fra_convention_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
54public:
57 std::optional<ores::security::jwt::jwt_authenticator> verifier)
58 : nats_(nats), ctx_(std::move(ctx)), verifier_(std::move(verifier)) {}
59
60 void list(ores::nats::message msg) {
61 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
62 << "Handling " << msg.subject;
63 auto req_ctx_expected = ores::service::service::make_request_context(
64 ctx_, msg, verifier_);
65 if (!req_ctx_expected) {
66 error_reply(nats_, msg, req_ctx_expected.error());
67 return;
68 }
69 const auto& req_ctx = *req_ctx_expected;
71 get_fra_conventions_response resp;
72 try {
73 resp.fra_conventions = svc.list_fra_conventions();
74 resp.total_available_count =
75 static_cast<int>(resp.fra_conventions.size());
76 } catch (const std::exception& e) {
77 BOOST_LOG_SEV(fra_convention_handler_lg(), error)
78 << msg.subject << " failed: " << e.what();
79 resp.success = false;
80 resp.message = e.what();
81 }
82 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
83 << "Completed " << msg.subject;
84 reply(nats_, msg, resp);
85 }
86
87 void save(ores::nats::message msg) {
88 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
89 << "Handling " << msg.subject;
90 auto req_ctx_expected = ores::service::service::make_request_context(
91 ctx_, msg, verifier_);
92 if (!req_ctx_expected) {
93 error_reply(nats_, msg, req_ctx_expected.error());
94 return;
95 }
96 const auto& req_ctx = *req_ctx_expected;
97 if (!has_permission(req_ctx, "refdata::fra_conventions:write")) {
98 error_reply(nats_, msg, ores::service::error_code::forbidden);
99 return;
100 }
102 if (auto req = decode<save_fra_convention_request>(msg)) {
103 try {
104 svc.save_fra_convention(req->data);
105 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
106 << "Completed " << msg.subject;
107 reply(nats_, msg,
108 save_fra_convention_response{.success = true});
109 } catch (const std::exception& e) {
110 BOOST_LOG_SEV(fra_convention_handler_lg(), error)
111 << msg.subject << " failed: " << e.what();
112 reply(nats_, msg, save_fra_convention_response{
113 .success = false, .message = e.what()});
114 }
115 } else {
116 BOOST_LOG_SEV(fra_convention_handler_lg(), warn)
117 << "Failed to decode: " << msg.subject;
118 error_reply(nats_, msg, ores::service::error_code::bad_request);
119 }
120 }
121
122 void history(ores::nats::message msg) {
123 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
124 << "Handling " << msg.subject;
125 auto req_ctx_expected = ores::service::service::make_request_context(
126 ctx_, msg, verifier_);
127 if (!req_ctx_expected) {
128 error_reply(nats_, msg, req_ctx_expected.error());
129 return;
130 }
131 const auto& req_ctx = *req_ctx_expected;
133 if (auto req = decode<get_fra_convention_history_request>(msg)) {
134 try {
135 auto hist = svc.get_fra_convention_history(req->id);
136 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
137 << "Completed " << msg.subject;
138 reply(nats_, msg, get_fra_convention_history_response{
139 .fra_conventions = std::move(hist), .success = true});
140 } catch (const std::exception& e) {
141 BOOST_LOG_SEV(fra_convention_handler_lg(), error)
142 << msg.subject << " failed: " << e.what();
143 reply(nats_, msg, get_fra_convention_history_response{
144 .success = false, .message = e.what()});
145 }
146 } else {
147 BOOST_LOG_SEV(fra_convention_handler_lg(), warn)
148 << "Failed to decode: " << msg.subject;
149 error_reply(nats_, msg, ores::service::error_code::bad_request);
150 }
151 }
152
153 void remove(ores::nats::message msg) {
154 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
155 << "Handling " << msg.subject;
156 auto req_ctx_expected = ores::service::service::make_request_context(
157 ctx_, msg, verifier_);
158 if (!req_ctx_expected) {
159 error_reply(nats_, msg, req_ctx_expected.error());
160 return;
161 }
162 const auto& req_ctx = *req_ctx_expected;
163 if (!has_permission(req_ctx, "refdata::fra_conventions:write")) {
164 error_reply(nats_, msg, ores::service::error_code::forbidden);
165 return;
166 }
168 if (auto req = decode<delete_fra_convention_request>(msg)) {
169 try {
170 for (const auto& code : req->codes)
171 svc.remove_fra_convention(code);
172 BOOST_LOG_SEV(fra_convention_handler_lg(), debug)
173 << "Completed " << msg.subject;
174 reply(nats_, msg,
175 delete_fra_convention_response{.success = true});
176 } catch (const std::exception& e) {
177 BOOST_LOG_SEV(fra_convention_handler_lg(), error)
178 << msg.subject << " failed: " << e.what();
179 reply(nats_, msg, delete_fra_convention_response{
180 .success = false, .message = e.what()});
181 }
182 } else {
183 BOOST_LOG_SEV(fra_convention_handler_lg(), warn)
184 << "Failed to decode: " << msg.subject;
185 error_reply(nats_, msg, ores::service::error_code::bad_request);
186 }
187 }
188
189private:
192 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
193};
194
195} // namespace ores::refdata::messaging
196
197#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
@ bad_request
The request payload was malformed or could not be decoded.
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
NATS message handler for FRA convention operations.
Definition fra_convention_handler.hpp:53
Service for managing FRA conventions.
Definition fra_convention_service.hpp:36