mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
feat: excluded bank transactions
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { param } from 'express-validator';
|
||||
import { NextFunction, Request, Response, Router } from 'express';
|
||||
import { NextFunction, Request, Response, Router, query } from 'express';
|
||||
import BaseController from '../BaseController';
|
||||
import { ExcludeBankTransactionsApplication } from '@/services/Banking/Exclude/ExcludeBankTransactionsApplication';
|
||||
|
||||
@@ -27,6 +27,12 @@ export class ExcludeBankTransactionsController extends BaseController {
|
||||
this.validationResult,
|
||||
this.unexcludeBankTransaction.bind(this)
|
||||
);
|
||||
router.get(
|
||||
'/excluded',
|
||||
[],
|
||||
this.validationResult,
|
||||
this.getExcludedBankTransactions.bind(this)
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -87,4 +93,32 @@ export class ExcludeBankTransactionsController extends BaseController {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the excluded uncategorized bank transactions.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
* @returns {Promise<Response|null>}
|
||||
*/
|
||||
private async getExcludedBankTransactions(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<Response | void> {
|
||||
const { tenantId } = req;
|
||||
const filter = this.matchedBodyData(req);
|
||||
|
||||
console.log('123');
|
||||
try {
|
||||
const data =
|
||||
await this.excludeBankTransactionApp.getExcludedBankTransactions(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
return res.status(200).send(data);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,14 +14,11 @@ export class RecognizedTransactionsController extends BaseController {
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.get(
|
||||
'/accounts/:accountId',
|
||||
this.getRecognizedTransactions.bind(this)
|
||||
);
|
||||
router.get('/', this.getRecognizedTransactions.bind(this));
|
||||
|
||||
return router;
|
||||
}
|
||||
k;
|
||||
|
||||
/**
|
||||
* Retrieves the recognized bank transactions.
|
||||
* @param {Request} req
|
||||
@@ -34,15 +31,15 @@ export class RecognizedTransactionsController extends BaseController {
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const { accountId } = req.params;
|
||||
const filter = this.matchedQueryData(req);
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const data = await this.cashflowApplication.getRecognizedTransactions(
|
||||
tenantId,
|
||||
accountId
|
||||
filter
|
||||
);
|
||||
return res.status(200).send({ data });
|
||||
return res.status(200).send(data);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -164,3 +164,10 @@ export interface IGetUncategorizedTransactionsQuery {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
|
||||
export interface IGetRecognizedTransactionsQuery {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
accountId?: number;
|
||||
}
|
||||
@@ -29,6 +29,7 @@ export class RecognizedBankTransaction extends TenantModel {
|
||||
static get relationMappings() {
|
||||
const UncategorizedCashflowTransaction = require('./UncategorizedCashflowTransaction');
|
||||
const Account = require('./Account');
|
||||
const { BankRule } = require('./BankRule');
|
||||
|
||||
return {
|
||||
/**
|
||||
@@ -54,6 +55,18 @@ export class RecognizedBankTransaction extends TenantModel {
|
||||
to: 'accounts.id',
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
* Recognized bank transaction may belongs to bank rule.
|
||||
*/
|
||||
bankRule: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: BankRule,
|
||||
join: {
|
||||
from: 'recognized_bank_transactions.bankRuleId',
|
||||
to: 'bank_rules.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { ExcludeBankTransaction } from './ExcludeBankTransaction';
|
||||
import { UnexcludeBankTransaction } from './UnexcludeBankTransaction';
|
||||
import { GetExcludedBankTransactionsService } from './GetExcludedBankTransactions';
|
||||
import { ExcludedBankTransactionsQuery } from './_types';
|
||||
|
||||
@Service()
|
||||
export class ExcludeBankTransactionsApplication {
|
||||
@@ -10,6 +12,9 @@ export class ExcludeBankTransactionsApplication {
|
||||
@Inject()
|
||||
private unexcludeBankTransactionService: UnexcludeBankTransaction;
|
||||
|
||||
@Inject()
|
||||
private getExcludedBankTransactionsService: GetExcludedBankTransactionsService;
|
||||
|
||||
/**
|
||||
* Marks a bank transaction as excluded.
|
||||
* @param {number} tenantId - The ID of the tenant.
|
||||
@@ -35,4 +40,20 @@ export class ExcludeBankTransactionsApplication {
|
||||
bankTransactionId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the excluded bank transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {ExcludedBankTransactionsQuery} filter
|
||||
* @returns {}
|
||||
*/
|
||||
public getExcludedBankTransactions(
|
||||
tenantId: number,
|
||||
filter: ExcludedBankTransactionsQuery
|
||||
) {
|
||||
return this.getExcludedBankTransactionsService.getExcludedBankTransactions(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { ExcludedBankTransactionsQuery } from './_types';
|
||||
import { UncategorizedTransactionTransformer } from '@/services/Cashflow/UncategorizedTransactionTransformer';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
|
||||
@Service()
|
||||
export class GetExcludedBankTransactionsService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private transformer: TransformerInjectable;
|
||||
|
||||
/**
|
||||
* Retrieves the excluded uncategorized bank transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {ExcludedBankTransactionsQuery} filter
|
||||
* @returns
|
||||
*/
|
||||
public async getExcludedBankTransactions(
|
||||
tenantId: number,
|
||||
filter: ExcludedBankTransactionsQuery
|
||||
) {
|
||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
// Parsed query with default values.
|
||||
const _query = {
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
...filter,
|
||||
};
|
||||
const { results, pagination } =
|
||||
await UncategorizedCashflowTransaction.query()
|
||||
.onBuild((q) => {
|
||||
q.where('excluded', true);
|
||||
q.orderBy('date', 'DESC');
|
||||
|
||||
if (_query.accountId) {
|
||||
q.where('account_id', _query.accountId);
|
||||
}
|
||||
})
|
||||
.pagination(_query.page - 1, _query.pageSize);
|
||||
|
||||
const data = await this.transformer.transform(
|
||||
tenantId,
|
||||
results,
|
||||
new UncategorizedTransactionTransformer()
|
||||
);
|
||||
return { data, pagination };
|
||||
}
|
||||
}
|
||||
6
packages/server/src/services/Banking/Exclude/_types.ts
Normal file
6
packages/server/src/services/Banking/Exclude/_types.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
export interface ExcludedBankTransactionsQuery {
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
accountId?: number;
|
||||
}
|
||||
@@ -65,7 +65,6 @@ export class RecognizeTranasctionsService {
|
||||
|
||||
if (batch) query.where('batch', batch);
|
||||
});
|
||||
|
||||
const bankRules = await BankRule.query().withGraphFetched('conditions');
|
||||
const bankRulesByAccountId = transformToMapBy(
|
||||
bankRules,
|
||||
@@ -92,7 +91,7 @@ export class RecognizeTranasctionsService {
|
||||
);
|
||||
}
|
||||
};
|
||||
await PromisePool.withConcurrency(MIGRATION_CONCURRENCY)
|
||||
const result = await PromisePool.withConcurrency(MIGRATION_CONCURRENCY)
|
||||
.for(uncategorizedTranasctions)
|
||||
.process((transaction: UncategorizedCashflowTransaction, index, pool) => {
|
||||
return regonizeTransaction(transaction);
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
ICashflowAccountsFilter,
|
||||
ICashflowNewCommandDTO,
|
||||
ICategorizeCashflowTransactioDTO,
|
||||
IGetRecognizedTransactionsQuery,
|
||||
IGetUncategorizedTransactionsQuery,
|
||||
} from '@/interfaces';
|
||||
import { CategorizeTransactionAsExpense } from './CategorizeTransactionAsExpense';
|
||||
@@ -18,6 +19,7 @@ import { GetUncategorizedTransaction } from './GetUncategorizedTransaction';
|
||||
import NewCashflowTransactionService from './NewCashflowTransactionService';
|
||||
import GetCashflowAccountsService from './GetCashflowAccountsService';
|
||||
import { GetCashflowTransactionService } from './GetCashflowTransactionsService';
|
||||
import { GetRecognizedTransactionsService } from './GetRecongizedTransactions';
|
||||
|
||||
@Service()
|
||||
export class CashflowApplication {
|
||||
@@ -51,6 +53,9 @@ export class CashflowApplication {
|
||||
@Inject()
|
||||
private createUncategorizedTransactionService: CreateUncategorizedTransaction;
|
||||
|
||||
@Inject()
|
||||
private getRecognizedTranasctionsService: GetRecognizedTransactionsService;
|
||||
|
||||
/**
|
||||
* Creates a new cashflow transaction.
|
||||
* @param {number} tenantId
|
||||
@@ -213,4 +218,20 @@ export class CashflowApplication {
|
||||
uncategorizedTransactionId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the recognized bank transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {number} accountId
|
||||
* @returns
|
||||
*/
|
||||
public getRecognizedTransactions(
|
||||
tenantId: number,
|
||||
filter?: IGetRecognizedTransactionsQuery
|
||||
) {
|
||||
return this.getRecognizedTranasctionsService.getRecognizedTranactions(
|
||||
tenantId,
|
||||
filter
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
import { Transformer } from '@/lib/Transformer/Transformer';
|
||||
import { formatNumber } from '@/utils';
|
||||
|
||||
export class GetRecognizedTransactionTransformer extends Transformer {
|
||||
/**
|
||||
* Include these attributes to sale credit note object.
|
||||
* @returns {Array}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [
|
||||
'uncategorizedTransactionId',
|
||||
'referenceNo',
|
||||
'description',
|
||||
'payee',
|
||||
'amount',
|
||||
'formattedAmount',
|
||||
'date',
|
||||
'formattedDate',
|
||||
'assignedAccountId',
|
||||
'assignedAccountName',
|
||||
'assignedAccountCode',
|
||||
'assignedPayee',
|
||||
'assignedMemo',
|
||||
'assignedCategory',
|
||||
'assignedCategoryFormatted',
|
||||
'withdrawal',
|
||||
'deposit',
|
||||
'isDepositTransaction',
|
||||
'isWithdrawalTransaction',
|
||||
'formattedDepositAmount',
|
||||
'formattedWithdrawalAmount',
|
||||
'bankRuleId',
|
||||
'bankRuleName',
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Exclude all attributes.
|
||||
* @returns {Array<string>}
|
||||
*/
|
||||
public excludeAttributes = (): string[] => {
|
||||
return ['*'];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the uncategorized transaction id.
|
||||
* @param transaction
|
||||
* @returns {number}
|
||||
*/
|
||||
public uncategorizedTransactionId = (transaction): number => {
|
||||
return transaction.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reference number of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public referenceNo(transaction: any): string {
|
||||
return transaction.referenceNo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the description of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public description(transaction: any): string {
|
||||
return transaction.description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the payee of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public payee(transaction: any): string {
|
||||
return transaction.payee;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {number}
|
||||
*/
|
||||
public amount(transaction: any): number {
|
||||
return transaction.amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted amount of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public formattedAmount(transaction: any): string {
|
||||
return this.formatNumber(transaction.formattedAmount, {
|
||||
money: true,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public date(transaction: any): string {
|
||||
return transaction.date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the formatted date of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public formattedDate(transaction: any): string {
|
||||
return this.formatDate(transaction.date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned account ID of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {number}
|
||||
*/
|
||||
public assignedAccountId(transaction: any): number {
|
||||
return transaction.recognizedTransaction.assignedAccountId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned account name of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public assignedAccountName(transaction: any): string {
|
||||
return transaction.recognizedTransaction.assignAccount.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned account code of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public assignedAccountCode(transaction: any): string {
|
||||
return transaction.recognizedTransaction.assignAccount.code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned payee of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public getAssignedPayee(transaction: any): string {
|
||||
return transaction.recognizedTransaction.assignedPayee;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned memo of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public assignedMemo(transaction: any): string {
|
||||
return transaction.recognizedTransaction.assignedMemo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the assigned category of the transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
public assignedCategory(transaction: any): string {
|
||||
return transaction.recognizedTransaction.assignedCategory;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
public assignedCategoryFormatted() {
|
||||
return 'Other Income'
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the transaction is a withdrawal.
|
||||
* @param {object} transaction
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isWithdrawal(transaction: any): boolean {
|
||||
return transaction.withdrawal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the transaction is a deposit.
|
||||
* @param {object} transaction
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isDeposit(transaction: any): boolean {
|
||||
return transaction.deposit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the transaction is a deposit transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isDepositTransaction(transaction: any): boolean {
|
||||
return transaction.isDepositTransaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the transaction is a withdrawal transaction.
|
||||
* @param {object} transaction
|
||||
* @returns {boolean}
|
||||
*/
|
||||
public isWithdrawalTransaction(transaction: any): boolean {
|
||||
return transaction.isWithdrawalTransaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted deposit amount.
|
||||
* @param {any} transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedDepositAmount(transaction) {
|
||||
if (transaction.isDepositTransaction) {
|
||||
return formatNumber(transaction.deposit, {
|
||||
currencyCode: transaction.currencyCode,
|
||||
});
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get formatted withdrawal amount.
|
||||
* @param transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedWithdrawalAmount(transaction) {
|
||||
if (transaction.isWithdrawalTransaction) {
|
||||
return formatNumber(transaction.withdrawal, {
|
||||
currencyCode: transaction.currencyCode,
|
||||
});
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transaction bank rule id.
|
||||
* @param transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
protected bankRuleId(transaction) {
|
||||
return transaction.recognizedTransaction.bankRuleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the transaction bank rule name.
|
||||
* @param transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
protected bankRuleName(transaction) {
|
||||
return transaction.recognizedTransaction.bankRule.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import HasTenancyService from '../Tenancy/TenancyService';
|
||||
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
|
||||
import { GetRecognizedTransactionTransformer } from './GetRecognizedTransactionTransformer';
|
||||
import { IGetRecognizedTransactionsQuery } from '@/interfaces';
|
||||
|
||||
@Service()
|
||||
export class GetRecognizedTransactionsService {
|
||||
@Inject()
|
||||
private tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
private transformer: TransformerInjectable;
|
||||
|
||||
/**
|
||||
* Retrieves the recognized transactions of the given account.
|
||||
* @param {number} tenantId
|
||||
* @param {IGetRecognizedTransactionsQuery} filter -
|
||||
*/
|
||||
async getRecognizedTranactions(
|
||||
tenantId: number,
|
||||
filter?: IGetRecognizedTransactionsQuery
|
||||
) {
|
||||
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
|
||||
|
||||
const _filter = {
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
...filter,
|
||||
};
|
||||
const { results, pagination } =
|
||||
await UncategorizedCashflowTransaction.query()
|
||||
.onBuild((q) => {
|
||||
q.withGraphFetched('recognizedTransaction.assignAccount');
|
||||
q.withGraphFetched('recognizedTransaction.bankRule');
|
||||
q.whereNotNull('recognizedTransactionId');
|
||||
|
||||
if (_filter.accountId) {
|
||||
q.where('accountId', _filter.accountId);
|
||||
}
|
||||
})
|
||||
.pagination(_filter.page - 1, _filter.pageSize);
|
||||
|
||||
const data = await this.transformer.transform(
|
||||
tenantId,
|
||||
results,
|
||||
new GetRecognizedTransactionTransformer()
|
||||
);
|
||||
return { data, pagination };
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
||||
return [
|
||||
'formattedAmount',
|
||||
'formattedDate',
|
||||
'formattetDepositAmount',
|
||||
'formattedDepositAmount',
|
||||
'formattedWithdrawalAmount',
|
||||
];
|
||||
};
|
||||
@@ -40,7 +40,7 @@ export class UncategorizedTransactionTransformer extends Transformer {
|
||||
* @param transaction
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattetDepositAmount(transaction) {
|
||||
protected formattedDepositAmount(transaction) {
|
||||
if (transaction.isDepositTransaction) {
|
||||
return formatNumber(transaction.deposit, {
|
||||
currencyCode: transaction.currencyCode,
|
||||
|
||||
Reference in New Issue
Block a user