File size: 5,358 Bytes
bbfde3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fs = require('fs').promises;
const path = require('path');
const { applyCorsHeaders, handleCorsPreflight } = require('../lib/cors-middleware');

module.exports = async (req, res) => {
  if (handleCorsPreflight(req, res, { allowedMethods: 'GET, DELETE, OPTIONS' })) {
    return;
  }
  applyCorsHeaders(req, res, { allowedMethods: 'GET, DELETE, OPTIONS' });

  const { action, reportId, batchId, limit = 50 } = req.query;

  try {
    switch (req.method) {
      case 'GET':
        if (action === 'list') {
          await listReports(req, res, { limit: parseInt(limit) });
        } else if (action === 'batches') {
          await listBatches(req, res);
        } else if (reportId) {
          await getReport(req, res, reportId);
        } else if (batchId) {
          await getBatch(req, res, batchId);
        } else {
          res.status(400).json({ error: 'Missing action or ID parameter' });
        }
        break;
        
      case 'DELETE':
        if (reportId) {
          await deleteReport(req, res, reportId);
        } else if (batchId) {
          await deleteBatch(req, res, batchId);
        } else {
          res.status(400).json({ error: 'Missing reportId or batchId parameter' });
        }
        break;
        
      default:
        res.status(405).json({ error: 'Method not allowed' });
    }
  } catch (error) {
    console.error('Reports API error:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
};

async function listReports(req, res, options = {}) {
  const reportsDir = 'reports';
  const files = await fs.readdir(reportsDir);
  
  // Filter for individual reports (not batch summaries)
  const reportFiles = files
    .filter(f => f.endsWith('-accessibility-report.json'))
    .sort((a, b) => {
      // Sort by timestamp (newest first)
      const aTime = parseInt(a.split('-')[0]);
      const bTime = parseInt(b.split('-')[0]);
      return bTime - aTime;
    })
    .slice(0, options.limit);

  const reports = [];
  
  for (const file of reportFiles) {
    try {
      const filePath = path.join(reportsDir, file);
      const content = await fs.readFile(filePath, 'utf8');
      const report = JSON.parse(content);
      
      reports.push({
        reportId: report.reportId,
        filename: report.filename,
        timestamp: report.timestamp,
        summary: report.summary,
        filePath: file
      });
    } catch (error) {
      console.warn(`Failed to read report ${file}:`, error.message);
    }
  }

  res.json({
    totalReports: reports.length,
    reports: reports
  });
}

async function listBatches(req, res) {
  const reportsDir = 'reports';
  const files = await fs.readdir(reportsDir);
  
  // Filter for batch summaries
  const batchFiles = files
    .filter(f => f.startsWith('batch-') && f.endsWith('-summary.json'))
    .sort((a, b) => {
      // Sort by timestamp (newest first)
      const aTime = parseInt(a.split('-')[1]);
      const bTime = parseInt(b.split('-')[1]);
      return bTime - aTime;
    });

  const batches = [];
  
  for (const file of batchFiles) {
    try {
      const filePath = path.join(reportsDir, file);
      const content = await fs.readFile(filePath, 'utf8');
      const batch = JSON.parse(content);
      
      batches.push({
        batchId: batch.batchId,
        timestamp: batch.timestamp,
        totalFiles: batch.totalFiles,
        successful: batch.results.filter(r => r.success).length,
        failed: batch.results.filter(r => !r.success).length,
        filePath: file
      });
    } catch (error) {
      console.warn(`Failed to read batch ${file}:`, error.message);
    }
  }

  res.json({
    totalBatches: batches.length,
    batches: batches
  });
}

async function getReport(req, res, reportId) {
  const reportPath = `reports/${reportId}-accessibility-report.json`;
  
  try {
    const content = await fs.readFile(reportPath, 'utf8');
    const report = JSON.parse(content);
    res.json(report);
  } catch (error) {
    res.status(404).json({ error: `Report ${reportId} not found` });
  }
}

async function getBatch(req, res, batchId) {
  const batchPath = `reports/batch-${batchId}-summary.json`;
  
  try {
    const content = await fs.readFile(batchPath, 'utf8');
    const batch = JSON.parse(content);
    res.json(batch);
  } catch (error) {
    res.status(404).json({ error: `Batch ${batchId} not found` });
  }
}

async function deleteReport(req, res, reportId) {
  const reportPath = `reports/${reportId}-accessibility-report.json`;
  
  try {
    await fs.unlink(reportPath);
    res.json({ message: `Report ${reportId} deleted successfully` });
  } catch (error) {
    res.status(404).json({ error: `Report ${reportId} not found` });
  }
}

async function deleteBatch(req, res, batchId) {
  const batchPath = `reports/batch-${batchId}-summary.json`;
  
  try {
    await fs.unlink(batchPath);
    
    // Also delete individual reports from this batch if they exist
    // This is optional - you might want to keep individual reports
    
    res.json({ message: `Batch ${batchId} deleted successfully` });
  } catch (error) {
    res.status(404).json({ error: `Batch ${batchId} not found` });
  }
}