|
| 1 | +import { |
| 2 | + APIGatewayProxyEvent, |
| 3 | + APIGatewayProxyResult, |
| 4 | + ScheduledEvent, |
| 5 | +} from "aws-lambda"; |
| 6 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 7 | +import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; |
| 8 | + |
| 9 | +const dynamoClient = new DynamoDBClient({ region: process.env.AWS_REGION }); |
| 10 | +const docClient = DynamoDBDocumentClient.from(dynamoClient); |
| 11 | + |
| 12 | +interface JailDataEvent { |
| 13 | + countyId: string; |
| 14 | + source: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface DetentionRecord { |
| 18 | + detaineeId: string; |
| 19 | + timestamp: string; |
| 20 | + status: "ACTIVE" | "INACTIVE"; |
| 21 | + createdDate: string; |
| 22 | + countyId: string; |
| 23 | + source: string; |
| 24 | + // Add other fields as needed |
| 25 | + firstName?: string; |
| 26 | + lastName?: string; |
| 27 | + bookingDate?: string; |
| 28 | + charges?: string[]; |
| 29 | + ttl?: number; |
| 30 | +} |
| 31 | + |
| 32 | +export const execute = async ( |
| 33 | + event: ScheduledEvent | APIGatewayProxyEvent |
| 34 | +): Promise<APIGatewayProxyResult | void> => { |
| 35 | + try { |
| 36 | + console.log("Data collection started", JSON.stringify(event, null, 2)); |
| 37 | + |
| 38 | + // Parse input from scheduled event or API Gateway |
| 39 | + let inputData: JailDataEvent; |
| 40 | + |
| 41 | + if ("source" in event && event.source === "aws.events") { |
| 42 | + // Scheduled event |
| 43 | + const scheduledEvent = event as ScheduledEvent; |
| 44 | + inputData = JSON.parse( |
| 45 | + scheduledEvent.detail ? JSON.stringify(scheduledEvent.detail) : "{}" |
| 46 | + ); |
| 47 | + } else { |
| 48 | + // API Gateway event (for manual testing) |
| 49 | + const apiEvent = event as APIGatewayProxyEvent; |
| 50 | + inputData = JSON.parse(apiEvent.body || "{}"); |
| 51 | + } |
| 52 | + |
| 53 | + const { countyId, source } = inputData; |
| 54 | + |
| 55 | + if (!countyId || !source) { |
| 56 | + const error = "Missing required parameters: countyId and source"; |
| 57 | + console.error(error); |
| 58 | + |
| 59 | + if ("httpMethod" in event) { |
| 60 | + return { |
| 61 | + statusCode: 400, |
| 62 | + body: JSON.stringify({ error }), |
| 63 | + }; |
| 64 | + } |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: Implement actual data collection logic |
| 69 | + // This is a stub that would be replaced with real county data scraping |
| 70 | + const mockData = await collectJailData(countyId, source); |
| 71 | + |
| 72 | + // Store the collected data |
| 73 | + const results = await Promise.all( |
| 74 | + mockData.map((record) => storeDetentionRecord(record)) |
| 75 | + ); |
| 76 | + |
| 77 | + console.log( |
| 78 | + `Successfully processed ${results.length} records for ${countyId}` |
| 79 | + ); |
| 80 | + |
| 81 | + if ("httpMethod" in event) { |
| 82 | + return { |
| 83 | + statusCode: 200, |
| 84 | + body: JSON.stringify({ |
| 85 | + message: `Successfully processed ${results.length} records`, |
| 86 | + countyId, |
| 87 | + source, |
| 88 | + }), |
| 89 | + }; |
| 90 | + } |
| 91 | + } catch (error) { |
| 92 | + console.error("Error in data collection:", error); |
| 93 | + |
| 94 | + if ("httpMethod" in event) { |
| 95 | + return { |
| 96 | + statusCode: 500, |
| 97 | + body: JSON.stringify({ error: "Internal server error" }), |
| 98 | + }; |
| 99 | + } |
| 100 | + throw error; |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +async function collectJailData( |
| 105 | + countyId: string, |
| 106 | + source: string |
| 107 | +): Promise<DetentionRecord[]> { |
| 108 | + // This is a stub - replace with actual data collection logic |
| 109 | + console.log(`Collecting data for county: ${countyId}, source: ${source}`); |
| 110 | + |
| 111 | + // Mock data for demonstration |
| 112 | + const now = new Date(); |
| 113 | + const today = now.toISOString().split("T")[0]; |
| 114 | + const timestamp = now.toISOString(); |
| 115 | + |
| 116 | + return [ |
| 117 | + { |
| 118 | + detaineeId: `${countyId}-${Date.now()}-001`, |
| 119 | + timestamp, |
| 120 | + status: "ACTIVE", |
| 121 | + createdDate: today, |
| 122 | + countyId, |
| 123 | + source, |
| 124 | + firstName: "John", |
| 125 | + lastName: "Doe", |
| 126 | + bookingDate: today, |
| 127 | + charges: ["DWI", "Traffic Violation"], |
| 128 | + ttl: Math.floor(Date.now() / 1000) + 365 * 24 * 60 * 60, // 1 year TTL |
| 129 | + }, |
| 130 | + ]; |
| 131 | +} |
| 132 | + |
| 133 | +async function storeDetentionRecord(record: DetentionRecord): Promise<void> { |
| 134 | + const params = { |
| 135 | + TableName: process.env.JAILDATA_TABLE!, |
| 136 | + Item: record, |
| 137 | + }; |
| 138 | + |
| 139 | + await docClient.send(new PutCommand(params)); |
| 140 | + console.log(`Stored record for detainee: ${record.detaineeId}`); |
| 141 | +} |
0 commit comments