Add payment module with DTOs, entity, controller, and service

This commit is contained in:
Jose Eduardo 2025-08-03 20:58:50 -04:00
parent 48c9a6a016
commit 4284ecfca4
6 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,9 @@
import {IsNumber, IsUUID} from "class-validator";
export class CreatePaymentDto {
@IsNumber()
amount: number;
@IsUUID()
correlationId: string;
}

View File

@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreatePaymentDto } from './create-payment.dto';
export class UpdatePaymentDto extends PartialType(CreatePaymentDto) {}

View File

@ -0,0 +1,24 @@
import {
Column,
CreateDateColumn,
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity('payments')
export class Payment {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'decimal', precision: 10, scale: 2 })
amount: number;
@Column({ type: 'varchar', name: 'payment_processor' })
paymentProcessor: string;
@Column({ type: 'uuid', name: 'correlation_id', unique: true })
correlationId: string;
@CreateDateColumn({ type: 'timestamp', name: 'created_at' })
createdAt: Date;
}

View File

@ -0,0 +1,13 @@
import { Body, Controller, Post } from '@nestjs/common';
import { PaymentsService } from './payments.service';
import { CreatePaymentDto } from './dto/create-payment.dto';
@Controller('payments')
export class PaymentsController {
constructor(private readonly paymentsService: PaymentsService) {}
@Post()
async payment(@Body() createPaymentDto: CreatePaymentDto) {
return this.paymentsService.store(createPaymentDto);
}
}

View File

@ -0,0 +1,20 @@
import { Module } from '@nestjs/common';
import { PaymentsService } from './payments.service';
import { PaymentsController } from './payments.controller';
import { HttpModule } from '@nestjs/axios';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Payment } from './entities/payment.entity';
import { BullModule } from '@nestjs/bullmq';
@Module({
imports: [
TypeOrmModule.forFeature([Payment]),
BullModule.registerQueue({
name: 'payments',
}),
HttpModule,
],
controllers: [PaymentsController],
providers: [PaymentsService],
})
export class PaymentsModule {}

View File

@ -0,0 +1,39 @@
import { Injectable, Logger } from '@nestjs/common';
import { CreatePaymentDto } from './dto/create-payment.dto';
import { HttpService } from '@nestjs/axios';
import { catchError, firstValueFrom } from 'rxjs';
import { AxiosError } from 'axios';
import { InjectRepository } from '@nestjs/typeorm';
import { Payment } from './entities/payment.entity';
import { Repository } from 'typeorm';
import { Queue } from 'bullmq';
import { InjectQueue } from '@nestjs/bullmq';
@Injectable()
export class PaymentsService {
private readonly logger = new Logger(PaymentsService.name);
constructor(
@InjectQueue('payments') private readonly paymentsQueue: Queue,
private readonly httpService: HttpService,
@InjectRepository(Payment) private readonly repository: Repository<Payment>,
) {}
async store(createPaymentDto: CreatePaymentDto) {
const urlPaymentDefault = 'http://192.168.1.126:8002/payments';
const { data } = await firstValueFrom(
this.httpService.post(urlPaymentDefault, createPaymentDto).pipe(
catchError((error: AxiosError) => {
this.logger.error(error?.response);
throw 'An error happened!';
}),
),
);
await this.repository.save({
...createPaymentDto,
paymentProcessor: 'default',
});
this.logger.log(data);
return data;
}
}