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
130 changes: 41 additions & 89 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,56 @@
# compiled output
/dist
/node_modules
/build

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# temp directory
.temp
.tmp

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.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"
}
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run build

RUN npm run build anti-fraud
22 changes: 22 additions & 0 deletions apps/anti-fraud/src/anti-fraud.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AntiFraudController } from './anti-fraud.controller';
import { AntiFraudService } from './anti-fraud.service';

describe('AntiFraudController', () => {
let antiFraudController: AntiFraudController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AntiFraudController],
providers: [AntiFraudService],
}).compile();

antiFraudController = app.get<AntiFraudController>(AntiFraudController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(antiFraudController.getHello()).toBe('Hello World!');
});
});
});
14 changes: 14 additions & 0 deletions apps/anti-fraud/src/anti-fraud.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller, Get } from '@nestjs/common';
import { AntiFraudService } from './anti-fraud.service';
import { EventPattern, Payload } from '@nestjs/microservices';

@Controller()
export class AntiFraudController {
constructor(private readonly antiFraudService: AntiFraudService) {}

@EventPattern('transaction_created')
handleTransactionCreated(@Payload() message: any) {
const transactionData = message.value ? message.value : message;
this.antiFraudService.handleTransactionValidation(transactionData);
}
}
31 changes: 31 additions & 0 deletions apps/anti-fraud/src/anti-fraud.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Module } from '@nestjs/common';
import { AntiFraudController } from './anti-fraud.controller';
import { AntiFraudService } from './anti-fraud.service';
import { ConfigModule } from '@nestjs/config';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
ClientsModule.register([
{
name: 'KAFKA_SERVICE',
transport: Transport.KAFKA,
options: {
client: {
brokers: [process.env.KAFKA_BROKER || 'kafka:9092'],
},
consumer: {
groupId: 'anti-fraud-consumer',
},
subscribe: {
fromBeginning: true,
},
},
},
]),
],
controllers: [AntiFraudController],
providers: [AntiFraudService],
})
export class AntiFraudModule {}
27 changes: 27 additions & 0 deletions apps/anti-fraud/src/anti-fraud.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { ClientKafka } from '@nestjs/microservices';

@Injectable()
export class AntiFraudService implements OnModuleInit {
constructor(
@Inject('KAFKA_SERVICE') private readonly kafkaClient: ClientKafka,
) {}

async onModuleInit() {
await this.kafkaClient.connect();
}

handleTransactionValidation(data: any) {
const { transactionExternalId, transactionValue } = data;
const status = transactionValue > 1000 ? 'rejected' : 'approved';

console.log(`Transaction ${transactionExternalId} with value ${transactionValue}. Status: ${status}`);

const payload = {
transactionExternalId,
status,
};

this.kafkaClient.emit('transaction_status_updated', JSON.stringify(payload));
}
}
25 changes: 25 additions & 0 deletions apps/anti-fraud/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NestFactory } from '@nestjs/core';
import { AntiFraudModule } from './anti-fraud.module';
import { MicroserviceOptions } from '@nestjs/microservices';
import { Transport } from '@nestjs/microservices';

async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AntiFraudModule,
{
transport: Transport.KAFKA,
options: {
client: {
brokers: [process.env.KAFKA_BROKER || 'kafka:9092'],
},
consumer: {
groupId: 'anti-fraud-consumer',
},
},
},
);

await app.listen();
}

bootstrap();
24 changes: 24 additions & 0 deletions apps/anti-fraud/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AntiFraudModule } from './../src/anti-fraud.module';

describe('AntiFraudController (e2e)', () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AntiFraudModule],
}).compile();

app = moduleFixture.createNestApplication();
await app.init();
});

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
});
});
9 changes: 9 additions & 0 deletions apps/anti-fraud/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
9 changes: 9 additions & 0 deletions apps/anti-fraud/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": false,
"outDir": "../../dist/apps/anti-fraud"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
22 changes: 22 additions & 0 deletions apps/yape-challenge/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});
28 changes: 28 additions & 0 deletions apps/yape-challenge/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { AppService } from './app.service';
import { CreateTransactionDto } from './dto/create-transaction.dto';
import { EventPattern, Payload } from '@nestjs/microservices';

@Controller('transactions')
export class AppController {
constructor(private readonly appService: AppService,) {}

@Post()
create(@Body() createTransactionDto: CreateTransactionDto) {
return this.appService.createTransaction(createTransactionDto);
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.appService.findTransaction(id);
}

@EventPattern('transaction_status_updated')
async handleTransactionStatusUpdated(@Payload() message: any) {
const data = message.value ? message.value : message;

console.log(`Update Request Received for: ${data.transactionExternalId} with status ${data.status}`);

await this.appService.updateStatus(data.transactionExternalId, data.status);
}
}
Loading