This repo provides a function which emails a transaction report based on a CSV transaction file.
The function is written in TypeScript and designed to run on AWS Lambda. It uses the Severless Framework to aid development and deployment, and Postmark to send emails. Jest is used to ensure quality via an automated test suite.
The function accepts an payload with the following format:
{
"filename": "/tmp/txns.csv",
"emailTo": "[email protected]"
}
Where filename
is the full file path (for local) or S3 key (for AWS) and emailTo
is the email address where the email will be received.
The app's business domain logic is in the /src/domain
folder. This represents pure business logic and can be tested by mocking the external dependencies found in the /src/utils
folder. /src/utils
contains services which are not core business logic, e.g. the connection to S3.
The app uses CloudFormation via the Serverless Framework to deploy the required services (S3, Lambda, CloudWatch) and configure the IAMs.
Prequesites:
- Node v14
To set-up the app locally:
- Clone this repo
- Run
npm i
to install dependencies - Create a
.env.json
file with the following content:
{
"postmark_api_key": "..."
}
Then to invoke the function:
npm run invoke -- --data '{"filename":"/tmp/txns.csv", "emailTo":"[email protected]"}'
Replace filename
with the full path of the test file in your local environment and emailTo
with the email which should receive the report. The folder tests/data
contains test CSV files.
To run type checking: npx tsc
.
To run the test suite: npm t
.
Prequesites:
- Follow the Local Development instructions to successfully run the app locally
- Setup an AWS account
- Install the AWS CLI locally and authenticate it to your AWS account
Then run npm run deploy
to deploy the app to AWS. It uses Infrastructure as Code via the Serverless Framework, and will deploy all necessary resources for you.
To invoke the function deployed to AWS (filename
is the object's key in the S3 bucket):
aws lambda invoke /dev/null \
--function-name aws-node-project-prod-processTransactions \
--cli-binary-format raw-in-base64-out \
--payload '{"filename":"txns.csv", "emailTo":"[email protected]"}'
Look in CloudWatch to view info logs and error messages if the function invocation fails.
The following assumptions where made when building this app:
- $0 is a credit
- All amounts are in USD
- All transactions in a given file are for the same year (year is not included in the date)
- Dates are in US format: MM/DD
- All valid rows in CSV files have an id, date and amount in this order
Given more time the app could be improved by:
- More extensive unit tests
- Higher level tests, e.g. contract or full e2e
- CI/CD pipeline to run tests and deploy to AWS
- Smoke tests against a deployed environment to ensure the app works as expected once deployed (include in the pipeline)
- Allow for processing of large files that may not fit in available memory
- Use an IoC container to inject dependencies (e.g. for local vs deployed file storage service)
- Use interfaces to completely separate the domain from the other utils (as in Hexagonal Architecture)