Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(config): add API route `set_config` (#1144)

Nishant Joshif31926b83355
diff --git a/crates/router/src/core/configs.rs b/crates/router/src/core/configs.rs
index b71c31b104..5f0e7d76c0 100644
--- a/crates/router/src/core/configs.rs
+++ b/crates/router/src/core/configs.rs
@@ -1,3 +1,5 @@
+use error_stack::ResultExt;
+
use crate::{
core::errors::{self, utils::StorageErrorExt, RouterResponse},
db::StorageInterface,
@@ -5,6 +7,22 @@ use crate::{
types::{api, transformers::ForeignInto},
};
+pub async fn set_config(
+ store: &dyn StorageInterface,
+ config: api::Config,
+) -> RouterResponse<api::Config> {
+ let config = store
+ .insert_config(storage_models::configs::ConfigNew {
+ key: config.key,
+ config: config.value,
+ })
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Unknown error, while setting config key")?;
+
+ Ok(ApplicationResponse::Json(config.foreign_into()))
+}
+
pub async fn read_config(store: &dyn StorageInterface, key: &str) -> RouterResponse<api::Config> {
let config = store
.find_config_by_key_cached(key)
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 06dc810b5d..74478c73ac 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -389,6 +389,7 @@ impl Configs {
pub fn server(config: AppState) -> Scope {
web::scope("/configs")
.app_data(web::Data::new(config))
+ .service(web::resource("/").route(web::post().to(config_key_create)))
.service(
web::resource("/{key}")
.route(web::get().to(config_key_retrieve))
diff --git a/crates/router/src/routes/configs.rs b/crates/router/src/routes/configs.rs
index 96c1c5a7db..16c15db663 100644
--- a/crates/router/src/routes/configs.rs
+++ b/crates/router/src/routes/configs.rs
@@ -8,6 +8,26 @@ use crate::{
types::api as api_types,
};
+#[instrument(skip_all, fields(flow = ?Flow::CreateConfigKey))]
+pub async fn config_key_create(
+ state: web::Data<AppState>,
+ req: HttpRequest,
+ json_payload: web::Json<api_types::Config>,
+) -> impl Responder {
+ let flow = Flow::CreateConfigKey;
+ let payload = json_payload.into_inner();
+
+ api::server_wrap(
+ flow,
+ state.get_ref(),
+ &req,
+ payload,
+ |state, _, data| configs::set_config(&*state.store, data),
+ &auth::AdminApiAuth,
+ )
+ .await
+}
+
#[instrument(skip_all, fields(flow = ?Flow::ConfigKeyFetch))]
pub async fn config_key_retrieve(
state: web::Data<AppState>,
diff --git a/crates/router/src/types/api/configs.rs b/crates/router/src/types/api/configs.rs
index 00db2cf4bf..9e39d5569b 100644
--- a/crates/router/src/types/api/configs.rs
+++ b/crates/router/src/types/api/configs.rs
@@ -1,4 +1,4 @@
-#[derive(Clone, serde::Serialize, Debug)]
+#[derive(Clone, serde::Serialize, Debug, serde::Deserialize)]
pub struct Config {
pub key: String,
pub value: String,
diff --git a/crates/router_env/src/logger/types.rs b/crates/router_env/src/logger/types.rs
index 96dfb07171..56c91f4ae9 100644
--- a/crates/router_env/src/logger/types.rs
+++ b/crates/router_env/src/logger/types.rs
@@ -176,6 +176,8 @@ pub enum Flow {
RetrieveFile,
/// Dispute Evidence submission flow
DisputesEvidenceSubmit,
+ /// Create Config Key flow
+ CreateConfigKey,
/// Attach Dispute Evidence flow
AttachDisputeEvidence,
}