38
from app.incidents.models import AssetFieldName
39
from app.incidents.models import Case
40
from app.incidents.models import CaseAlertLink
41
+from app.incidents.models import CaseComment
42
from app.incidents.models import CaseDataStore
43
from app.incidents.models import CaseReportTemplateDataStore
44
from app.incidents.models import Comment
61
from app.incidents.schema.db_operations import CaseAlertLinksCreate
62
from app.incidents.schema.db_operations import CaseAlertUnLink
63
from app.incidents.schema.db_operations import CaseAlertUnLinkResponse
64
+from app.incidents.schema.db_operations import CaseCommentBase
65
+from app.incidents.schema.db_operations import CaseCommentCreate
66
+from app.incidents.schema.db_operations import CaseCommentEdit
67
from app.incidents.schema.db_operations import CaseCreate
68
from app.incidents.schema.db_operations import CaseOut
69
from app.incidents.schema.db_operations import CaseReportTemplateDataStoreListResponse
837
return alert
838
839
840
+async def update_alert_escalated(alert_id: int, escalated: bool, db: AsyncSession) -> Alert:
841
+ result = await db.execute(select(Alert).where(Alert.id == alert_id))
842
+ alert = result.scalars().first()
843
+ if not alert:
844
+ raise HTTPException(status_code=404, detail="Alert not found")
845
+ alert.escalated = escalated
846
+ await db.commit()
847
+ return alert
848
+
849
+
850
+async def update_case_escalated(case_id: int, escalated: bool, db: AsyncSession) -> Case:
851
+ result = await db.execute(select(Case).where(Case.id == case_id))
852
+ case = result.scalars().first()
853
+ if not case:
854
+ raise HTTPException(status_code=404, detail="Case not found")
855
+ case.escalated = escalated
856
+ await db.commit()
857
+ return case
858
+
859
+
860
async def increment_case_notification_count(case_id: int, db: AsyncSession) -> Case:
861
result = await db.execute(select(Case).where(Case.id == case_id))
862
case = result.scalars().first()
914
return comment
915
916
917
+async def create_case_comment(comment: CaseCommentCreate, db: AsyncSession) -> CaseComment:
918
+ # Check if the case exists
919
+ result = await db.execute(select(Case).options(selectinload(Case.comments)).where(Case.id == comment.case_id))
920
+ case = result.scalars().first()
921
+ if not case:
922
+ raise HTTPException(status_code=404, detail="Case not found")
923
+
924
+ # Create comment with automatic timestamp if not provided
925
+ comment_data = comment.dict()
926
+ if comment_data.get("created_at") is None:
927
+ comment_data["created_at"] = datetime.utcnow()
928
+
929
+ db_comment = CaseComment(**comment_data)
930
+ db.add(db_comment)
931
+ try:
932
+ await db.commit()
933
+ except IntegrityError:
934
+ raise HTTPException(status_code=400, detail="Comment already exists")
935
+ return db_comment
936
+
937
+
938
+async def edit_case_comment(comment: CaseCommentEdit, db: AsyncSession) -> CaseComment:
939
+ result = await db.execute(select(CaseComment).where(CaseComment.id == comment.comment_id))
940
+ db_comment = result.scalars().first()
941
+ if not db_comment:
942
+ raise HTTPException(status_code=404, detail="Comment not found")
943
+ db_comment.comment = comment.comment
944
+ db_comment.user_name = comment.user_name
945
+ await db.commit()
946
+ return db_comment
947
+
948
+
949
+async def delete_case_comment(comment_id: int, db: AsyncSession) -> CaseComment:
950
+ result = await db.execute(select(CaseComment).where(CaseComment.id == comment_id))
951
+ comment = result.scalars().first()
952
+ if not comment:
953
+ raise HTTPException(status_code=404, detail="Comment not found")
954
+ await db.execute(delete(CaseComment).where(CaseComment.id == comment_id))
955
+ await db.commit()
956
+ return comment
957
+
958
+
959
async def create_asset(asset: AssetCreate, db: AsyncSession) -> Asset:
960
# Check if the alert exists
961
result = await db.execute(select(Alert).options(selectinload(Alert.assets)).where(Alert.id == asset.alert_linked))
1119
customer_code=alert.customer_code,
1120
source=alert.source,
1121
assigned_to=alert.assigned_to,
1122
+ escalated=alert.escalated,
1123
comments=comments,
1124
assets=assets,
1125
tags=tags,
1165
customer_code=alert.customer_code,
1166
source=alert.source,
1167
assigned_to=alert.assigned_to,
1168
+ escalated=alert.escalated,
1169
comments=comments,
1170
assets=assets,
1171
tags=tags,
1200
case_description=alert.alert_description,
1201
case_status=alert.status,
1202
assigned_to=alert.assigned_to,
1203
+ escalated=alert.escalated,
1204
customer_code=alert.customer_code,
1205
)
1206
db.add(case)
1274
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1275
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.cases).selectinload(CaseAlertLink.case),
1276
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.iocs).selectinload(AlertToIoC.ioc),
1277
+ selectinload(Case.comments),
1278
),
1279
)
1280
case = result.scalars().first()
1298
customer_code=alert.customer_code,
1299
source=alert.source,
1300
assigned_to=alert.assigned_to,
1301
+ escalated=alert.escalated,
1302
comments=comments,
1303
assets=assets,
1304
tags=tags,
1306
iocs=iocs,
1307
)
1308
alerts_out.append(alert_out)
1309
+
1310
+ # Extract case comments
1311
+ case_comments = [CaseCommentBase(**comment.__dict__) for comment in case.comments]
1312
+
1313
case_out = CaseOut(
1314
id=case.id,
1315
case_name=case.case_name,
1319
case_creation_time=case.case_creation_time,
1320
customer_code=case.customer_code,
1321
notification_invoked_number=case.notification_invoked_number or 0,
1322
+ comments=case_comments,
1323
+ escalated=case.escalated,
1324
)
1325
return case_out
1326
1333
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1334
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.cases).selectinload(CaseAlertLink.case),
1335
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.iocs).selectinload(AlertToIoC.ioc),
1336
+ selectinload(Case.comments),
1337
),
1338
)
1339
cases = result.scalars().all()
1357
customer_code=alert.customer_code,
1358
source=alert.source,
1359
assigned_to=alert.assigned_to,
1360
+ escalated=alert.escalated,
1361
comments=comments,
1362
assets=assets,
1363
tags=tags,
1365
iocs=iocs,
1366
)
1367
alerts_out.append(alert_out)
1368
+
1369
+ # Extract case comments
1370
+ case_comments = [CaseCommentBase(**comment.__dict__) for comment in case.comments]
1371
+
1372
case_out = CaseOut(
1373
id=case.id,
1374
case_name=case.case_name,
1379
case_status=case.case_status,
1380
customer_code=case.customer_code,
1381
notification_invoked_number=case.notification_invoked_number or 0,
1382
+ comments=case_comments,
1383
+ escalated=case.escalated,
1384
)
1385
cases_out.append(case_out)
1386
return cases_out
1396
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1397
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.cases).selectinload(CaseAlertLink.case),
1398
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.iocs).selectinload(AlertToIoC.ioc),
1399
+ selectinload(Case.comments),
1400
),
1401
)
1402
cases = result.scalars().all()
1420
customer_code=alert.customer_code,
1421
source=alert.source,
1422
assigned_to=alert.assigned_to,
1423
+ escalated=alert.escalated,
1424
comments=comments,
1425
assets=assets,
1426
tags=tags,
1428
iocs=iocs,
1429
)
1430
alerts_out.append(alert_out)
1431
+
1432
+ # Extract case comments
1433
+ case_comments = [CaseCommentBase(**comment.__dict__) for comment in case.comments]
1434
+
1435
case_out = CaseOut(
1436
id=case.id,
1437
case_name=case.case_name,
1438
case_description=case.case_description,
1439
assigned_to=case.assigned_to,
1440
alerts=alerts_out,
1441
+ case_creation_time=case.case_creation_time,
1442
+ case_status=case.case_status,
1443
customer_code=case.customer_code,
1444
+ notification_invoked_number=case.notification_invoked_number or 0,
1445
+ comments=case_comments,
1446
+ escalated=case.escalated,
1447
)
1448
cases_out.append(case_out)
1449
return cases_out
1457
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.comments),
1458
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.assets),
1459
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1460
+ selectinload(Case.comments),
1461
),
1462
)
1463
cases = result.scalars().all()
1479
customer_code=alert.customer_code,
1480
source=alert.source,
1481
assigned_to=alert.assigned_to,
1482
+ escalated=alert.escalated,
1483
comments=comments,
1484
assets=assets,
1485
tags=tags,
1486
)
1487
alerts_out.append(alert_out)
1488
+
1489
+ # Handle case comments
1490
+ case_comments = []
1491
+ for comment in case.comments:
1492
+ case_comment = CaseCommentBase(
1493
+ id=comment.id,
1494
+ case_id=comment.case_id,
1495
+ user_name=comment.user_name,
1496
+ comment=comment.comment,
1497
+ created_at=comment.created_at,
1498
+ )
1499
+ case_comments.append(case_comment)
1500
+
1501
case_out = CaseOut(
1502
id=case.id,
1503
case_name=case.case_name,
1505
assigned_to=case.assigned_to,
1506
alerts=alerts_out,
1507
customer_code=case.customer_code,
1508
+ comments=case_comments,
1509
+ escalated=case.escalated,
1510
)
1511
cases_out.append(case_out)
1512
return cases_out
1523
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.comments),
1524
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.assets),
1525
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1526
+ selectinload(Case.comments),
1527
),
1528
)
1529
cases = result.scalars().all()
1545
customer_code=alert.customer_code,
1546
source=alert.source,
1547
assigned_to=alert.assigned_to,
1548
+ escalated=alert.escalated,
1549
comments=comments,
1550
assets=assets,
1551
tags=tags,
1552
)
1553
alerts_out.append(alert_out)
1554
+
1555
+ # Handle case comments
1556
+ case_comments = []
1557
+ for comment in case.comments:
1558
+ case_comment = CaseCommentBase(
1559
+ id=comment.id,
1560
+ case_id=comment.case_id,
1561
+ user_name=comment.user_name,
1562
+ comment=comment.comment,
1563
+ created_at=comment.created_at,
1564
+ )
1565
+ case_comments.append(case_comment)
1566
+
1567
case_out = CaseOut(
1568
id=case.id,
1569
case_name=case.case_name,
1571
assigned_to=case.assigned_to,
1572
alerts=alerts_out,
1573
customer_code=case.customer_code,
1574
+ comments=case_comments,
1575
+ escalated=case.escalated,
1576
)
1577
cases_out.append(case_out)
1578
return cases_out
1586
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.comments),
1587
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.assets),
1588
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
1589
+ selectinload(Case.comments),
1590
),
1591
)
1592
cases = result.scalars().all()
1608
customer_code=alert.customer_code,
1609
source=alert.source,
1610
assigned_to=alert.assigned_to,
1611
+ escalated=alert.escalated,
1612
comments=comments,
1613
assets=assets,
1614
tags=tags,
1615
)
1616
alerts_out.append(alert_out)
1617
+
1618
+ # Handle case comments
1619
+ case_comments = []
1620
+ for comment in case.comments:
1621
+ case_comment = CaseCommentBase(
1622
+ id=comment.id,
1623
+ case_id=comment.case_id,
1624
+ user_name=comment.user_name,
1625
+ comment=comment.comment,
1626
+ created_at=comment.created_at,
1627
+ )
1628
+ case_comments.append(case_comment)
1629
+
1630
case_out = CaseOut(
1631
id=case.id,
1632
case_name=case.case_name,
1634
assigned_to=case.assigned_to,
1635
alerts=alerts_out,
1636
customer_code=case.customer_code,
1637
+ comments=case_comments,
1638
+ escalated=case.escalated,
1639
)
1640
cases_out.append(case_out)
1641
return cases_out
1688
customer_code=alert.customer_code,
1689
source=alert.source,
1690
assigned_to=alert.assigned_to,
1691
+ escalated=alert.escalated,
1692
comments=comments,
1693
assets=assets,
1694
tags=tags,
1735
customer_code=alert.customer_code,
1736
source=alert.source,
1737
assigned_to=alert.assigned_to,
1738
+ escalated=alert.escalated,
1739
comments=comments,
1740
assets=assets,
1741
tags=tags,
1781
customer_code=alert.customer_code,
1782
source=alert.source,
1783
assigned_to=alert.assigned_to,
1784
+ escalated=alert.escalated,
1785
comments=comments,
1786
assets=assets,
1787
tags=tags,
1831
customer_code=alert.customer_code,
1832
source=alert.source,
1833
assigned_to=alert.assigned_to,
1834
+ escalated=alert.escalated,
1835
comments=comments,
1836
assets=assets,
1837
tags=tags,
1879
customer_code=alert.customer_code,
1880
source=alert.source,
1881
assigned_to=alert.assigned_to,
1882
+ escalated=alert.escalated,
1883
comments=comments,
1884
assets=assets,
1885
tags=tags,
1927
customer_code=alert.customer_code,
1928
source=alert.source,
1929
assigned_to=alert.assigned_to,
1930
+ escalated=alert.escalated,
1931
comments=comments,
1932
assets=assets,
1933
tags=tags,
1975
customer_code=alert.customer_code,
1976
source=alert.source,
1977
assigned_to=alert.assigned_to,
1978
+ escalated=alert.escalated,
1979
comments=comments,
1980
assets=assets,
1981
tags=tags,
2023
customer_code=alert.customer_code,
2024
source=alert.source,
2025
assigned_to=alert.assigned_to,
2026
+ escalated=alert.escalated,
2027
comments=comments,
2028
assets=assets,
2029
tags=tags,
2110
customer_code=alert.customer_code,
2111
source=alert.source,
2112
assigned_to=alert.assigned_to,
2113
+ escalated=alert.escalated,
2114
comments=comments,
2115
assets=assets,
2116
tags=tags,
2166
customer_code=alert.customer_code,
2167
source=alert.source,
2168
assigned_to=alert.assigned_to,
2169
+ escalated=alert.escalated,
2170
comments=comments,
2171
assets=assets,
2172
tags=tags,
2189
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.tags).selectinload(AlertToTag.tag),
2190
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.cases).selectinload(CaseAlertLink.case),
2191
selectinload(Case.alerts).selectinload(CaseAlertLink.alert).selectinload(Alert.iocs).selectinload(AlertToIoC.ioc),
2192
+ selectinload(Case.comments),
2193
)
2194
2195
# Apply customer filtering
2219
customer_code=alert.customer_code,
2220
source=alert.source,
2221
assigned_to=alert.assigned_to,
2222
+ escalated=alert.escalated,
2223
comments=comments,
2224
assets=assets,
2225
tags=tags,
2227
iocs=iocs,
2228
)
2229
alerts_out.append(alert_out)
2230
+
2231
+ # Handle case comments
2232
+ case_comments = []
2233
+ for comment in case.comments:
2234
+ case_comment = CaseCommentBase(
2235
+ id=comment.id,
2236
+ case_id=comment.case_id,
2237
+ user_name=comment.user_name,
2238
+ comment=comment.comment,
2239
+ created_at=comment.created_at,
2240
+ )
2241
+ case_comments.append(case_comment)
2242
+
2243
case_out = CaseOut(
2244
id=case.id,
2245
case_name=case.case_name,
2250
case_status=case.case_status,
2251
customer_code=case.customer_code,
2252
notification_invoked_number=case.notification_invoked_number or 0,
2253
+ comments=case_comments,
2254
+ escalated=case.escalated,
2255
)
2256
cases_out.append(case_out)
2257
return cases_out