diff --git a/backend/app/incidents/routes/db_operations.py b/backend/app/incidents/routes/db_operations.py index dabc2a1f..17ae4e4c 100644 --- a/backend/app/incidents/routes/db_operations.py +++ b/backend/app/incidents/routes/db_operations.py @@ -56,7 +56,7 @@ from app.incidents.schema.db_operations import ConfiguredSourcesResponse from app.incidents.schema.db_operations import FieldAndAssetNames from app.incidents.schema.db_operations import FieldAndAssetNamesResponse -from app.incidents.schema.db_operations import MappingsResponse +from app.incidents.schema.db_operations import MappingsResponse, AlertTagDelete from app.incidents.schema.db_operations import UpdateAlertStatus from app.incidents.schema.db_operations import UpdateCaseStatus from app.incidents.services.db_operations import add_alert_title_name @@ -325,9 +325,9 @@ async def list_alerts_by_tag_endpoint(tag: str, db: AsyncSession = Depends(get_d @incidents_db_operations_router.delete("/alert/tag", response_model=AlertTagResponse) -async def delete_alert_tag_endpoint(alert_tag: AlertTagCreate, db: AsyncSession = Depends(get_db)): +async def delete_alert_tag_endpoint(alert_tag: AlertTagDelete, db: AsyncSession = Depends(get_db)): return AlertTagResponse( - alert_tag=await delete_alert_tag(alert_tag.alert_id, alert_tag.tag, db), + alert_tag=await delete_alert_tag(alert_tag.alert_id, alert_tag.tag_id, db), success=True, message="Alert tag deleted successfully", ) diff --git a/backend/app/incidents/schema/db_operations.py b/backend/app/incidents/schema/db_operations.py index d1533517..f9693801 100644 --- a/backend/app/incidents/schema/db_operations.py +++ b/backend/app/incidents/schema/db_operations.py @@ -203,6 +203,10 @@ class AlertTagCreate(BaseModel): alert_id: int tag: str +class AlertTagDelete(BaseModel): + alert_id: int + tag_id: int + class CommentBase(BaseModel): user_name: str diff --git a/backend/app/incidents/services/db_operations.py b/backend/app/incidents/services/db_operations.py index 559cf609..3c4e6381 100644 --- a/backend/app/incidents/services/db_operations.py +++ b/backend/app/incidents/services/db_operations.py @@ -365,22 +365,21 @@ async def create_alert_tag(alert_tag: AlertTagCreate, db: AsyncSession) -> Alert raise HTTPException(status_code=400, detail="Alert tag already exists") return db_alert_tag - -async def delete_alert_tag(alert_id: int, tag: str, db: AsyncSession): - result = await db.execute(select(AlertTag).where(AlertTag.tag == tag)) +async def delete_alert_tag(alert_id: int, tag_id: int, db: AsyncSession): + result = await db.execute(select(AlertTag).where(AlertTag.id == tag_id)) alert_tag = result.scalars().first() if not alert_tag: raise HTTPException(status_code=404, detail="Alert tag not found") - result = await db.execute(select(AlertToTag).where((AlertToTag.alert_id == alert_id) & (AlertToTag.tag_id == alert_tag.id))) + result = await db.execute(select(AlertToTag).where((AlertToTag.alert_id == alert_id) & (AlertToTag.tag_id == tag_id))) alert_to_tag = result.scalars().first() if not alert_to_tag: raise HTTPException(status_code=404, detail="Alert to tag link not found") - await db.execute(delete(AlertToTag).where((AlertToTag.alert_id == alert_id) & (AlertToTag.tag_id == alert_tag.id))) + await db.execute(delete(AlertToTag).where((AlertToTag.alert_id == alert_id) & (AlertToTag.tag_id == tag_id))) # Delete the tag from the AlertTag table - await db.execute(delete(AlertTag).where(AlertTag.id == alert_tag.id)) + await db.execute(delete(AlertTag).where(AlertTag.id == tag_id)) try: await db.commit() @@ -391,6 +390,7 @@ async def delete_alert_tag(alert_id: int, tag: str, db: AsyncSession): return alert_tag + async def create_alert_context(alert_context: AlertContextCreate, db: AsyncSession) -> AlertContext: db_alert_context = AlertContext(**alert_context.dict()) db.add(db_alert_context)