Add payment module with DTOs, entity, controller, and service
This commit is contained in:
parent
48c9a6a016
commit
4284ecfca4
9
src/modules/payments/dto/create-payment.dto.ts
Normal file
9
src/modules/payments/dto/create-payment.dto.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import {IsNumber, IsUUID} from "class-validator";
|
||||
|
||||
export class CreatePaymentDto {
|
||||
@IsNumber()
|
||||
amount: number;
|
||||
|
||||
@IsUUID()
|
||||
correlationId: string;
|
||||
}
|
||||
4
src/modules/payments/dto/update-payment.dto.ts
Normal file
4
src/modules/payments/dto/update-payment.dto.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreatePaymentDto } from './create-payment.dto';
|
||||
|
||||
export class UpdatePaymentDto extends PartialType(CreatePaymentDto) {}
|
||||
24
src/modules/payments/entities/payment.entity.ts
Normal file
24
src/modules/payments/entities/payment.entity.ts
Normal 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;
|
||||
}
|
||||
13
src/modules/payments/payments.controller.ts
Normal file
13
src/modules/payments/payments.controller.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
20
src/modules/payments/payments.module.ts
Normal file
20
src/modules/payments/payments.module.ts
Normal 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 {}
|
||||
39
src/modules/payments/payments.service.ts
Normal file
39
src/modules/payments/payments.service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user