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

fix: do not allow duplicate organization name (#5919)

Hrithikeshc8f7232a3001
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.json
index 6ff3e0176e..926af8c162 100644
--- a/api-reference-v2/openapi_spec.json
+++ b/api-reference-v2/openapi_spec.json
@@ -32,7 +32,7 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/OrganizationRequest"
+ "$ref": "#/components/schemas/OrganizationCreateRequest"
},
"examples": {
"Create an organization with organization_name": {
@@ -129,7 +129,7 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/OrganizationRequest"
+ "$ref": "#/components/schemas/OrganizationUpdateRequest"
},
"examples": {
"Update organization_name of the organization": {
@@ -10116,12 +10116,14 @@
"confirm"
]
},
- "OrganizationRequest": {
+ "OrganizationCreateRequest": {
"type": "object",
+ "required": [
+ "organization_name"
+ ],
"properties": {
"organization_name": {
- "type": "string",
- "nullable": true
+ "type": "string"
},
"organization_details": {
"type": "object",
@@ -10131,7 +10133,8 @@
"type": "object",
"nullable": true
}
- }
+ },
+ "additionalProperties": false
},
"OrganizationResponse": {
"type": "object",
@@ -10169,6 +10172,24 @@
}
}
},
+ "OrganizationUpdateRequest": {
+ "type": "object",
+ "properties": {
+ "organization_name": {
+ "type": "string",
+ "nullable": true
+ },
+ "organization_details": {
+ "type": "object",
+ "nullable": true
+ },
+ "metadata": {
+ "type": "object",
+ "nullable": true
+ }
+ },
+ "additionalProperties": false
+ },
"OutgoingWebhook": {
"type": "object",
"required": [
diff --git a/api-reference/openapi_spec.json b/api-reference/openapi_spec.json
index 4934b4fb34..2635cdb42f 100644
--- a/api-reference/openapi_spec.json
+++ b/api-reference/openapi_spec.json
@@ -1121,7 +1121,7 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/OrganizationRequest"
+ "$ref": "#/components/schemas/OrganizationCreateRequest"
},
"examples": {
"Create an organization with organization_name": {
@@ -1218,7 +1218,7 @@
"content": {
"application/json": {
"schema": {
- "$ref": "#/components/schemas/OrganizationRequest"
+ "$ref": "#/components/schemas/OrganizationUpdateRequest"
},
"examples": {
"Update organization_name of the organization": {
@@ -13913,12 +13913,14 @@
}
}
},
- "OrganizationRequest": {
+ "OrganizationCreateRequest": {
"type": "object",
+ "required": [
+ "organization_name"
+ ],
"properties": {
"organization_name": {
- "type": "string",
- "nullable": true
+ "type": "string"
},
"organization_details": {
"type": "object",
@@ -13928,7 +13930,8 @@
"type": "object",
"nullable": true
}
- }
+ },
+ "additionalProperties": false
},
"OrganizationResponse": {
"type": "object",
@@ -13966,6 +13969,24 @@
}
}
},
+ "OrganizationUpdateRequest": {
+ "type": "object",
+ "properties": {
+ "organization_name": {
+ "type": "string",
+ "nullable": true
+ },
+ "organization_details": {
+ "type": "object",
+ "nullable": true
+ },
+ "metadata": {
+ "type": "object",
+ "nullable": true
+ }
+ },
+ "additionalProperties": false
+ },
"OutgoingWebhook": {
"type": "object",
"required": [
diff --git a/crates/api_models/src/events.rs b/crates/api_models/src/events.rs
index b10f8e32db..29fe0bed22 100644
--- a/crates/api_models/src/events.rs
+++ b/crates/api_models/src/events.rs
@@ -32,7 +32,9 @@ use crate::{
disputes::*,
files::*,
mandates::*,
- organization::{OrganizationId, OrganizationRequest, OrganizationResponse},
+ organization::{
+ OrganizationCreateRequest, OrganizationId, OrganizationResponse, OrganizationUpdateRequest,
+ },
payment_methods::*,
payments::*,
user::{UserKeyTransferRequest, UserTransferKeyResponse},
@@ -129,7 +131,8 @@ impl_api_event_type!(
DisputeFiltersResponse,
GetDisputeMetricRequest,
OrganizationResponse,
- OrganizationRequest,
+ OrganizationCreateRequest,
+ OrganizationUpdateRequest,
OrganizationId,
CustomerListRequest
)
diff --git a/crates/api_models/src/organization.rs b/crates/api_models/src/organization.rs
index 1e2203eea3..f95a159511 100644
--- a/crates/api_models/src/organization.rs
+++ b/crates/api_models/src/organization.rs
@@ -20,7 +20,18 @@ pub struct OrganizationId {
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
-pub struct OrganizationRequest {
+#[serde(deny_unknown_fields)]
+pub struct OrganizationCreateRequest {
+ pub organization_name: String,
+ #[schema(value_type = Option<Object>)]
+ pub organization_details: Option<pii::SecretSerdeValue>,
+ #[schema(value_type = Option<Object>)]
+ pub metadata: Option<pii::SecretSerdeValue>,
+}
+
+#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
+#[serde(deny_unknown_fields)]
+pub struct OrganizationUpdateRequest {
pub organization_name: Option<String>,
#[schema(value_type = Option<Object>)]
pub organization_details: Option<pii::SecretSerdeValue>,
diff --git a/crates/openapi/src/openapi.rs b/crates/openapi/src/openapi.rs
index 0c9010421b..f4cceec8be 100644
--- a/crates/openapi/src/openapi.rs
+++ b/crates/openapi/src/openapi.rs
@@ -202,7 +202,8 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::refunds::RefundResponse,
api_models::refunds::RefundStatus,
api_models::refunds::RefundUpdateRequest,
- api_models::organization::OrganizationRequest,
+ api_models::organization::OrganizationCreateRequest,
+ api_models::organization::OrganizationUpdateRequest,
api_models::organization::OrganizationResponse,
api_models::admin::MerchantAccountCreate,
api_models::admin::MerchantAccountUpdate,
diff --git a/crates/openapi/src/openapi_v2.rs b/crates/openapi/src/openapi_v2.rs
index ca2974d011..da0a95aa6d 100644
--- a/crates/openapi/src/openapi_v2.rs
+++ b/crates/openapi/src/openapi_v2.rs
@@ -127,7 +127,8 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::refunds::RefundResponse,
api_models::refunds::RefundStatus,
api_models::refunds::RefundUpdateRequest,
- api_models::organization::OrganizationRequest,
+ api_models::organization::OrganizationCreateRequest,
+ api_models::organization::OrganizationUpdateRequest,
api_models::organization::OrganizationResponse,
api_models::admin::MerchantAccountCreate,
api_models::admin::MerchantAccountUpdate,
diff --git a/crates/openapi/src/routes/organization.rs b/crates/openapi/src/routes/organization.rs
index 44d5f94357..ad15c0e679 100644
--- a/crates/openapi/src/routes/organization.rs
+++ b/crates/openapi/src/routes/organization.rs
@@ -6,7 +6,7 @@
post,
path = "/organization",
request_body(
- content = OrganizationRequest,
+ content = OrganizationCreateRequest,
examples(
(
"Create an organization with organization_name" = (
@@ -51,7 +51,7 @@ pub async fn organization_retrieve() {}
put,
path = "/organization/{organization_id}",
request_body(
- content = OrganizationRequest,
+ content = OrganizationUpdateRequest,
examples(
(
"Update organization_name of the organization" = (
@@ -79,7 +79,7 @@ pub async fn organization_update() {}
post,
path = "/v2/organization",
request_body(
- content = OrganizationRequest,
+ content = OrganizationCreateRequest,
examples(
(
"Create an organization with organization_name" = (
@@ -124,7 +124,7 @@ pub async fn organization_retrieve() {}
put,
path = "/v2/organization/{organization_id}",
request_body(
- content = OrganizationRequest,
+ content = OrganizationUpdateRequest,
examples(
(
"Update organization_name of the organization" = (
diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs
index 4000add34e..65bdeb04a4 100644
--- a/crates/router/src/core/admin.rs
+++ b/crates/router/src/core/admin.rs
@@ -114,14 +114,16 @@ fn add_publishable_key_to_decision_service(
#[cfg(feature = "olap")]
pub async fn create_organization(
state: SessionState,
- req: api::OrganizationRequest,
+ req: api::OrganizationCreateRequest,
) -> RouterResponse<api::OrganizationResponse> {
let db_organization = ForeignFrom::foreign_from(req);
state
.store
.insert_organization(db_organization)
.await
- .to_duplicate_response(errors::ApiErrorResponse::InternalServerError)
+ .to_duplicate_response(errors::ApiErrorResponse::GenericDuplicateError {
+ message: "Organization with the given organization_name already exists".to_string(),
+ })
.attach_printable("Error when creating organization")
.map(ForeignFrom::foreign_from)
.map(service_api::ApplicationResponse::Json)
@@ -131,7 +133,7 @@ pub async fn create_organization(
pub async fn update_organization(
state: SessionState,
org_id: api::OrganizationId,
- req: api::OrganizationRequest,
+ req: api::OrganizationUpdateRequest,
) -> RouterResponse<api::OrganizationResponse> {
let organization_update = diesel_models::organization::OrganizationUpdate::Update {
organization_name: req.organization_name,
diff --git a/crates/router/src/routes/admin.rs b/crates/router/src/routes/admin.rs
index 14711bef6a..e5a18fba53 100644
--- a/crates/router/src/routes/admin.rs
+++ b/crates/router/src/routes/admin.rs
@@ -14,7 +14,7 @@ use crate::{
pub async fn organization_create(
state: web::Data<AppState>,
req: HttpRequest,
- json_payload: web::Json<admin::OrganizationRequest>,
+ json_payload: web::Json<admin::OrganizationCreateRequest>,
) -> HttpResponse {
let flow = Flow::OrganizationCreate;
Box::pin(api::server_wrap(
@@ -35,7 +35,7 @@ pub async fn organization_update(
state: web::Data<AppState>,
req: HttpRequest,
org_id: web::Path<common_utils::id_type::OrganizationId>,
- json_payload: web::Json<admin::OrganizationRequest>,
+ json_payload: web::Json<admin::OrganizationUpdateRequest>,
) -> HttpResponse {
let flow = Flow::OrganizationUpdate;
let organization_id = org_id.into_inner();
diff --git a/crates/router/src/types/api/admin.rs b/crates/router/src/types/api/admin.rs
index f50e5376db..cb75fe6a9b 100644
--- a/crates/router/src/types/api/admin.rs
+++ b/crates/router/src/types/api/admin.rs
@@ -11,7 +11,9 @@ pub use api_models::{
ProfileCreate, ProfileResponse, ProfileUpdate, ToggleAllKVRequest, ToggleAllKVResponse,
ToggleKVRequest, ToggleKVResponse, WebhookDetails,
},
- organization::{OrganizationId, OrganizationRequest, OrganizationResponse},
+ organization::{
+ OrganizationCreateRequest, OrganizationId, OrganizationResponse, OrganizationUpdateRequest,
+ },
};
use common_utils::ext_traits::ValueExt;
use diesel_models::organization::OrganizationBridge;
diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs
index 4ecda43f7e..e9a70cda9c 100644
--- a/crates/router/src/types/transformers.rs
+++ b/crates/router/src/types/transformers.rs
@@ -1561,17 +1561,17 @@ impl ForeignFrom<api_models::organization::OrganizationNew>
}
}
-impl ForeignFrom<api_models::organization::OrganizationRequest>
+impl ForeignFrom<api_models::organization::OrganizationCreateRequest>
for diesel_models::organization::OrganizationNew
{
- fn foreign_from(item: api_models::organization::OrganizationRequest) -> Self {
+ fn foreign_from(item: api_models::organization::OrganizationCreateRequest) -> Self {
let org_new = api_models::organization::OrganizationNew::new(None);
- let api_models::organization::OrganizationRequest {
+ let api_models::organization::OrganizationCreateRequest {
organization_name,
organization_details,
metadata,
} = item;
- let mut org_new_db = Self::new(org_new.org_id, organization_name);
+ let mut org_new_db = Self::new(org_new.org_id, Some(organization_name));
org_new_db.organization_details = organization_details;
org_new_db.metadata = metadata;
org_new_db
diff --git a/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/down.sql b/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/down.sql
index 99af8b6ded..6acb08597f 100644
--- a/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/down.sql
+++ b/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/down.sql
@@ -8,6 +8,8 @@ ALTER TABLE ORGANIZATION DROP CONSTRAINT organization_pkey_id;
ALTER TABLE ORGANIZATION
ADD CONSTRAINT organization_pkey PRIMARY KEY (org_id);
+ALTER TABLE organization DROP CONSTRAINT organization_organization_name_key;
+
-- back fill
UPDATE ORGANIZATION
SET org_name = organization_name
diff --git a/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/up.sql b/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/up.sql
index cde27046bc..440391529a 100644
--- a/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/up.sql
+++ b/v2_migrations/2024-08-28-081838_update_v2_primary_key_constraints/up.sql
@@ -16,6 +16,9 @@ ALTER TABLE ORGANIZATION DROP CONSTRAINT organization_pkey;
ALTER TABLE ORGANIZATION
ADD CONSTRAINT organization_pkey_id PRIMARY KEY (id);
+ALTER TABLE ORGANIZATION
+ADD CONSTRAINT organization_organization_name_key UNIQUE (organization_name);
+
------------------------ Merchant Account -----------------------
-- The new primary key for v2 merchant account will be `id`
ALTER TABLE merchant_account DROP CONSTRAINT merchant_account_pkey;