Skip to content

Commit

Permalink
feat: Add AlertTagDelete model and update delete_alert_tag_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorwalton committed Aug 17, 2024
1 parent bc5e327 commit 7332118
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions backend/app/incidents/routes/db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
)
Expand Down
4 changes: 4 additions & 0 deletions backend/app/incidents/schema/db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions backend/app/incidents/services/db_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 7332118

Please sign in to comment.