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

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Hrithikesh
2024-09-20 13:09:38 +05:30
committed by GitHub
parent a94cf25bb6
commit c8f7232a30
13 changed files with 98 additions and 31 deletions

View File

@ -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,

View File

@ -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();

View File

@ -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;

View File

@ -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