11from flask import Blueprint , request , jsonify
2- from routes .checkov import logger
3- from utils .db import db
4- from models .scan_history import ScanHistory
5- from models .file_content import FileContent
2+ from services .history_service import get_scan_history
3+ from schemas .history_dto import ScanHistoryResponse , HistoryErrorResponse
4+ import logging
65
7- history_bp = Blueprint ('history' , __name__ )
6+ history_bp = Blueprint ("history" , __name__ )
7+ logger = logging .getLogger (__name__ )
88
99@history_bp .route ("/history" , methods = ["GET" ])
10- def get_scan_history ():
10+ def get_scan_history_route ():
1111 user_id = request .headers .get ("X-User-ID" )
12- if not user_id :
13- return jsonify ({"error" : "user_id is required" }), 400
14-
1512 scan_type = request .args .get ("scan_type" )
1613
1714 try :
18- # Base query
19- query = ScanHistory .query .filter_by (user_id = user_id )
20-
21- # Add scan_type filter if provided
22- if scan_type :
23- query = query .filter_by (scan_type = scan_type )
24-
25- # Order by created_at DESC
26- query = query .order_by (ScanHistory .created_at .desc ())
27-
28- history = []
29- for scan in query .all ():
30- item_name = None
31- if scan .input_type :
32- file_content = FileContent .query .filter_by (scan_id = scan .id ).first ()
33- if file_content :
34- item_name = f"{ file_content .file_path } { file_content .id } "
35- logger .debug (f"Item name for scan_id { scan .id } : { item_name } " )
36-
37- element = {
38- "id" : scan .id ,
39- "repo_id" : scan .repo_id ,
40- "scan_result" : scan .scan_result ,
41- "repo_url" : scan .repo_url ,
42- "item_name" : item_name ,
43- "status" : scan .status ,
44- "score" : scan .score ,
45- "compliant" : scan .compliant ,
46- "created_at" : scan .created_at .isoformat (),
47- "input_type" : scan .input_type ,
48- "scan_type" : scan .scan_type
49- }
50- history .append (element )
51-
52- return jsonify (history )
53- except Exception as e :
54- logger .error (f"Failed to fetch scan history for user_id { user_id } : { str (e )} " )
55- db .session .rollback ()
56- return jsonify ({"error" : "Failed to fetch scan history" }), 500
15+ history = get_scan_history (user_id , scan_type )
16+ return jsonify ([ScanHistoryResponse (** item ).dict () for item in history ]), 200
17+ except ValueError as e :
18+ logger .error (f"Invalid input for scan history: { str (e )} " )
19+ return jsonify (HistoryErrorResponse (error = str (e )).dict ()), 400
20+ except RuntimeError as e :
21+ logger .error (f"Error fetching scan history: { str (e )} " )
22+ return jsonify (HistoryErrorResponse (error = str (e )).dict ()), 500
0 commit comments