refactor: Improve endpoint name uniqueness handling in create_flow function
This commit is contained in:
parent
669237cbd9
commit
ba835dc16f
1 changed files with 17 additions and 0 deletions
|
|
@ -50,6 +50,23 @@ def create_flow(
|
||||||
flow.name = f"{flow.name} ({max(numbers) + 1})"
|
flow.name = f"{flow.name} ({max(numbers) + 1})"
|
||||||
else:
|
else:
|
||||||
flow.name = f"{flow.name} (1)"
|
flow.name = f"{flow.name} (1)"
|
||||||
|
# Now check if the endpoint is unique
|
||||||
|
if session.exec(
|
||||||
|
select(Flow).where(Flow.endpoint_name == flow.endpoint_name).where(Flow.user_id == current_user.id)
|
||||||
|
).first():
|
||||||
|
flows = session.exec(
|
||||||
|
select(Flow)
|
||||||
|
.where(Flow.endpoint_name.like(f"{flow.endpoint_name}-%"))
|
||||||
|
.where(Flow.user_id == current_user.id)
|
||||||
|
).all()
|
||||||
|
if flows:
|
||||||
|
# The endpoitn name is like "my-endpoint","my-endpoint-1", "my-endpoint-2"
|
||||||
|
# so we need to get the highest number and add 1
|
||||||
|
# we need to get the last part of the endpoint name
|
||||||
|
numbers = [int(flow.endpoint_name.split("-")[-1]) for flow in flows]
|
||||||
|
flow.endpoint_name = f"{flow.endpoint_name}-{max(numbers) + 1}"
|
||||||
|
else:
|
||||||
|
flow.endpoint_name = f"{flow.endpoint_name}-1"
|
||||||
|
|
||||||
db_flow = Flow.model_validate(flow, from_attributes=True)
|
db_flow = Flow.model_validate(flow, from_attributes=True)
|
||||||
db_flow.updated_at = datetime.now(timezone.utc)
|
db_flow.updated_at = datetime.now(timezone.utc)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue