Skip to content

Instantly share code, notes, and snippets.

@leegilmorecode
Created November 6, 2022 16:30
Show Gist options
  • Save leegilmorecode/e8cc2323ede59af2bb2b9805b6ca73ab to your computer and use it in GitHub Desktop.
Save leegilmorecode/e8cc2323ede59af2bb2b9805b6ca73ab to your computer and use it in GitHub Desktop.
Example of our primary adapter for Lambda
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { CreateCustomerAccountProps } from '@models/types';
import { ValidationError } from '@errors/validation-error';
import { createCustomerAccountUseCase } from '@use-cases/create-customer-account';
import { errorHandler } from '@packages/apigw-error-handler';
import { schema } from './create-customer-account.schema';
import { schemaValidator } from '@packages/schema-validator';
import { v4 as uuid } from 'uuid';
// adapts a proxy event (infra) into a dto for the use case
// (adapter) --> use case --> domain
export const createCustomerAccountAdapter = async ({
body,
}: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
try {
const correlationId = uuid();
const method = 'create-customer-account.handler';
const prefix = `${correlationId} - ${method}`;
if (!body) throw new ValidationError('no order body');
const customerAccount: CreateCustomerAccountProps = JSON.parse(body);
schemaValidator(schema, customerAccount);
console.log(
`${prefix} - customer account: ${JSON.stringify(customerAccount)}`
);
const createdAccount: CreateCustomerAccountProps =
await createCustomerAccountUseCase(customerAccount);
console.log(
`${prefix} - customer account created: ${JSON.stringify(createdAccount)}`
);
return {
statusCode: 201,
body: JSON.stringify(createdAccount),
};
} catch (error) {
return errorHandler(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment