refactor: remove db.commit() from audit_service.log_action, all callers use UoW
This commit is contained in:
@@ -9,6 +9,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app.dependencies.auth import get_current_user, require_role
|
||||
from app.domain.unit_of_work import UnitOfWork
|
||||
from app.models.jira_link import JiraLinkEntityType
|
||||
from app.models.user import User
|
||||
from app.schemas.jira_schema import (
|
||||
@@ -40,29 +41,30 @@ def create_link(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Associate an Aegis entity with a Jira issue."""
|
||||
link = jira_service.create_link(
|
||||
db,
|
||||
entity_type=body.entity_type,
|
||||
entity_id=body.entity_id,
|
||||
jira_issue_key=body.jira_issue_key,
|
||||
sync_direction=body.sync_direction,
|
||||
created_by=user.id,
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
link = jira_service.create_link(
|
||||
db,
|
||||
entity_type=body.entity_type,
|
||||
entity_id=body.entity_id,
|
||||
jira_issue_key=body.jira_issue_key,
|
||||
sync_direction=body.sync_direction,
|
||||
created_by=user.id,
|
||||
)
|
||||
audit_service.log_action(
|
||||
db,
|
||||
user_id=user.id,
|
||||
action="jira_link_created",
|
||||
entity_type="jira_link",
|
||||
entity_id=str(link.id),
|
||||
details={
|
||||
"linked_entity_type": body.entity_type.value,
|
||||
"linked_entity_id": str(body.entity_id),
|
||||
"jira_issue_key": body.jira_issue_key,
|
||||
},
|
||||
)
|
||||
uow.commit()
|
||||
db.refresh(link)
|
||||
|
||||
audit_service.log_action(
|
||||
db,
|
||||
user_id=user.id,
|
||||
action="jira_link_created",
|
||||
entity_type="jira_link",
|
||||
entity_id=str(link.id),
|
||||
details={
|
||||
"linked_entity_type": body.entity_type.value,
|
||||
"linked_entity_id": str(body.entity_id),
|
||||
"jira_issue_key": body.jira_issue_key,
|
||||
},
|
||||
)
|
||||
return link
|
||||
|
||||
|
||||
@@ -88,9 +90,10 @@ def sync_link(
|
||||
user: User = Depends(require_role("admin")),
|
||||
):
|
||||
"""Force bidirectional sync for a specific Jira link."""
|
||||
link = jira_service.get_link_or_raise(db, link_id)
|
||||
jira_service.sync_jira_to_aegis(db, link)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
link = jira_service.get_link_or_raise(db, link_id)
|
||||
jira_service.sync_jira_to_aegis(db, link)
|
||||
uow.commit()
|
||||
return {"message": "Sync completed", "jira_status": link.jira_status}
|
||||
|
||||
|
||||
@@ -101,16 +104,17 @@ def delete_link(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Remove a Jira link."""
|
||||
link = jira_service.delete_link(db, link_id)
|
||||
db.commit()
|
||||
audit_service.log_action(
|
||||
db,
|
||||
user_id=user.id,
|
||||
action="jira_link_deleted",
|
||||
entity_type="jira_link",
|
||||
entity_id=str(link_id),
|
||||
details={"jira_issue_key": link.jira_issue_key},
|
||||
)
|
||||
with UnitOfWork(db) as uow:
|
||||
link = jira_service.delete_link(db, link_id)
|
||||
audit_service.log_action(
|
||||
db,
|
||||
user_id=user.id,
|
||||
action="jira_link_deleted",
|
||||
entity_type="jira_link",
|
||||
entity_id=str(link_id),
|
||||
details={"jira_issue_key": link.jira_issue_key},
|
||||
)
|
||||
uow.commit()
|
||||
|
||||
|
||||
@router.post("/create-issue")
|
||||
@@ -121,11 +125,12 @@ def create_issue_from_entity(
|
||||
user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Auto-create a Jira issue from an Aegis entity and link them."""
|
||||
result = jira_service.create_issue_and_link(
|
||||
db,
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
created_by=user.id,
|
||||
)
|
||||
db.commit()
|
||||
with UnitOfWork(db) as uow:
|
||||
result = jira_service.create_issue_and_link(
|
||||
db,
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
created_by=user.id,
|
||||
)
|
||||
uow.commit()
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user