Refactor payment processors to use CreatePaymentDto and streamline payment execution logic
This commit is contained in:
parent
3c2c43106a
commit
434f03f6c6
@ -4,9 +4,8 @@ import { Payment } from '../entities/payment.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
||||
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||
import { PaymentJobData } from '../../queue/queue.service';
|
||||
import { CreatePaymentDto } from '../dto/create-payment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PaymentDefaultProcessor {
|
||||
@ -18,7 +17,7 @@ export class PaymentDefaultProcessor {
|
||||
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||
) {}
|
||||
|
||||
async execute(payment: PaymentJobData): Promise<boolean> {
|
||||
async execute(payment: CreatePaymentDto): Promise<boolean> {
|
||||
const url = this.configService.get('paymentProcessors.defaultUrl');
|
||||
|
||||
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||
@ -26,17 +25,13 @@ export class PaymentDefaultProcessor {
|
||||
url,
|
||||
);
|
||||
|
||||
if (responseExists) {
|
||||
await this.repository.save({
|
||||
amount: payment.paymentData.amount,
|
||||
correlationId: payment.paymentData.correlationId,
|
||||
createdAt: payment.createdAt,
|
||||
paymentProcessor: ProcessorTypeEnum.DEFAULT,
|
||||
status: PaymentStatusEnum.SUCCESS,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (!responseExists) return false;
|
||||
|
||||
return false;
|
||||
await this.repository.save({
|
||||
...payment,
|
||||
paymentProcessor: ProcessorTypeEnum.DEFAULT,
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,9 +4,8 @@ import { Payment } from '../entities/payment.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
||||
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||
import { PaymentJobData } from '../../queue/queue.service';
|
||||
import { CreatePaymentDto } from '../dto/create-payment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class PaymentFallbackProcessor {
|
||||
@ -18,7 +17,7 @@ export class PaymentFallbackProcessor {
|
||||
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||
) {}
|
||||
|
||||
async execute(payment: PaymentJobData): Promise<boolean> {
|
||||
async execute(payment: CreatePaymentDto): Promise<boolean> {
|
||||
const url = this.configService.get('paymentProcessors.fallbackUrl');
|
||||
|
||||
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||
@ -26,16 +25,12 @@ export class PaymentFallbackProcessor {
|
||||
url,
|
||||
);
|
||||
|
||||
if (responseExists) {
|
||||
await this.repository.save({
|
||||
amount: payment.paymentData.amount,
|
||||
correlationId: payment.paymentData.correlationId,
|
||||
createdAt: payment.createdAt,
|
||||
paymentProcessor: ProcessorTypeEnum.FALLBACK,
|
||||
status: PaymentStatusEnum.SUCCESS,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
if (!responseExists) return false;
|
||||
|
||||
await this.repository.save({
|
||||
...payment,
|
||||
paymentProcessor: ProcessorTypeEnum.FALLBACK,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,21 +1,32 @@
|
||||
import { Job } from 'bullmq';
|
||||
import { PaymentJobData } from '../../queue/queue.service';
|
||||
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { ProcessPaymentService } from '../services/process-payment.service';
|
||||
import { PAYMENT_QUEUE } from '../../queue/constants/queue.constants';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Payment } from '../entities/payment.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreatePaymentDto } from '../dto/create-payment.dto';
|
||||
|
||||
@Processor(PAYMENT_QUEUE, { concurrency: 2 })
|
||||
@Injectable()
|
||||
export class PaymentProcessor extends WorkerHost {
|
||||
private readonly logger = new Logger(PaymentProcessor.name);
|
||||
|
||||
constructor(private processPaymentService: ProcessPaymentService) {
|
||||
constructor(
|
||||
private processPaymentService: ProcessPaymentService,
|
||||
@InjectRepository(Payment)
|
||||
private readonly paymentRepository: Repository<Payment>,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async process(job: Job<PaymentJobData>) {
|
||||
async process(job: Job<CreatePaymentDto>) {
|
||||
const payment = job.data;
|
||||
await this.processPaymentService.execute(payment);
|
||||
const exists = await this.paymentRepository.findOne({
|
||||
where: { correlationId: payment.correlationId },
|
||||
});
|
||||
|
||||
if (!exists) await this.processPaymentService.execute(payment);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user