Skip to content

Commit 2f00ab8

Browse files
Miguelmbernedo
authored andcommitted
challenge resolved
1 parent 270dee2 commit 2f00ab8

30 files changed

Lines changed: 772 additions & 57 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
#package-lock.json
107+
package-lock.json

README.md

Lines changed: 120 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,148 @@
1-
# Yape Code Challenge :rocket:
1+
# app-nodejs-codechallenge
22

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).
44

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.
66

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
1012

11-
# Problem
13+
## Requisitos
14+
- Docker & Docker Compose
15+
- Node 18+ y npm o yarn
1216

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:
1519

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
2131

22-
Every transaction with a value greater than 1000 should be rejected.
32+
1. Instala dependencias:
2333

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
3138
```
3239

33-
# Tech Stack
40+
2. Levanta infra con Docker Compose (Postgres, Zookeeper, Kafka):
3441

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+
```
4045

41-
We do provide a `Dockerfile` to help you get started with a dev environment.
46+
3. Ejecuta la aplicación en modo desarrollo:
4247

43-
You must have two resources:
48+
```bash
49+
npm run local
50+
# o
51+
yarn local
52+
```
4453

45-
1. Resource to create a transaction that must containt:
54+
### Levantar los dos servicios (transaction + anti-fraud)
4655

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
5472
```
5573

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):
5784

5885
```json
5986
{
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
6991
}
7092
```
7193

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`
73108

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).
75110

76-
You can use Graphql;
111+
```bash
112+
curl http://localhost:3000/transaction
113+
```
77114

78-
# Send us your challenge
115+
## Flujo Kafka (anti-fraud)
79116

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+
```
81147

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.

anti-fraud/.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: __dirname,
6+
sourceType: 'module',
7+
},
8+
plugins: ['@typescript-eslint/eslint-plugin'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true,
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
},
25+
};

anti-fraud/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

anti-fraud/nest-cli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

anti-fraud/package.json

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "anti-fraud",
3+
"version": "0.0.1",
4+
"description": "",
5+
"author": "",
6+
"private": true,
7+
"license": "UNLICENSED",
8+
"scripts": {
9+
"build": "nest build",
10+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11+
"start": "nest start",
12+
"local": "nest start --watch",
13+
"start:debug": "nest start --debug --watch",
14+
"start:prod": "node dist/main",
15+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16+
"test": "jest",
17+
"test:watch": "jest --watch",
18+
"test:cov": "jest --coverage",
19+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20+
"test:e2e": "jest --config ./test/jest-e2e.json"
21+
},
22+
"dependencies": {
23+
"@nestjs/common": "^10.0.0",
24+
"@nestjs/config": "^4.0.2",
25+
"@nestjs/core": "^10.0.0",
26+
"@nestjs/microservices": "^10.4.18",
27+
"@nestjs/platform-express": "^10.0.0",
28+
"@nestjs/typeorm": "^11.0.0",
29+
"class-transformer": "^0.5.1",
30+
"class-validator": "^0.14.2",
31+
"kafkajs": "^2.2.4",
32+
"pg": "^8.16.0",
33+
"reflect-metadata": "^0.2.0",
34+
"rxjs": "^7.8.1",
35+
"typeorm": "^0.3.24"
36+
},
37+
"devDependencies": {
38+
"@nestjs/cli": "^10.0.0",
39+
"@nestjs/schematics": "^10.0.0",
40+
"@nestjs/testing": "^10.0.0",
41+
"@types/express": "^5.0.0",
42+
"@types/jest": "^29.5.2",
43+
"@types/node": "^20.3.1",
44+
"@types/supertest": "^6.0.0",
45+
"@typescript-eslint/eslint-plugin": "^8.0.0",
46+
"@typescript-eslint/parser": "^8.0.0",
47+
"eslint": "^8.0.0",
48+
"eslint-config-prettier": "^9.0.0",
49+
"eslint-plugin-prettier": "^5.0.0",
50+
"jest": "^29.5.0",
51+
"prettier": "^3.0.0",
52+
"source-map-support": "^0.5.21",
53+
"supertest": "^7.0.0",
54+
"ts-jest": "^29.1.0",
55+
"ts-loader": "^9.4.3",
56+
"ts-node": "^10.9.1",
57+
"tsconfig-paths": "^4.2.0",
58+
"typescript": "^5.1.3"
59+
},
60+
"jest": {
61+
"moduleFileExtensions": [
62+
"js",
63+
"json",
64+
"ts"
65+
],
66+
"rootDir": "src",
67+
"testRegex": ".*\\.spec\\.ts$",
68+
"transform": {
69+
"^.+\\.(t|j)s$": "ts-jest"
70+
},
71+
"collectCoverageFrom": [
72+
"**/*.(t|j)s"
73+
],
74+
"coverageDirectory": "../coverage",
75+
"testEnvironment": "node"
76+
}
77+
}

anti-fraud/src/app.controller.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Controller, Logger } from '@nestjs/common';
2+
import { MessagePattern, Payload } from '@nestjs/microservices';
3+
import { AppService } from './app.service';
4+
import { TransactionCreatedEvent } from './models/transaction-created.event';
5+
6+
@Controller()
7+
export class AppController {
8+
constructor(private readonly appService: AppService) {}
9+
10+
@MessagePattern('transaction.created')
11+
async approvedTransaction(
12+
@Payload() payload: TransactionCreatedEvent,
13+
): Promise<void> {
14+
Logger.debug(payload);
15+
await this.appService.evaluateTransaction(payload);
16+
}
17+
}

anti-fraud/src/app.module.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Module } from '@nestjs/common';
2+
import { ConfigModule } from '@nestjs/config';
3+
import { ClientsModule, Transport } from '@nestjs/microservices';
4+
import { AppController } from './app.controller';
5+
import { AppService } from './app.service';
6+
7+
@Module({
8+
imports: [
9+
ConfigModule.forRoot(),
10+
ClientsModule.register([
11+
{
12+
name: 'KAFKA',
13+
transport: Transport.KAFKA,
14+
options: {
15+
client: {
16+
clientId: 'anti-fraud',
17+
brokers: [`${process.env.KAFKA_HOST}:9092`],
18+
},
19+
consumer: {
20+
groupId: 'anti-fraud-consumer',
21+
},
22+
},
23+
},
24+
]),
25+
],
26+
controllers: [AppController],
27+
providers: [AppService]
28+
})
29+
export class AppModule { }

0 commit comments

Comments
 (0)