|
| 1 | +import { event, graphqlQuery } from "#src/utils"; |
| 2 | + |
| 3 | +const BATCH_SIZE = 1000; |
| 4 | + |
| 5 | +const csvEscape = (val: any) => { |
| 6 | + const str = val === null || val === undefined ? "" : String(val); |
| 7 | + return `"${str.replace(/"/g, '""')}"`; |
| 8 | +}; |
| 9 | + |
| 10 | +const extractHref = (content: string): string => { |
| 11 | + const match = content?.match(/href=["']([^"']+)["']/i); |
| 12 | + return match ? match[1] : ""; |
| 13 | +}; |
| 14 | + |
| 15 | +export const exportAuditTablePdfSourceLinks = async () => { |
| 16 | + const auditId = (event.queryStringParameters as any).id; |
| 17 | + |
| 18 | + const scanQuery = { |
| 19 | + query: `query ($audit_id: uuid!) { |
| 20 | + audits_by_pk(id: $audit_id) { |
| 21 | + name |
| 22 | + scans(order_by: {created_at: desc}, limit: 1) { |
| 23 | + id |
| 24 | + } |
| 25 | + } |
| 26 | +}`, |
| 27 | + variables: { audit_id: auditId }, |
| 28 | + }; |
| 29 | + const scanResp = await graphqlQuery(scanQuery); |
| 30 | + const auditName = scanResp.audits_by_pk?.name; |
| 31 | + const latestScanId = scanResp.audits_by_pk?.scans?.[0]?.id; |
| 32 | + |
| 33 | + const datePart = new Date().toISOString().split("T")[0]; |
| 34 | + const safeName = auditName ? auditName.replace(/[^a-z0-9-_]/gi, "_") + "-" : ""; |
| 35 | + const filename = `pdf-links-${safeName}${auditId}-${datePart}.csv`; |
| 36 | + |
| 37 | + const emptyResponse = { |
| 38 | + statusCode: 200, |
| 39 | + headers: { |
| 40 | + "content-type": "text/csv; charset=utf-8", |
| 41 | + "content-disposition": `attachment; filename="${filename}"`, |
| 42 | + }, |
| 43 | + body: "Source URL,PDF Link\n", |
| 44 | + }; |
| 45 | + |
| 46 | + if (!latestScanId) return emptyResponse; |
| 47 | + |
| 48 | + const scopedWhere = { |
| 49 | + _and: [ |
| 50 | + { scan_id: { _eq: latestScanId } }, |
| 51 | + { url: { type: { _eq: "html" } } }, |
| 52 | + { blocker_messages: { message: { category: { _eq: "pdf-link" } } } }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + |
| 56 | + const rows: string[] = []; |
| 57 | + let offset = 0; |
| 58 | + while (true) { |
| 59 | + const batchQuery = { |
| 60 | + query: `query ($limit: Int!, $offset: Int!, $where: blockers_bool_exp!) { |
| 61 | + blockers(where: $where, limit: $limit, offset: $offset) { |
| 62 | + content |
| 63 | + url { url } |
| 64 | + } |
| 65 | +}`, |
| 66 | + variables: { limit: BATCH_SIZE, offset, where: scopedWhere }, |
| 67 | + }; |
| 68 | + const batchResp = await graphqlQuery(batchQuery); |
| 69 | + const batch: any[] = batchResp.blockers || []; |
| 70 | + |
| 71 | + for (const blocker of batch) { |
| 72 | + const sourceUrl = blocker.url?.url || ""; |
| 73 | + const pdfLink = extractHref(blocker.content); |
| 74 | + rows.push([csvEscape(sourceUrl), csvEscape(pdfLink)].join(",")); |
| 75 | + } |
| 76 | + |
| 77 | + if (batch.length < BATCH_SIZE) break; |
| 78 | + offset += BATCH_SIZE; |
| 79 | + } |
| 80 | + |
| 81 | + const csv = ["Source URL,PDF Link", ...rows].join("\n"); |
| 82 | + |
| 83 | + return { |
| 84 | + statusCode: 200, |
| 85 | + headers: { |
| 86 | + "content-type": "text/csv; charset=utf-8", |
| 87 | + "content-disposition": `attachment; filename="${filename}"`, |
| 88 | + }, |
| 89 | + body: csv, |
| 90 | + }; |
| 91 | +}; |
0 commit comments