ORE Studio 0.0.4
Loading...
Searching...
No Matches
rounding_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_ROUNDING_TYPE_HANDLER_HPP
21#define ORES_REFDATA_CORE_MESSAGING_ROUNDING_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/rounding_type_protocol.hpp"
32#include "ores.refdata.core/service/rounding_type_service.hpp"
33
34namespace ores::refdata::messaging {
35
36namespace {
37inline auto& rounding_type_handler_lg() {
38 static auto instance = ores::logging::make_logger(
39 "ores.refdata.messaging.rounding_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 rounding_type_handler {
51public:
52 rounding_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(rounding_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::rounding_type_service svc(ctx);
68 get_rounding_types_response resp;
69 try {
70 resp.rounding_types = svc.list_types();
71 resp.total_available_count =
72 static_cast<int>(resp.rounding_types.size());
73 BOOST_LOG_SEV(rounding_type_handler_lg(), debug)
74 << "Completed " << msg.subject;
75 } catch (const std::exception& e) {
76 BOOST_LOG_SEV(rounding_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(rounding_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::rounding_types:write")) {
93 error_reply(nats_, msg, ores::service::error_code::forbidden);
94 return;
95 }
96 service::rounding_type_service svc(ctx);
97 auto req = decode<save_rounding_type_request>(msg);
98 if (!req) {
99 BOOST_LOG_SEV(rounding_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(rounding_type_handler_lg(), debug)
106 << "Completed " << msg.subject;
107 reply(nats_, msg,
108 save_rounding_type_response{.success = true});
109 } catch (const std::exception& e) {
110 BOOST_LOG_SEV(rounding_type_handler_lg(), error)
111 << msg.subject << " failed: " << e.what();
112 reply(nats_, msg, save_rounding_type_response{
113 .success = false, .message = e.what()});
114 }
115 }
116
117 void del(ores::nats::message msg) {
118 BOOST_LOG_SEV(rounding_type_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::rounding_types:delete")) {
128 error_reply(nats_, msg, ores::service::error_code::forbidden);
129 return;
130 }
131 service::rounding_type_service svc(ctx);
132 auto req = decode<delete_rounding_type_request>(msg);
133 if (!req) {
134 BOOST_LOG_SEV(rounding_type_handler_lg(), warn)
135 << "Failed to decode: " << msg.subject;
136 return;
137 }
138 try {
139 svc.remove_type(req->type);
140 BOOST_LOG_SEV(rounding_type_handler_lg(), debug)
141 << "Completed " << msg.subject;
142 reply(nats_, msg,
143 delete_rounding_type_response{.success = true});
144 } catch (const std::exception& e) {
145 BOOST_LOG_SEV(rounding_type_handler_lg(), error)
146 << msg.subject << " failed: " << e.what();
147 reply(nats_, msg, delete_rounding_type_response{
148 .success = false, .message = e.what()});
149 }
150 }
151
152 void history(ores::nats::message msg) {
153 BOOST_LOG_SEV(rounding_type_handler_lg(), debug)
154 << "Handling " << msg.subject;
155 auto ctx_expected = ores::service::service::make_request_context(
156 ctx_, msg, verifier_);
157 if (!ctx_expected) {
158 error_reply(nats_, msg, ctx_expected.error());
159 return;
160 }
161 const auto& ctx = *ctx_expected;
162 service::rounding_type_service svc(ctx);
163 auto req = decode<get_rounding_type_history_request>(msg);
164 if (!req) {
165 BOOST_LOG_SEV(rounding_type_handler_lg(), warn)
166 << "Failed to decode: " << msg.subject;
167 return;
168 }
169 try {
170 auto h = svc.get_type_history(req->type);
171 BOOST_LOG_SEV(rounding_type_handler_lg(), debug)
172 << "Completed " << msg.subject;
173 reply(nats_, msg, get_rounding_type_history_response{
174 .success = true, .history = std::move(h)});
175 } catch (const std::exception& e) {
176 BOOST_LOG_SEV(rounding_type_handler_lg(), error)
177 << msg.subject << " failed: " << e.what();
178 reply(nats_, msg, get_rounding_type_history_response{
179 .success = false, .message = e.what()});
180 }
181 }
182
183private:
186 std::optional<ores::security::jwt::jwt_authenticator> verifier_;
187};
188
189} // namespace ores::refdata::messaging
190#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