|
1 | | -# Yape Code Challenge :rocket: |
| 1 | +# app-nodejs-codechallenge |
2 | 2 |
|
3 | | -Our code challenge will let you marvel us with your Jedi coding skills :smile:. |
| 3 | +Este repositorio es un microservicio en NestJS para manejar transacciones financieras. Usa PostgreSQL para persistencia y Kafka para la validación asíncrona (anti-fraud). |
4 | 4 |
|
5 | | -Don't forget that the proper way to submit your work is to fork the repo and create a PR :wink: ... have fun !! |
| 5 | +Este README describe cómo levantar el entorno, cómo usar los servicios (endpoints) y cómo comprobar el flujo de mensajes con Kafka. |
6 | 6 |
|
7 | | -- [Problem](#problem) |
8 | | -- [Tech Stack](#tech_stack) |
9 | | -- [Send us your challenge](#send_us_your_challenge) |
| 7 | +## Resumen técnico |
| 8 | +- Framework: NestJS |
| 9 | +- Persistencia: PostgreSQL (TypeORM) |
| 10 | +- Mensajería: Kafka |
| 11 | +- Arquitectura: monolito modular con flujo event-driven para validación anti-fraud |
10 | 12 |
|
11 | | -# Problem |
| 13 | +## Requisitos |
| 14 | +- Docker & Docker Compose |
| 15 | +- Node 18+ y npm o yarn |
12 | 16 |
|
13 | | -Every time a financial transaction is created it must be validated by our anti-fraud microservice and then the same service sends a message back to update the transaction status. |
14 | | -For now, we have only three transaction statuses: |
| 17 | +## Configuración (.env) |
| 18 | +Crear un archivo `.env` en la raíz con las variables de Postgres: |
15 | 19 |
|
16 | | -<ol> |
17 | | - <li>pending</li> |
18 | | - <li>approved</li> |
19 | | - <li>rejected</li> |
20 | | -</ol> |
| 20 | +``` |
| 21 | +POSTGRES_HOST=localhost |
| 22 | +POSTGRES_PORT=5432 |
| 23 | +POSTGRES_USER=postgres |
| 24 | +POSTGRES_PASSWORD=postgres |
| 25 | +POSTGRES_DB=postgres |
| 26 | +``` |
| 27 | + |
| 28 | +Nota: la app por defecto usa `localhost:9092` como broker Kafka. Si ejecutas la app dentro de Docker, usa `kafka:29092` como broker. |
| 29 | + |
| 30 | +## Levantar el entorno |
21 | 31 |
|
22 | | -Every transaction with a value greater than 1000 should be rejected. |
| 32 | +1. Instala dependencias: |
23 | 33 |
|
24 | | -```mermaid |
25 | | - flowchart LR |
26 | | - Transaction -- Save Transaction with pending Status --> transactionDatabase[(Database)] |
27 | | - Transaction --Send transaction Created event--> Anti-Fraud |
28 | | - Anti-Fraud -- Send transaction Status Approved event--> Transaction |
29 | | - Anti-Fraud -- Send transaction Status Rejected event--> Transaction |
30 | | - Transaction -- Update transaction Status event--> transactionDatabase[(Database)] |
| 34 | +```bash |
| 35 | +npm install |
| 36 | +# o |
| 37 | +yarn install |
31 | 38 | ``` |
32 | 39 |
|
33 | | -# Tech Stack |
| 40 | +2. Levanta infra con Docker Compose (Postgres, Zookeeper, Kafka): |
34 | 41 |
|
35 | | -<ol> |
36 | | - <li>Node. You can use any framework you want (i.e. Nestjs with an ORM like TypeOrm or Prisma) </li> |
37 | | - <li>Any database</li> |
38 | | - <li>Kafka</li> |
39 | | -</ol> |
| 42 | +```bash |
| 43 | +docker compose up -d |
| 44 | +``` |
40 | 45 |
|
41 | | -We do provide a `Dockerfile` to help you get started with a dev environment. |
| 46 | +3. Ejecuta la aplicación en modo desarrollo: |
42 | 47 |
|
43 | | -You must have two resources: |
| 48 | +```bash |
| 49 | +npm run local |
| 50 | +# o |
| 51 | +yarn local |
| 52 | +``` |
44 | 53 |
|
45 | | -1. Resource to create a transaction that must containt: |
| 54 | +### Levantar los dos servicios (transaction + anti-fraud) |
46 | 55 |
|
47 | | -```json |
48 | | -{ |
49 | | - "accountExternalIdDebit": "Guid", |
50 | | - "accountExternalIdCredit": "Guid", |
51 | | - "tranferTypeId": 1, |
52 | | - "value": 120 |
53 | | -} |
| 56 | +Abre dos terminales separados y en cada uno ejecuta: |
| 57 | + |
| 58 | +Terminal A (transaction): |
| 59 | + |
| 60 | +```bash |
| 61 | +cd transaction |
| 62 | +npm install |
| 63 | +npm run local |
| 64 | +``` |
| 65 | + |
| 66 | +Terminal B (anti-fraud): |
| 67 | + |
| 68 | +```bash |
| 69 | +cd anti-fraud |
| 70 | +npm install |
| 71 | +npm run local |
54 | 72 | ``` |
55 | 73 |
|
56 | | -2. Resource to retrieve a transaction |
| 74 | +Ambos servicios deben poder conectar al broker Kafka que levantaste con `docker compose up -d`. |
| 75 | + |
| 76 | +## Cómo usar los servicios (endpoints) |
| 77 | + |
| 78 | +Base URL: `http://localhost:3000` |
| 79 | + |
| 80 | +1) Crear transacción — POST `/transaction` |
| 81 | + |
| 82 | +- Descripción: crea una transacción y publica el evento para validación anti-fraud. |
| 83 | +- Body (JSON): |
57 | 84 |
|
58 | 85 | ```json |
59 | 86 | { |
60 | | - "transactionExternalId": "Guid", |
61 | | - "transactionType": { |
62 | | - "name": "" |
63 | | - }, |
64 | | - "transactionStatus": { |
65 | | - "name": "" |
66 | | - }, |
67 | | - "value": 120, |
68 | | - "createdAt": "Date" |
| 87 | + "accountExternalIdDebit": "5429d629-c239-45fa-8235-1a386258c536", |
| 88 | + "accountExternalIdCredit": "d6cd54da-8ce3-4f79-abda-bd5be9b19e68", |
| 89 | + "tranferTypeId": 1, |
| 90 | + "value": 120 |
69 | 91 | } |
70 | 92 | ``` |
71 | 93 |
|
72 | | -## Optional |
| 94 | +Ejemplos `curl`: |
| 95 | + |
| 96 | +```bash |
| 97 | +curl -i -X POST http://localhost:3000/transaction \ |
| 98 | + -H "Content-Type: application/json" \ |
| 99 | + -d '{ |
| 100 | + "accountExternalIdDebit": "5429d629-c239-45fa-8235-1a386258c536", |
| 101 | + "accountExternalIdCredit": "d6cd54da-8ce3-4f79-abda-bd5be9b19e68", |
| 102 | + "tranferTypeId": 1, |
| 103 | + "value": 120 |
| 104 | + }' |
| 105 | +``` |
| 106 | + |
| 107 | +2) Listar transacciones — GET `/transaction` |
73 | 108 |
|
74 | | -You can use any approach to store transaction data but you should consider that we may deal with high volume scenarios where we have a huge amount of writes and reads for the same data at the same time. How would you tackle this requirement? |
| 109 | +- Descripción: devuelve todas las transacciones guardadas (útil para debug local). |
75 | 110 |
|
76 | | -You can use Graphql; |
| 111 | +```bash |
| 112 | +curl http://localhost:3000/transaction |
| 113 | +``` |
77 | 114 |
|
78 | | -# Send us your challenge |
| 115 | +## Flujo Kafka (anti-fraud) |
79 | 116 |
|
80 | | -When you finish your challenge, after forking a repository, you **must** open a pull request to our repository. There are no limitations to the implementation, you can follow the programming paradigm, modularization, and style that you feel is the most appropriate solution. |
| 117 | +- Al crear una transacción se publica un evento `transaction.created` con los datos básicos. |
| 118 | +- Un consumidor (anti-fraud) valida la transacción y emite `transaction.validated` con `{ id, status }`. |
| 119 | +- La app escucha `transaction.validated` y actualiza el `status` en la base de datos. |
| 120 | + |
| 121 | +Regla implementada en el anti-fraud (ejemplo): `if value > 1000 => rejected`; else `approved`. |
| 122 | + |
| 123 | +3. Consultar la última transacción: |
| 124 | + |
| 125 | +```sql |
| 126 | +select * from transaction where "accountExternalIdDebit" = {{ID}}; |
| 127 | +``` |
| 128 | + |
| 129 | +## DDL (esquema) para probar localmente |
| 130 | + |
| 131 | +```sql |
| 132 | + |
| 133 | +CREATE TABLE public.transaction |
| 134 | +( |
| 135 | + "accountExternalIdDebit" varchar NOT NULL, |
| 136 | + "accountExternalIdCredit" varchar NOT NULL, |
| 137 | + "tranferTypeId" integer NOT NULL, |
| 138 | + value numeric NOT NULL, |
| 139 | + id uuid DEFAULT uuid_generate_v4() NOT NULL |
| 140 | + CONSTRAINT "PK_89eadb93a89810556e1cbcd6ab9" |
| 141 | + PRIMARY KEY, |
| 142 | + "createdAt" timestamp DEFAULT now() NOT NULL, |
| 143 | + "updatedAt" timestamp DEFAULT now() NOT NULL, |
| 144 | + status integer NOT NULL |
| 145 | +); |
| 146 | +``` |
81 | 147 |
|
82 | | -If you have any questions, please let us know. |
| 148 | +Si usas TypeORM, la entidad del proyecto crea automáticamente la tabla al iniciar (si `synchronize: true`). Usa este DDL solo si quieres preparar la tabla manualmente o probar fuera del ciclo de arranque de la app. |
0 commit comments