refactor: dynamic list to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-12 18:22:48 +02:00
parent ddaea20d16
commit 270b421a6c
117 changed files with 4232 additions and 1493 deletions

View File

@@ -1,4 +1,12 @@
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Query,
} from '@nestjs/common';
import { BankingTransactionsApplication } from './BankingTransactionsApplication.service';
import { ICashflowNewCommandDTO } from './types/BankingTransactions.types';
import { PublicRoute } from '../Auth/Jwt.guard';
@@ -10,6 +18,11 @@ export class BankingTransactionsController {
private readonly bankingTransactionsApplication: BankingTransactionsApplication,
) {}
@Get('')
async getBankAccounts(@Query() filterDTO: ICashflowAccountsFilter) {
return this.bankingTransactionsApplication.getBankAccounts(filterDTO);
}
@Post()
async createTransaction(@Body() transactionDTO: ICashflowNewCommandDTO) {
return this.bankingTransactionsApplication.createTransaction(

View File

@@ -4,6 +4,7 @@ import { CreateBankTransactionService } from './commands/CreateBankTransaction.s
import { GetBankTransactionService } from './queries/GetBankTransaction.service';
import { ICashflowNewCommandDTO } from './types/BankingTransactions.types';
import { Injectable } from '@nestjs/common';
import { GetBankAccountsService } from './queries/GetBankAccounts.service';
@Injectable()
export class BankingTransactionsApplication {
@@ -11,7 +12,7 @@ export class BankingTransactionsApplication {
private readonly createTransactionService: CreateBankTransactionService,
private readonly deleteTransactionService: DeleteCashflowTransaction,
private readonly getCashflowTransactionService: GetBankTransactionService,
// private readonly getCashflowAccountsService: GetBankingAccountsServic,
private readonly getBankAccountsService: GetBankAccountsService,
) {}
/**
@@ -48,11 +49,8 @@ export class BankingTransactionsApplication {
/**
* Retrieves the cashflow accounts.
* @param {ICashflowAccountsFilter} filterDTO
* @returns
*/
public getCashflowAccounts(
// filterDTO: ICashflowAccountsFilter,
) {
// return this.getCashflowAccountsService.getCashflowAccounts(filterDTO);
public getBankAccounts(filterDTO: ICashflowAccountsFilter) {
return this.getBankAccountsService.getBankAccounts(filterDTO);
}
}

View File

@@ -0,0 +1,139 @@
/* eslint-disable global-require */
import { mixin, Model } from 'objection';
import { castArray } from 'lodash';
import { BaseModel } from '@/models/Model';
import { AccountTypesUtils } from '@/libs/accounts-utils/AccountTypesUtils';
import { PlaidItem } from '@/modules/BankingPlaid/models/PlaidItem';
export class BankAccount extends BaseModel {
public name!: string;
public slug!: string;
public code!: string;
public index!: number;
public accountType!: string;
public predefined!: boolean;
public currencyCode!: string;
public active!: boolean;
public bankBalance!: number;
public lastFeedsUpdatedAt!: string | null;
public amount!: number;
public plaidItemId!: number;
public plaidItem!: PlaidItem;
/**
* Table name.
*/
static get tableName() {
return 'accounts';
}
/**
* Timestamps columns.
*/
static get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return ['accountTypeLabel'];
}
/**
* Retrieve account type label.
*/
get accountTypeLabel() {
return AccountTypesUtils.getType(this.accountType, 'label');
}
/**
* Allows to mark model as resourceable to viewable and filterable.
*/
static get resourceable() {
return true;
}
/**
* Model modifiers.
*/
static get modifiers() {
return {
/**
* Inactive/Active mode.
*/
inactiveMode(query, active = false) {
query.where('accounts.active', !active);
},
};
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const AccountTransaction = require('models/AccountTransaction');
return {
/**
* Account model may has many transactions.
*/
transactions: {
relation: Model.HasManyRelation,
modelClass: AccountTransaction.default,
join: {
from: 'accounts.id',
to: 'accounts_transactions.accountId',
},
},
};
}
/**
* Detarmines whether the given type equals the account type.
* @param {string} accountType
* @return {boolean}
*/
isAccountType(accountType) {
const types = castArray(accountType);
return types.indexOf(this.accountType) !== -1;
}
/**
* Detarmine whether the given parent type equals the account type.
* @param {string} parentType
* @return {boolean}
*/
isParentType(parentType) {
return AccountTypesUtils.isParentTypeEqualsKey(
this.accountType,
parentType
);
}
// /**
// * Model settings.
// */
// static get meta() {
// return CashflowAccountSettings;
// }
// /**
// * Retrieve the default custom views, roles and columns.
// */
// static get defaultViews() {
// return DEFAULT_VIEWS;
// }
/**
* Model search roles.
*/
static get searchRoles() {
return [
{ condition: 'or', fieldKey: 'name', comparator: 'contains' },
{ condition: 'or', fieldKey: 'code', comparator: 'like' },
];
}
}

View File

@@ -1,61 +1,52 @@
// import { Service, Inject } from 'typedi';
// import { ICashflowAccount, ICashflowAccountsFilter } from '@/interfaces';
// import { CashflowAccountTransformer } from './queries/BankAccountTransformer';
// import TenancyService from '@/services/Tenancy/TenancyService';
// import DynamicListingService from '@/services/DynamicListing/DynamicListService';
// import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
// import { ACCOUNT_TYPE } from '@/data/AccountTypes';
import { Injectable, Inject } from '@nestjs/common';
import { BankAccount } from '../models/BankAccount';
import { DynamicListService } from '@/modules/DynamicListing/DynamicList.service';
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { CashflowAccountTransformer } from './BankAccountTransformer';
import { ACCOUNT_TYPE } from '@/constants/accounts';
// @Service()
// export default class GetCashflowAccountsService {
// @Inject()
// private tenancy: TenancyService;
@Injectable()
export class GetBankAccountsService {
constructor(
private readonly dynamicListService: DynamicListService,
private readonly transformer: TransformerInjectable,
// @Inject()
// private dynamicListService: DynamicListingService;
@Inject(BankAccount.name)
private readonly bankAccountModel: typeof BankAccount
) {}
// @Inject()
// private transformer: TransformerInjectable;
/**
* Retrieve the cash flow accounts.
* @param {ICashflowAccountsFilter} filterDTO - Filter DTO.
* @returns {ICashflowAccount[]}
*/
public async getBankAccounts(
filterDTO: ICashflowAccountsFilter,
): Promise<BankAccount[]> {
// Parsees accounts list filter DTO.
const filter = this.dynamicListService.parseStringifiedFilter(filterDTO);
// /**
// * Retrieve the cash flow accounts.
// * @param {number} tenantId - Tenant id.
// * @param {ICashflowAccountsFilter} filterDTO - Filter DTO.
// * @returns {ICashflowAccount[]}
// */
// public async getCashflowAccounts(
// tenantId: number,
// filterDTO: ICashflowAccountsFilter
// ): Promise<{ cashflowAccounts: ICashflowAccount[] }> {
// const { CashflowAccount } = this.tenancy.models(tenantId);
// Dynamic list service.
const dynamicList = await this.dynamicListService.dynamicList(
BankAccount,
filter,
);
// Retrieve accounts model based on the given query.
const accounts = await this.bankAccountModel.query().onBuild((builder) => {
dynamicList.buildQuery()(builder);
// // Parsees accounts list filter DTO.
// const filter = this.dynamicListService.parseStringifiedFilter(filterDTO);
// // Dynamic list service.
// const dynamicList = await this.dynamicListService.dynamicList(
// tenantId,
// CashflowAccount,
// filter
// );
// // Retrieve accounts model based on the given query.
// const accounts = await CashflowAccount.query().onBuild((builder) => {
// dynamicList.buildQuery()(builder);
// builder.whereIn('account_type', [
// ACCOUNT_TYPE.BANK,
// ACCOUNT_TYPE.CASH,
// ACCOUNT_TYPE.CREDIT_CARD,
// ]);
// builder.modify('inactiveMode', filter.inactiveMode);
// });
// // Retrieves the transformed accounts.
// const transformed = await this.transformer.transform(
// tenantId,
// accounts,
// new CashflowAccountTransformer()
// );
// return transformed;
// }
// }
builder.whereIn('account_type', [
ACCOUNT_TYPE.BANK,
ACCOUNT_TYPE.CASH,
ACCOUNT_TYPE.CREDIT_CARD,
]);
builder.modify('inactiveMode', filter.inactiveMode);
});
// Retrieves the transformed accounts.
const transformed = await this.transformer.transform(
accounts,
new CashflowAccountTransformer(),
);
return transformed;
}
}