@@ -23,19 +23,22 @@ def get_tenant_info(tenant_id: str) -> Dict[str, Any]:
2323 """
2424 Get tenant information by tenant ID
2525
26+ If TENANT_NAME config is missing, automatically create one with default name.
27+
2628 Args:
2729 tenant_id (str): Tenant ID
2830
2931 Returns:
3032 Dict[str, Any]: Tenant information
31-
32- Raises:
33- NotFoundException: When tenant not found
3433 """
3534 # Get tenant name
3635 name_config = get_single_config_info (tenant_id , TENANT_NAME )
3736 if not name_config :
38- logging .warning (f"The name of tenant { tenant_id } not found." )
37+ logger .warning (f"The name of tenant { tenant_id } not found, creating default config." )
38+ # Auto-create TENANT_NAME config with default name
39+ _ensure_tenant_name_config (tenant_id )
40+ # Re-fetch after creation
41+ name_config = get_single_config_info (tenant_id , TENANT_NAME )
3942
4043 group_config = get_single_config_info (tenant_id , DEFAULT_GROUP_ID )
4144
@@ -48,6 +51,38 @@ def get_tenant_info(tenant_id: str) -> Dict[str, Any]:
4851 return tenant_info
4952
5053
54+ def _ensure_tenant_name_config (tenant_id : str ) -> bool :
55+ """
56+ Ensure TENANT_NAME config exists for the tenant.
57+ Creates a default name config if it doesn't exist.
58+
59+ Args:
60+ tenant_id: Tenant ID
61+
62+ Returns:
63+ bool: True if config exists or was created successfully, False otherwise
64+ """
65+ # Check if already exists (double-check in case of race condition)
66+ existing = get_single_config_info (tenant_id , TENANT_NAME )
67+ if existing :
68+ return True
69+
70+ # Create default TENANT_NAME config
71+ tenant_name_data = {
72+ "tenant_id" : tenant_id ,
73+ "config_key" : TENANT_NAME ,
74+ "config_value" : "Unnamed Tenant" ,
75+ "created_by" : "system_auto_create" ,
76+ "updated_by" : "system_auto_create"
77+ }
78+ success = insert_config (tenant_name_data )
79+ if success :
80+ logger .info (f"Auto-created TENANT_NAME config for tenant { tenant_id } " )
81+ else :
82+ logger .error (f"Failed to auto-create TENANT_NAME config for tenant { tenant_id } " )
83+ return success
84+
85+
5186def get_all_tenants () -> List [Dict [str , Any ]]:
5287 """
5388 Get all tenants
@@ -163,6 +198,8 @@ def update_tenant_info(tenant_id: str, tenant_name: str, updated_by: Optional[st
163198 """
164199 Update tenant information
165200
201+ If TENANT_NAME config doesn't exist, creates it with the provided name.
202+
166203 Args:
167204 tenant_id (str): Tenant ID
168205 tenant_name (str): New tenant name
@@ -172,25 +209,35 @@ def update_tenant_info(tenant_id: str, tenant_name: str, updated_by: Optional[st
172209 Dict[str, Any]: Updated tenant information
173210
174211 Raises:
175- NotFoundException: When tenant not found
176- ValidationError: When tenant name is invalid
212+ ValidationError: When tenant name is invalid or update fails
177213 """
178- # Check if tenant exists and get current name config
179- name_config = get_single_config_info (tenant_id , TENANT_NAME )
180- if not name_config :
181- raise NotFoundException (f"Tenant { tenant_id } not found" )
182-
183214 # Validate tenant name
184215 if not tenant_name or not tenant_name .strip ():
185216 raise ValidationError ("Tenant name cannot be empty" )
186217
187- # Update tenant name
188- success = update_config_by_tenant_config_id (
189- name_config ["tenant_config_id" ],
190- tenant_name .strip ()
191- )
192- if not success :
193- raise ValidationError ("Failed to update tenant name" )
218+ # Check if tenant name config exists
219+ name_config = get_single_config_info (tenant_id , TENANT_NAME )
220+ if not name_config :
221+ # Tenant config doesn't exist, create it with the provided name
222+ logger .info (f"TENANT_NAME config not found for { tenant_id } , creating new config." )
223+ tenant_name_data = {
224+ "tenant_id" : tenant_id ,
225+ "config_key" : TENANT_NAME ,
226+ "config_value" : tenant_name .strip (),
227+ "created_by" : updated_by ,
228+ "updated_by" : updated_by
229+ }
230+ success = insert_config (tenant_name_data )
231+ if not success :
232+ raise ValidationError ("Failed to create tenant name configuration" )
233+ else :
234+ # Update existing config
235+ success = update_config_by_tenant_config_id (
236+ name_config ["tenant_config_id" ],
237+ tenant_name .strip ()
238+ )
239+ if not success :
240+ raise ValidationError ("Failed to update tenant name" )
194241
195242 # Return updated tenant information
196243 updated_tenant = get_tenant_info (tenant_id )
0 commit comments