feat(server): move all cashflow under application service

This commit is contained in:
Ahmed Bouhuolia
2024-03-07 14:19:11 +02:00
parent d87d674aba
commit 62d3e386dd
8 changed files with 138 additions and 49 deletions

View File

@@ -5,19 +5,33 @@ import { CategorizeCashflowTransaction } from './CategorizeCashflowTransaction';
import {
CategorizeTransactionAsExpenseDTO,
CreateUncategorizedTransactionDTO,
ICashflowAccountsFilter,
ICashflowNewCommandDTO,
ICategorizeCashflowTransactioDTO,
IUncategorizedCashflowTransaction,
IGetUncategorizedTransactionsQuery,
} from '@/interfaces';
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
import { GetUncategorizedTransactions } from './GetUncategorizedTransactions';
import { CreateUncategorizedTransaction } from './CreateUncategorizedTransaction';
import { GetUncategorizedTransaction } from './GetUncategorizedTransaction';
import NewCashflowTransactionService from './NewCashflowTransactionService';
import GetCashflowAccountsService from './GetCashflowAccountsService';
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
@Service()
export class CashflowApplication {
@Inject()
private createTransactionService: NewCashflowTransactionService;
@Inject()
private deleteTransactionService: DeleteCashflowTransaction;
@Inject()
private getCashflowAccountsService: GetCashflowAccountsService;
@Inject()
private getCashflowTransactionService: GetCashflowTransactionService;
@Inject()
private uncategorizeTransactionService: UncategorizeCashflowTransaction;
@@ -36,6 +50,25 @@ export class CashflowApplication {
@Inject()
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
/**
* Creates a new cashflow transaction.
* @param {number} tenantId
* @param {ICashflowNewCommandDTO} transactionDTO
* @param {number} userId
* @returns
*/
public createTransaction(
tenantId: number,
transactionDTO: ICashflowNewCommandDTO,
userId?: number
) {
return this.createTransactionService.newCashflowTransaction(
tenantId,
transactionDTO,
userId
);
}
/**
* Deletes the given cashflow transaction.
* @param {number} tenantId
@@ -49,6 +82,35 @@ export class CashflowApplication {
);
}
/**
* Retrieves specific cashflow transaction.
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @returns
*/
public getTransaction(tenantId: number, cashflowTransactionId: number) {
return this.getCashflowTransactionService.getCashflowTransaction(
tenantId,
cashflowTransactionId
);
}
/**
* Retrieves the cashflow accounts.
* @param {number} tenantId
* @param {ICashflowAccountsFilter} filterDTO
* @returns
*/
public getCashflowAccounts(
tenantId: number,
filterDTO: ICashflowAccountsFilter
) {
return this.getCashflowAccountsService.getCashflowAccounts(
tenantId,
filterDTO
);
}
/**
* Creates a new uncategorized cash transaction.
* @param {number} tenantId
@@ -105,7 +167,6 @@ export class CashflowApplication {
* @param {number} tenantId
* @param {number} cashflowTransactionId
* @param {CategorizeTransactionAsExpenseDTO} transactionDTO
* @returns
*/
public categorizeAsExpense(
tenantId: number,
@@ -122,20 +183,23 @@ export class CashflowApplication {
/**
* Retrieves the uncategorized cashflow transactions.
* @param {number} tenantId
* @returns {}
*/
public getUncategorizedTransactions(tenantId: number, accountId: number) {
public getUncategorizedTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
return this.getUncategorizedTransactionsService.getTransactions(
tenantId,
accountId
accountId,
query
);
}
/**
*
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
* @returns
* Retrieves specific uncategorized transaction.
* @param {number} tenantId
* @param {number} uncategorizedTransactionId
*/
public getUncategorizedTransaction(
tenantId: number,

View File

@@ -7,7 +7,7 @@ import { ServiceError } from '@/exceptions';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
@Service()
export default class GetCashflowTransactionsService {
export class GetCashflowTransactionService {
@Inject()
private tenancy: HasTenancyService;

View File

@@ -2,6 +2,7 @@ import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UncategorizedTransactionTransformer } from './UncategorizedTransactionTransformer';
import { IGetUncategorizedTransactionsQuery } from '@/interfaces';
@Service()
export class GetUncategorizedTransactions {
@@ -16,16 +17,26 @@ export class GetUncategorizedTransactions {
* @param {number} tenantId - Tenant id.
* @param {number} accountId - Account Id.
*/
public async getTransactions(tenantId: number, accountId: number) {
public async getTransactions(
tenantId: number,
accountId: number,
query: IGetUncategorizedTransactionsQuery
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
// Parsed query with default values.
const _query = {
page: 1,
pageSize: 20,
...query,
};
const { results, pagination } =
await UncategorizedCashflowTransaction.query()
.where('accountId', accountId)
.where('categorized', false)
.withGraphFetched('account')
.orderBy('date', 'DESC')
.pagination(0, 1000);
.pagination(_query.page - 1, _query.pageSize);
const data = await this.transformer.transform(
tenantId,