mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 14:50:32 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import {
|
||||
validateTransactionNotCategorized,
|
||||
validateTransactionNotExcluded,
|
||||
} from './utils';
|
||||
import {
|
||||
IBankTransactionUnexcludedEventPayload,
|
||||
IBankTransactionUnexcludingEventPayload,
|
||||
} from '../types/BankTransactionsExclude.types';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class ExcludeBankTransactionService {
|
||||
constructor(
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private uncategorizedBankTransactionModel: TenantModelProxy<
|
||||
typeof UncategorizedBankTransaction
|
||||
>,
|
||||
|
||||
private uow: UnitOfWork,
|
||||
private eventEmitter: EventEmitter2,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Marks the given bank transaction as excluded.
|
||||
* @param {number} uncategorizedTransactionId - Uncategorized bank transaction identifier.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async excludeBankTransaction(uncategorizedTransactionId: number) {
|
||||
const oldUncategorizedTransaction =
|
||||
await this.uncategorizedBankTransactionModel()
|
||||
.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate the transaction shouldn't be excluded.
|
||||
validateTransactionNotExcluded(oldUncategorizedTransaction);
|
||||
|
||||
// Validate the transaction shouldn't be categorized.
|
||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
||||
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
await this.eventEmitter.emitAsync(events.bankTransactions.onExcluding, {
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
} as IBankTransactionUnexcludingEventPayload);
|
||||
|
||||
await this.uncategorizedBankTransactionModel()
|
||||
.query(trx)
|
||||
.findById(uncategorizedTransactionId)
|
||||
.patch({
|
||||
excludedAt: new Date(),
|
||||
});
|
||||
|
||||
await this.eventEmitter.emitAsync(events.bankTransactions.onExcluded, {
|
||||
uncategorizedTransactionId,
|
||||
trx,
|
||||
} as IBankTransactionUnexcludedEventPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import PromisePool from '@supercharge/promise-pool';
|
||||
import { castArray, uniq } from 'lodash';
|
||||
import { ExcludeBankTransactionService } from './ExcludeBankTransaction.service';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class ExcludeBankTransactionsService {
|
||||
constructor(
|
||||
private readonly excludeBankTransaction: ExcludeBankTransactionService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Exclude bank transactions in bulk.
|
||||
* @param {Array<number> | number} bankTransactionIds - The IDs of the bank transactions to exclude.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async excludeBankTransactions(
|
||||
bankTransactionIds: Array<number> | number,
|
||||
) {
|
||||
const _bankTransactionIds = uniq(castArray(bankTransactionIds));
|
||||
|
||||
await PromisePool.withConcurrency(1)
|
||||
.for(_bankTransactionIds)
|
||||
.process((bankTransactionId: number) => {
|
||||
return this.excludeBankTransaction.excludeBankTransaction(
|
||||
bankTransactionId,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
validateTransactionNotCategorized,
|
||||
validateTransactionShouldBeExcluded,
|
||||
} from './utils';
|
||||
import {
|
||||
IBankTransactionExcludedEventPayload,
|
||||
IBankTransactionExcludingEventPayload,
|
||||
} from '../types/BankTransactionsExclude.types';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { UncategorizedBankTransaction } from '@/modules/BankingTransactions/models/UncategorizedBankTransaction';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class UnexcludeBankTransactionService {
|
||||
constructor(
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly uow: UnitOfWork,
|
||||
|
||||
@Inject(UncategorizedBankTransaction.name)
|
||||
private readonly uncategorizedBankTransactionModel: TenantModelProxy<
|
||||
typeof UncategorizedBankTransaction
|
||||
>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Marks the given bank transaction as excluded.
|
||||
* @param {number} tenantId
|
||||
* @param {number} bankTransactionId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public async unexcludeBankTransaction(
|
||||
uncategorizedTransactionId: number,
|
||||
): Promise<void> {
|
||||
const oldUncategorizedTransaction =
|
||||
await this.uncategorizedBankTransactionModel()
|
||||
.query()
|
||||
.findById(uncategorizedTransactionId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate the transaction should be excludded.
|
||||
validateTransactionShouldBeExcluded(oldUncategorizedTransaction);
|
||||
|
||||
// Validate the transaction shouldn't be categorized.
|
||||
validateTransactionNotCategorized(oldUncategorizedTransaction);
|
||||
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
await this.eventEmitter.emitAsync(events.bankTransactions.onUnexcluding, {
|
||||
uncategorizedTransactionId,
|
||||
} as IBankTransactionExcludingEventPayload);
|
||||
|
||||
await this.uncategorizedBankTransactionModel()
|
||||
.query(trx)
|
||||
.findById(uncategorizedTransactionId)
|
||||
.patch({
|
||||
excludedAt: null,
|
||||
});
|
||||
|
||||
await this.eventEmitter.emitAsync(events.bankTransactions.onUnexcluded, {
|
||||
uncategorizedTransactionId,
|
||||
} as IBankTransactionExcludedEventPayload);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import { castArray, uniq } from 'lodash';
|
||||
import { UnexcludeBankTransactionService } from './UnexcludeBankTransaction.service';
|
||||
|
||||
@Injectable()
|
||||
export class UnexcludeBankTransactionsService {
|
||||
constructor(
|
||||
private readonly unexcludeBankTransaction: UnexcludeBankTransactionService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Unexclude bank transactions in bulk.
|
||||
* @param {Array<number> | number} bankTransactionIds - The IDs of the bank transactions to unexclude.
|
||||
*/
|
||||
public async unexcludeBankTransactions(
|
||||
bankTransactionIds: Array<number> | number
|
||||
) {
|
||||
const _bankTransactionIds = uniq(castArray(bankTransactionIds));
|
||||
|
||||
await PromisePool.withConcurrency(1)
|
||||
.for(_bankTransactionIds)
|
||||
.process((bankTransactionId: number) => {
|
||||
return this.unexcludeBankTransaction.unexcludeBankTransaction(
|
||||
bankTransactionId
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { UncategorizedBankTransaction } from "@/modules/BankingTransactions/models/UncategorizedBankTransaction";
|
||||
import { ServiceError } from "@/modules/Items/ServiceError";
|
||||
|
||||
const ERRORS = {
|
||||
TRANSACTION_ALREADY_CATEGORIZED: 'TRANSACTION_ALREADY_CATEGORIZED',
|
||||
TRANSACTION_ALREADY_EXCLUDED: 'TRANSACTION_ALREADY_EXCLUDED',
|
||||
TRANSACTION_NOT_EXCLUDED: 'TRANSACTION_NOT_EXCLUDED',
|
||||
};
|
||||
|
||||
export const validateTransactionNotCategorized = (
|
||||
transaction: UncategorizedBankTransaction
|
||||
) => {
|
||||
if (transaction.categorized) {
|
||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_CATEGORIZED);
|
||||
}
|
||||
};
|
||||
|
||||
export const validateTransactionNotExcluded = (
|
||||
transaction: UncategorizedBankTransaction
|
||||
) => {
|
||||
if (transaction.isExcluded) {
|
||||
throw new ServiceError(ERRORS.TRANSACTION_ALREADY_EXCLUDED);
|
||||
}
|
||||
};
|
||||
|
||||
export const validateTransactionShouldBeExcluded = (
|
||||
transaction: UncategorizedBankTransaction
|
||||
) => {
|
||||
if (!transaction.isExcluded) {
|
||||
throw new ServiceError(ERRORS.TRANSACTION_NOT_EXCLUDED);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user