import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { validateUserData, processUser, generateUserStats } from './lib/userProcessor';
import { createResponse, handleError } from './lib/utils';
export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
try {
console.log('User processor called:', {
method: event.httpMethod,
path: event.path
});
if (event.httpMethod === 'GET') {
return createResponse(200, {
message: 'User Data Processor API',
endpoints: {
process: 'POST /process - Process user data',
stats: 'GET /stats?users=number - Generate user statistics'
},
example: {
url: 'POST YOUR_FUNCTION_URL',
body: {
name: 'John Doe',
email: 'john@example.com',
age: 28,
interests: ['technology', 'sports'],
location: 'New York'
}
}
});
}
if (event.httpMethod === 'POST') {
const userData = JSON.parse(event.body || '{}');
// Validate user data
const validation = validateUserData(userData);
if (!validation.isValid) {
return createResponse(400, {
error: 'Invalid user data',
details: validation.errors
});
}
// Process and transform user data
const processedUser = processUser(userData);
return createResponse(200, {
original: userData,
processed: processedUser,
transformation: {
added: ['id', 'slug', 'category', 'score', 'recommendations'],
computed: ['ageGroup', 'riskLevel', 'primaryInterest'],
formatted: ['email', 'name', 'location']
}
});
}
// Handle stats endpoint
if (event.path?.includes('stats')) {
const userCount = parseInt(event.queryStringParameters?.users || '10');
const stats = generateUserStats(userCount);
return createResponse(200, {
stats,
generated: userCount,
timestamp: new Date().toISOString()
});
}
return createResponse(405, { error: 'Method not allowed' });
} catch (error) {
return handleError(error);
}
};