Skip to content
Closed
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
25 changes: 25 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

#package-lock.json
package-lock.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
90 changes: 25 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,42 @@
# Yape Code Challenge :rocket:
## 🚀 Instrucciones para levantar el entorno

Our code challenge will let you marvel us with your Jedi coding skills :smile:.
### 1. Crear archivo .env en la raiz

Don't forget that the proper way to submit your work is to fork the repo and create a PR :wink: ... have fun !!
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres

- [Problem](#problem)
- [Tech Stack](#tech_stack)
- [Send us your challenge](#send_us_your_challenge)
### 2. Instalar las dependencias

# Problem
npm install

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.
For now, we have only three transaction statuses:
# o

<ol>
<li>pending</li>
<li>approved</li>
<li>rejected</li>
</ol>
yarn install

Every transaction with a value greater than 1000 should be rejected.
### 3. Levantar los servicios de base de datos, zookeeper y kafka

```mermaid
flowchart LR
Transaction -- Save Transaction with pending Status --> transactionDatabase[(Database)]
Transaction --Send transaction Created event--> Anti-Fraud
Anti-Fraud -- Send transaction Status Approved event--> Transaction
Anti-Fraud -- Send transaction Status Rejected event--> Transaction
Transaction -- Update transaction Status event--> transactionDatabase[(Database)]
```
docker-compose up -d

# Tech Stack
### 4. Correr la aplicacion

<ol>
<li>Node. You can use any framework you want (i.e. Nestjs with an ORM like TypeOrm or Prisma) </li>
<li>Any database</li>
<li>Kafka</li>
</ol>
npm run local

We do provide a `Dockerfile` to help you get started with a dev environment.
# o

You must have two resources:
yarn local

1. Resource to create a transaction that must containt:
### 5. Probar los servicios usando los siguientes curl

```json
{
"accountExternalIdDebit": "Guid",
"accountExternalIdCredit": "Guid",
curl --location 'localhost:3000/transaction' \
--header 'Content-Type: application/json' \
--data '{
"accountExternalIdDebit": "5429d629-c239-45fa-8235-1a386258c536",
"accountExternalIdCredit": "d6cd54da-8ce3-4f79-abda-bd5be9b19e68",
"tranferTypeId": 1,
"value": 120
}
```
}'

2. Resource to retrieve a transaction

```json
{
"transactionExternalId": "Guid",
"transactionType": {
"name": ""
},
"transactionStatus": {
"name": ""
},
"value": 120,
"createdAt": "Date"
}
```

## Optional

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?

You can use Graphql;

# Send us your challenge

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.

If you have any questions, please let us know.
curl --location 'localhost:3000/transaction/5429d629-c239-45fa-8235-1a386258c536'
28 changes: 16 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ services:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
zookeeper:
image: confluentinc/cp-zookeeper:5.5.3
image: bitnami/zookeeper:3.9
container_name: zookeeper
ports:
- '2181:2181'
environment:
ZOOKEEPER_CLIENT_PORT: 2181
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
image: confluentinc/cp-enterprise-kafka:5.5.3
depends_on: [zookeeper]
environment:
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_JMX_PORT: 9991
image: bitnami/kafka:3.6
container_name: kafka
ports:
- 9092:9092
- '9092:9092'
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- ALLOW_PLAINTEXT_LISTENER=yes
depends_on:
- zookeeper
8 changes: 8 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
77 changes: 77 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"name": "app-nodejs-codechallenge",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"local": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^10.0.0",
"@nestjs/microservices": "^10.4.18",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^11.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.2",
"kafkajs": "^2.2.4",
"pg": "^8.16.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.24"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"eslint": "^8.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
26 changes: 26 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Module } from '@nestjs/common';
import { TransactionModule } from './transaction/transaction.module';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT, 10),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
autoLoadEntities: true,
synchronize: true,
}),
TransactionModule,
],
controllers: [],
providers: [],
})
export class AppModule { }
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.KAFKA,
options: {
client: {
brokers: ['localhost:9092'],
},
consumer: {
groupId: 'transaction-consumer-group',
},
},
});

await app.startAllMicroservices();
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
13 changes: 13 additions & 0 deletions src/shared/enum/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum TransferType {
TRANSFER = 1,
PAYMENT = 2,
REFUND = 3,
WITHDRAWAL = 4
}

export const TransferTypeLabel = {
[TransferType.TRANSFER]: 'transfer',
[TransferType.PAYMENT]: 'payment',
[TransferType.REFUND]: 'refund',
[TransferType.WITHDRAWAL]: 'withdrawal',
} as const;
8 changes: 8 additions & 0 deletions src/shared/kafka/kafka.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { KafkaProducerService } from './kafka.producer';

@Module({
providers: [KafkaProducerService],
exports: [KafkaProducerService],
})
export class KafkaModule {}
29 changes: 29 additions & 0 deletions src/shared/kafka/kafka.producer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { Kafka, Producer } from 'kafkajs';

@Injectable()
export class KafkaProducerService implements OnModuleInit, OnModuleDestroy {
private readonly kafka = new Kafka({
brokers: ['localhost:9092'],
});

private readonly producer: Producer = this.kafka.producer();

async onModuleInit() {
await this.producer.connect();
console.log('[KafkaProducer] Conectado');
}

async emit(topic: string, message: any) {
await this.producer.send({
topic,
messages: [{ value: JSON.stringify(message) }],
});

console.log(`[KafkaProducer] Emitido a ${topic}`, message);
}

async onModuleDestroy() {
await this.producer.disconnect();
}
}
16 changes: 16 additions & 0 deletions src/transaction/dto/create-transaction.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsEnum, IsPositive, IsUUID } from 'class-validator';
import { TransferType } from 'src/shared/enum/enum';

export class CreateTransactionDto {
@IsUUID()
accountExternalIdDebit: string;

@IsUUID()
accountExternalIdCredit: string;

@IsEnum(TransferType)
tranferTypeId: number;

@IsPositive()
value: number
}
Loading