import { Injectable } from "@nestjs/common"; import { DataSource, FindOptionsWhere, QueryDeepPartialEntity, QueryRunner, Repository } from "typeorm"; import { PaymentEntity } from "./entities/payment.entity"; @Injectable() export class PaymentRepository { private readonly paymentRepo: Repository; constructor(private readonly dataSource: DataSource) { this.paymentRepo = this.dataSource.getRepository(PaymentEntity) } async createTr(qr: QueryRunner, data: Pick): Promise { const payment = qr.manager.create(PaymentEntity, data) return qr.manager.save(payment) } findOneBy(options: FindOptionsWhere | FindOptionsWhere[]): Promise { return this.paymentRepo.findOneBy(options); } update(where: FindOptionsWhere, data: QueryDeepPartialEntity) { return this.paymentRepo.update(where, data) } getActivePaymentByRefIdAndMethod(refId: string, method: PaymentEntity["method"]) { return this.paymentRepo .createQueryBuilder('payment') .where('payment.method = :method', { method }) .andWhere('payment.refId = :refId', { refId }) .andWhere('payment.status IN (:...statuses)', { statuses: ['action-required'], }) .andWhere('payment.expiresAt > :now', { now: new Date() }) .getOne(); } }