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 { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
|
||||||
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||||
import { PaymentJobData } from '../../queue/queue.service';
|
import { CreatePaymentDto } from '../dto/create-payment.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PaymentDefaultProcessor {
|
export class PaymentDefaultProcessor {
|
||||||
@ -18,7 +17,7 @@ export class PaymentDefaultProcessor {
|
|||||||
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(payment: PaymentJobData): Promise<boolean> {
|
async execute(payment: CreatePaymentDto): Promise<boolean> {
|
||||||
const url = this.configService.get('paymentProcessors.defaultUrl');
|
const url = this.configService.get('paymentProcessors.defaultUrl');
|
||||||
|
|
||||||
const responseExists = await this.makePaymentToProcessorService.execute(
|
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||||
@ -26,17 +25,13 @@ export class PaymentDefaultProcessor {
|
|||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (responseExists) {
|
if (!responseExists) return false;
|
||||||
await this.repository.save({
|
|
||||||
amount: payment.paymentData.amount,
|
|
||||||
correlationId: payment.paymentData.correlationId,
|
|
||||||
createdAt: payment.createdAt,
|
|
||||||
paymentProcessor: ProcessorTypeEnum.DEFAULT,
|
|
||||||
status: PaymentStatusEnum.SUCCESS,
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
import { ProcessorTypeEnum } from '../enumns/processor-type.enum';
|
||||||
import { PaymentStatusEnum } from '../enumns/payment-status.enum';
|
|
||||||
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
import { MakePaymentToProcessorService } from '../services/make-payment-to-processor.service';
|
||||||
import { PaymentJobData } from '../../queue/queue.service';
|
import { CreatePaymentDto } from '../dto/create-payment.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PaymentFallbackProcessor {
|
export class PaymentFallbackProcessor {
|
||||||
@ -18,7 +17,7 @@ export class PaymentFallbackProcessor {
|
|||||||
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
private makePaymentToProcessorService: MakePaymentToProcessorService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async execute(payment: PaymentJobData): Promise<boolean> {
|
async execute(payment: CreatePaymentDto): Promise<boolean> {
|
||||||
const url = this.configService.get('paymentProcessors.fallbackUrl');
|
const url = this.configService.get('paymentProcessors.fallbackUrl');
|
||||||
|
|
||||||
const responseExists = await this.makePaymentToProcessorService.execute(
|
const responseExists = await this.makePaymentToProcessorService.execute(
|
||||||
@ -26,16 +25,12 @@ export class PaymentFallbackProcessor {
|
|||||||
url,
|
url,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (responseExists) {
|
if (!responseExists) return false;
|
||||||
await this.repository.save({
|
|
||||||
amount: payment.paymentData.amount,
|
await this.repository.save({
|
||||||
correlationId: payment.paymentData.correlationId,
|
...payment,
|
||||||
createdAt: payment.createdAt,
|
paymentProcessor: ProcessorTypeEnum.FALLBACK,
|
||||||
paymentProcessor: ProcessorTypeEnum.FALLBACK,
|
});
|
||||||
status: PaymentStatusEnum.SUCCESS,
|
return true;
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,32 @@
|
|||||||
import { Job } from 'bullmq';
|
import { Job } from 'bullmq';
|
||||||
import { PaymentJobData } from '../../queue/queue.service';
|
|
||||||
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { ProcessPaymentService } from '../services/process-payment.service';
|
import { ProcessPaymentService } from '../services/process-payment.service';
|
||||||
import { PAYMENT_QUEUE } from '../../queue/constants/queue.constants';
|
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 })
|
@Processor(PAYMENT_QUEUE, { concurrency: 2 })
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PaymentProcessor extends WorkerHost {
|
export class PaymentProcessor extends WorkerHost {
|
||||||
private readonly logger = new Logger(PaymentProcessor.name);
|
private readonly logger = new Logger(PaymentProcessor.name);
|
||||||
|
|
||||||
constructor(private processPaymentService: ProcessPaymentService) {
|
constructor(
|
||||||
|
private processPaymentService: ProcessPaymentService,
|
||||||
|
@InjectRepository(Payment)
|
||||||
|
private readonly paymentRepository: Repository<Payment>,
|
||||||
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
async process(job: Job<PaymentJobData>) {
|
async process(job: Job<CreatePaymentDto>) {
|
||||||
const payment = job.data;
|
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