feat(nestjs): migrate to NestJS

This commit is contained in:
Ahmed Bouhuolia
2025-04-07 11:51:24 +02:00
parent f068218a16
commit 55fcc908ef
3779 changed files with 631 additions and 195332 deletions

View File

@@ -0,0 +1,59 @@
import { Knex } from 'knex';
import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { TransactionPaymentServiceEntry } from '../models/TransactionPaymentServiceEntry.model';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { events } from '@/common/events/events';
@Injectable()
export class DeletePaymentMethodService {
constructor(
private readonly uow: UnitOfWork,
private readonly eventEmitter: EventEmitter2,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
@Inject(TransactionPaymentServiceEntry.name)
private readonly transactionPaymentServiceEntryModel: TenantModelProxy<
typeof TransactionPaymentServiceEntry
>,
) {}
/**
* Deletes the given payment integration.
* @param {number} paymentIntegrationId
* @returns {Promise<void>}
*/
public async deletePaymentMethod(
paymentIntegrationId: number,
): Promise<void> {
const paymentIntegration = await this.paymentIntegrationModel()
.query()
.findById(paymentIntegrationId)
.throwIfNotFound();
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Delete payment methods links.
await this.transactionPaymentServiceEntryModel()
.query(trx)
.where('paymentIntegrationId', paymentIntegrationId)
.delete();
// Delete the payment integration.
await this.paymentIntegrationModel()
.query(trx)
.findById(paymentIntegrationId)
.delete();
// Triggers `onPaymentMethodDeleted` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onDeleted, {
paymentIntegrationId,
});
});
}
}

View File

@@ -0,0 +1,58 @@
import { Knex } from 'knex';
import { EditPaymentMethodDTO } from '../types';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { PaymentIntegration } from '../models/PaymentIntegration.model';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Inject, Injectable } from '@nestjs/common';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { events } from '@/common/events/events';
@Injectable()
export class EditPaymentMethodService {
constructor(
private readonly uow: UnitOfWork,
private readonly eventEmitter: EventEmitter2,
@Inject(PaymentIntegration.name)
private readonly paymentIntegrationModel: TenantModelProxy<
typeof PaymentIntegration
>,
) {}
/**
* Edits the given payment method.
* @param {number} paymentIntegrationId - The ID of the payment method.
* @param {EditPaymentMethodDTO} editPaymentMethodDTO
* @returns {Promise<void>}
*/
async editPaymentMethod(
paymentIntegrationId: number,
editPaymentMethodDTO: EditPaymentMethodDTO,
): Promise<void> {
const paymentMethod = await this.paymentIntegrationModel()
.query()
.findById(paymentIntegrationId)
.throwIfNotFound();
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
// Triggers `onPaymentMethodEditing` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onEditing, {
paymentIntegrationId,
editPaymentMethodDTO,
trx,
});
await this.paymentIntegrationModel()
.query(trx)
.findById(paymentIntegrationId)
.patch({
...editPaymentMethodDTO,
});
// Triggers `onPaymentMethodEdited` event.
await this.eventEmitter.emitAsync(events.paymentMethod.onEdited, {
paymentIntegrationId,
editPaymentMethodDTO,
trx,
});
});
}
}