fix(model): fix issue where associated model config status was not updated when deleting API Key

When deleting an API Key, check if the associated model configuration has other active keys; if not, automatically set it to inactive.
Also optimize the model configuration query method to support multi-type queries and add sorting conditions.
This commit is contained in:
wwq
2026-04-16 13:35:35 +08:00
parent 1faa258e23
commit b59e2b5bcd
2 changed files with 25 additions and 13 deletions

View File

@@ -729,10 +729,21 @@ class ModelApiKeyService:
@staticmethod
def delete_api_key(db: Session, api_key_id: uuid.UUID) -> bool:
"""删除API Key"""
if not ModelApiKeyRepository.get_by_id(db, api_key_id):
api_key = ModelApiKeyRepository.get_by_id(db, api_key_id)
if not api_key:
raise BusinessException("API Key不存在", BizCode.NOT_FOUND)
model_config_ids = [mc.id for mc in api_key.model_configs]
success = ModelApiKeyRepository.delete(db, api_key_id)
for model_config_id in model_config_ids:
model_config = ModelConfigRepository.get_by_id(db, model_config_id)
if model_config:
has_active_key = any(key.is_active for key in model_config.api_keys)
if not has_active_key and model_config.is_active:
model_config.is_active = False
db.commit()
return success