Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
147 changes: 57 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,123 +1,90 @@
# Reto técnico iO - Backend
<!--
title: 'Serverless Framework Node Express API service backed by DynamoDB on AWS'
description: 'This template demonstrates how to develop and deploy a simple Node Express API service backed by DynamoDB running on AWS Lambda using the Serverless Framework.'
layout: Doc
framework: v4
platform: AWS
language: nodeJS
priority: 1
authorLink: 'https://github.com/serverless'
authorName: 'Serverless, Inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
-->

## Descripción:
Se requiere implementar un proyecto serverless de registro de pagos y consulta de transacciones. A continuación se muestran los diagramas correspondientes:
# Serverless Framework Node Express API on AWS

### Diagrama 1:
![Diagrama 1](images/post.png)
Este API debe de llamar a un Step Function el cual debe:
- Validar el id de usuario comparándolo en la tabla **users**
- En caso el usuario exista, un lambda llamado **execute-payments** debe llamar a un API Mock que el postulante debe crear, el cual debe de regresar una transacción exitosa
- Si la transacción es exitosa, debe de grabarse un registro en la tabla **transactions**
- Al terminar todo de forma exitosa, debe dar una respuesta satisfactoria que contenga el id de la transacción
This template demonstrates how to develop and deploy a simple Node Express API service, backed by DynamoDB table, running on AWS Lambda using the Serverless Framework.

Nota: En el momento que se interactúa con la tabla **transactions**, un stream de DynamoDB debe activar el lambda **register-activity**, el cual debe de guardar un registro del suceso en la tabla **activity**
This template configures a single function, `api`, which is responsible for handling all incoming requests using the `httpApi` event. To learn more about `httpApi` event configuration options, please refer to [httpApi event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). As the event is configured in a way to accept all incoming requests, the Express.js framework is responsible for routing and handling requests internally. This implementation uses the `serverless-http` package to transform the incoming event request payloads to payloads compatible with Express.js. To learn more about `serverless-http`, please refer to the [serverless-http README](https://github.com/dougmoscrop/serverless-http).

### Diagrama 2:
![Diagrama 2](images/get.png)
Este API debe de llamar a un Lambda Function la cual debe:
- Consultar por el id de transacción desde el lambda **get-transaction** en la tabla **transactions**
- En caso la transacción exista, debe de regresar el registro de la transacción
- En caso la transacción no exista, debe de regresar una respuesta con el mensaje "Usuario no encontrado"
Additionally, it also handles provisioning of a DynamoDB database that is used for storing data about users. The Express.js application exposes two endpoints, `POST /users` and `GET /user/:userId`, which create and retrieve a user record.

## Consideraciones:
## Usage

Obligatorio :
1. Respetar el arquetipo
2. Construir pruebas unitarias
3. Buenas Prácticas (SOLID, Clean Code, etc)
### Deployment

Deseable:
1. Crear los componentes con IaC (Terraform, Cloudformation)
2. Logs usando CloudWatch
3. Formatters / Linters
Install dependencies with:

## Anexos:
```
npm install
```

### POST /v1/payments
and then deploy with:

Payload
```json
{
"userId": "f529177d-0521-414e-acd9-6ac840549e97",
"amount": 30
}
```
serverless deploy
```

Respusta OK (201)
```json
{
"message": "Payment registered successfully",
"transactionId": "8db0a6fc-ad42-4974-ac1f-36bb90730afe"
}
After running deploy, you should see output similar to:

```
Deploying "aws-node-express-dynamodb-api" to stage "dev" (us-east-1)

Respuesta errada (400)
```json
{
"message": "Something was wrong"
}
✔ Service deployed to stack aws-node-express-dynamodb-api-dev (109s)

endpoint: ANY - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com
functions:
api: aws-node-express-dynamodb-api-dev-api (3.8 MB)
```

### GET /v1/transactions
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [`httpApi` event docs](https://www.serverless.com/framework/docs/providers/aws/events/http-api/). Additionally, in current configuration, the DynamoDB table will be removed when running `serverless remove`. To retain the DynamoDB table even after removal of the stack, add `DeletionPolicy: Retain` to its resource definition.

### Invocation

After successful deployment, you can create a new user by calling the corresponding endpoint:

Query params
```
transactionId: "8db0a6fc-ad42-4974-ac1f-36bb90730afe"
curl --request POST 'https://xxxxxx.execute-api.us-east-1.amazonaws.com/users' --header 'Content-Type: application/json' --data-raw '{"name": "John", "userId": "someUserId"}'
```

Respusta OK (200)
```json
{
"transactionId": "8db0a6fc-ad42-4974-ac1f-36bb90730afe",
"userId": "f529177d-0521-414e-acd9-6ac840549e97",
"paymentAmount": 30
}
```
Which should result in the following response:

Respuesta errada (404)
```json
{
"message": "Transaction not found"
}
{ "userId": "someUserId", "name": "John" }
```

### Tabla users:
You can later retrieve the user by `userId` by calling the following endpoint:

La tabla users debe de contener datos de los usuarios que pueden realizar una transacción.
```
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/users/someUserId
```

<dl>
<dt>Esquema:</dt>
<dd>userId (string - partition key)</dd>
<dd>name (string)</dd>
<dd>lastName (string)</dd>
</dl>
Which should result in the following response:

La tabla debe de tener el siguiente contenido:
| userId | name | lastName |
| ----------- | ----------- | ----------- |
| f529177d-0521-414e-acd9-6ac840549e97 | Pedro | Suarez |
| 15f1c60a-2833-49b7-8660-065b58be2f89 | Andrea | Vargas |
```json
{ "userId": "someUserId", "name": "John" }
```

### Tabla transactions:
### Local development

<dl>
<dt>Esquema:</dt>
<dd>transactionId (partition key)</dd>
<dd>userId</dd>
<dd>amount</dd>
</dl>
The easiest way to develop and test your function is to use the `dev` command:

### Tabla activity:
```
serverless dev
```

<dl>
<dt>Esquema:</dt>
<dd>activityId (partition key)</dd>
<dd>transactionId</dd>
<dd>date</dd>
</dl>
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.

## Send us your challenge
Cuando termines el reto, luego de forkear el repositorio, debes crear un pull request to our repository indicando en la descripción de este tu nombre y correo.
Now you can invoke the function as before, but this time the function will be executed locally. Now you can develop your function locally, invoke it, and see the results immediately without having to re-deploy.

### Tiempo de resolución: 3 días
When you are done developing, don't forget to run `serverless deploy` to deploy the function to the cloud.
Loading