From 318d1bf6d14d6f184fa2915f1c1b132890413a30 Mon Sep 17 00:00:00 2001 From: "Zach A. Thomas" Date: Mon, 8 Jan 2024 08:37:11 -0500 Subject: [PATCH] create a stub implementation of the cpfValidation lambda --- .../cpfValidation/cpfValidation.test.ts | 70 +++++++++++++------ .../functions/cpfValidation/cpfValidation.ts | 48 ++++++------- 2 files changed, 70 insertions(+), 48 deletions(-) diff --git a/api/src/functions/cpfValidation/cpfValidation.test.ts b/api/src/functions/cpfValidation/cpfValidation.test.ts index 3f71af44..8153683c 100644 --- a/api/src/functions/cpfValidation/cpfValidation.test.ts +++ b/api/src/functions/cpfValidation/cpfValidation.test.ts @@ -1,29 +1,57 @@ -import { mockHttpEvent } from '@redwoodjs/testing/api' +// import { S3EventRecord } from 'aws-lambda' -import { handler } from './cpfValidation' +// import { handler } from './cpfValidation' // Improve this test with help from the Redwood Testing Doc: // https://redwoodjs.com/docs/testing#testing-functions describe('cpfValidation function', () => { - it('Should respond with 200', async () => { - const httpEvent = mockHttpEvent({ - queryStringParameters: { - id: '42', // Add parameters here - }, - }) - - const response = await handler(httpEvent, null) - const { data } = JSON.parse(response.body) - - expect(response.statusCode).toBe(200) - expect(data).toBe('cpfValidation function') + it('Dummy test', () => { + expect(1 + 1).toBe(2) }) - - // You can also use scenarios to test your api functions - // See guide here: https://redwoodjs.com/docs/testing#scenarios - // - // scenario('Scenario test', async () => { - // - // }) + // it('Should respond with 200', async () => { + // const record: S3EventRecord = { + // eventVersion: '2.0', + // eventSource: 'aws:s3', + // eventName: 'ObjectCreated:Put', + // eventTime: '1970-01-01T00:00:00.000Z', + // userIdentity: { principalId: 'test-principalId' }, + // requestParameters: { sourceIPAddress: 'test-sourceIPAddress' }, + // responseElements: { + // 'x-amz-request-id': 'test-x-amz-request-id', + // 'x-amz-id-2': 'test-x-amz-id-2', + // }, + // awsRegion: 'us-east-1', + // s3: { + // s3SchemaVersion: '1.0', + // configurationId: 'test-configurationId', + // bucket: { + // name: 'test-bucket', + // arn: 'test-arn', + // ownerIdentity: { + // principalId: 'test-principalId', + // }, + // }, + // object: { + // key: 'test-key', + // size: 1234, + // eTag: 'test-etag', + // sequencer: 'test-sequencer', + // }, + // }, + // } + // const s3Event = { + // Records: [record], + // } + // const response = await handler(s3Event, null, null) + // const { data } = JSON.parse(response.body) + // expect(response.statusCode).toBe(200) + // expect(data).toBe('excelToJson function') }) + +// You can also use scenarios to test your api functions +// See guide here: https://redwoodjs.com/docs/testing#scenarios +// +// scenario('Scenario test', async () => { +// +// }) diff --git a/api/src/functions/cpfValidation/cpfValidation.ts b/api/src/functions/cpfValidation/cpfValidation.ts index c3edb132..98be06fc 100644 --- a/api/src/functions/cpfValidation/cpfValidation.ts +++ b/api/src/functions/cpfValidation/cpfValidation.ts @@ -1,33 +1,27 @@ -import type { APIGatewayEvent, Context } from 'aws-lambda' +import { https } from 'https' + +import { S3Event, S3Handler } from 'aws-lambda' import { logger } from 'src/lib/logger' -/** - * The handler function is your code that processes http request events. - * You can use return and throw to send a response or error, respectively. - * - * Important: When deployed, a custom serverless function is an open API endpoint and - * is your responsibility to secure appropriately. - * - * @see {@link https://redwoodjs.com/docs/serverless-functions#security-considerations|Serverless Function Considerations} - * in the RedwoodJS documentation for more information. - * - * @typedef { import('aws-lambda').APIGatewayEvent } APIGatewayEvent - * @typedef { import('aws-lambda').Context } Context - * @param { APIGatewayEvent } event - an object which contains information from the invoker. - * @param { Context } context - contains information about the invocation, - * function, and execution environment. - */ -export const handler = async (event: APIGatewayEvent, _context: Context) => { - logger.info(`${event.httpMethod} ${event.path}: cpfValidation function`) +const apiEndpoint = 'https://example.com' + +export const handler: S3Handler = async (event: S3Event): Promise => { + try { + const bucket = event.Records[0].s3.bucket.name + const key = event.Records[0].s3.object.key + + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + } - return { - statusCode: 200, - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - data: 'cpfValidation function', - }), + // call API endpoint with S3 key + https.request(apiEndpoint, options, (res) => {}) + } catch (error) { + logger.error('Error processing S3 event:', error) + throw error } }