mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -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,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user