mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
refactor: migrate item categories module to nestjs
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Transformer } from '../Transformer/Transformer';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { flatToNestedArray } from '@/utils/flat-to-nested-array';
|
||||
import { assocDepthLevelToObjectTree } from '@/utils/assoc-depth-level-to-object-tree';
|
||||
import { nestedArrayToFlatten } from '@/utils/nested-array-to-flatten';
|
||||
@@ -35,7 +35,7 @@ export class AccountTransformer extends Transformer {
|
||||
* @param {IAccount} account -
|
||||
* @returns {string}
|
||||
*/
|
||||
public flattenName = (account: AccountModel): string => {
|
||||
public flattenName = (account: Account): string => {
|
||||
const parentDependantsIds = this.options.accountsGraph.dependantsOf(
|
||||
account.id,
|
||||
);
|
||||
@@ -51,7 +51,7 @@ export class AccountTransformer extends Transformer {
|
||||
* @param {IAccount} invoice
|
||||
* @returns {string}
|
||||
*/
|
||||
protected formattedAmount = (account: AccountModel): string => {
|
||||
protected formattedAmount = (account: Account): string => {
|
||||
return this.formatNumber(account.amount, {
|
||||
currencyCode: account.currencyCode,
|
||||
});
|
||||
@@ -59,10 +59,10 @@ export class AccountTransformer extends Transformer {
|
||||
|
||||
/**
|
||||
* Retrieves the formatted bank balance.
|
||||
* @param {AccountModel} account
|
||||
* @param {Account} account
|
||||
* @returns {string}
|
||||
*/
|
||||
protected bankBalanceFormatted = (account: AccountModel): string => {
|
||||
protected bankBalanceFormatted = (account: Account): string => {
|
||||
return this.formatNumber(account.bankBalance, {
|
||||
currencyCode: account.currencyCode,
|
||||
});
|
||||
@@ -73,7 +73,7 @@ export class AccountTransformer extends Transformer {
|
||||
* @param {IAccount} account
|
||||
* @returns {string}
|
||||
*/
|
||||
protected lastFeedsUpdatedAtFormatted = (account: AccountModel): string => {
|
||||
protected lastFeedsUpdatedAtFormatted = (account: Account): string => {
|
||||
return account.lastFeedsUpdatedAt
|
||||
? this.formatDate(account.lastFeedsUpdatedAt)
|
||||
: '';
|
||||
@@ -84,7 +84,7 @@ export class AccountTransformer extends Transformer {
|
||||
* @param account
|
||||
* @returns {boolean}
|
||||
*/
|
||||
protected isFeedsPaused = (account: AccountModel): boolean => {
|
||||
protected isFeedsPaused = (account: Account): boolean => {
|
||||
// return account.plaidItem?.isPaused || false;
|
||||
|
||||
return false;
|
||||
@@ -94,7 +94,7 @@ export class AccountTransformer extends Transformer {
|
||||
* Retrieves formatted account type label.
|
||||
* @returns {string}
|
||||
*/
|
||||
protected accountTypeLabel = (account: AccountModel): string => {
|
||||
protected accountTypeLabel = (account: Account): string => {
|
||||
return this.context.i18n.t(account.accountTypeLabel);
|
||||
};
|
||||
|
||||
@@ -102,7 +102,7 @@ export class AccountTransformer extends Transformer {
|
||||
* Retrieves formatted account normal.
|
||||
* @returns {string}
|
||||
*/
|
||||
protected accountNormalFormatted = (account: AccountModel): string => {
|
||||
protected accountNormalFormatted = (account: Account): string => {
|
||||
return this.context.i18n.t(account.accountNormalFormatted);
|
||||
};
|
||||
|
||||
@@ -111,7 +111,7 @@ export class AccountTransformer extends Transformer {
|
||||
* @param {IAccount[]}
|
||||
* @returns {IAccount[]}
|
||||
*/
|
||||
protected postCollectionTransform = (accounts: AccountModel[]) => {
|
||||
protected postCollectionTransform = (accounts: Account[]) => {
|
||||
// Transfom the flatten to accounts tree.
|
||||
const transformed = flatToNestedArray(accounts, {
|
||||
id: 'id',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Knex } from 'knex';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
// import { IDynamicListFilterDTO } from '@/interfaces/DynamicFilter';
|
||||
|
||||
export enum AccountNormal {
|
||||
@@ -42,26 +42,26 @@ export interface IAccountEventCreatingPayload {
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
export interface IAccountEventCreatedPayload {
|
||||
account: AccountModel;
|
||||
account: Account;
|
||||
accountId: number;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventEditedPayload {
|
||||
account: AccountModel;
|
||||
oldAccount: AccountModel;
|
||||
account: Account;
|
||||
oldAccount: Account;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventDeletedPayload {
|
||||
accountId: number;
|
||||
oldAccount: AccountModel;
|
||||
oldAccount: Account;
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
|
||||
export interface IAccountEventDeletePayload {
|
||||
trx: Knex.Transaction;
|
||||
oldAccount: AccountModel;
|
||||
oldAccount: Account;
|
||||
}
|
||||
|
||||
export interface IAccountEventActivatedPayload {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { CreateAccountService } from './CreateAccount.service';
|
||||
import { DeleteAccount } from './DeleteAccount.service';
|
||||
import { EditAccount } from './EditAccount.service';
|
||||
import { CreateAccountDTO } from './CreateAccount.dto';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { EditAccountDTO } from './EditAccount.dto';
|
||||
import { GetAccount } from './GetAccount.service';
|
||||
import { ActivateAccount } from './ActivateAccount.service';
|
||||
@@ -37,7 +37,7 @@ export class AccountsApplication {
|
||||
public createAccount = (
|
||||
accountDTO: CreateAccountDTO,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<AccountModel> => {
|
||||
): Promise<Account> => {
|
||||
return this.createAccountService.createAccount(accountDTO, trx);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { IAccountEventActivatedPayload } from './Accounts.types';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { AccountRepository } from './repositories/Account.repository';
|
||||
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
@@ -13,8 +13,8 @@ export class ActivateAccount {
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly uow: UnitOfWork,
|
||||
|
||||
@Inject(AccountModel.name)
|
||||
private readonly accountModel: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
) {}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Inject, Injectable, Scope } from '@nestjs/common';
|
||||
// import AccountTypesUtils from '@/lib/AccountTypes';
|
||||
import { ServiceError } from '../Items/ServiceError';
|
||||
import { ERRORS, MAX_ACCOUNTS_CHART_DEPTH } from './constants';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { AccountRepository } from './repositories/Account.repository';
|
||||
import { AccountTypesUtils } from './utils/AccountType.utils';
|
||||
import { CreateAccountDTO } from './CreateAccount.dto';
|
||||
@@ -13,16 +13,16 @@ import { EditAccountDTO } from './EditAccount.dto';
|
||||
@Injectable({ scope: Scope.REQUEST })
|
||||
export class CommandAccountValidators {
|
||||
constructor(
|
||||
@Inject(AccountModel.name)
|
||||
private readonly accountModel: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Throws error if the account was prefined.
|
||||
* @param {AccountModel} account
|
||||
* @param {Account} account
|
||||
*/
|
||||
public throwErrorIfAccountPredefined(account: AccountModel) {
|
||||
public throwErrorIfAccountPredefined(account: Account) {
|
||||
if (account.predefined) {
|
||||
throw new ServiceError(ERRORS.ACCOUNT_PREDEFINED);
|
||||
}
|
||||
@@ -31,12 +31,12 @@ export class CommandAccountValidators {
|
||||
/**
|
||||
* Diff account type between new and old account, throw service error
|
||||
* if they have different account type.
|
||||
* @param {AccountModel|CreateAccountDTO|EditAccountDTO} oldAccount
|
||||
* @param {AccountModel|CreateAccountDTO|EditAccountDTO} newAccount
|
||||
* @param {Account|CreateAccountDTO|EditAccountDTO} oldAccount
|
||||
* @param {Account|CreateAccountDTO|EditAccountDTO} newAccount
|
||||
*/
|
||||
public async isAccountTypeChangedOrThrowError(
|
||||
oldAccount: AccountModel | CreateAccountDTO | EditAccountDTO,
|
||||
newAccount: AccountModel | CreateAccountDTO | EditAccountDTO,
|
||||
oldAccount: Account | CreateAccountDTO | EditAccountDTO,
|
||||
newAccount: Account | CreateAccountDTO | EditAccountDTO,
|
||||
) {
|
||||
if (oldAccount.accountType !== newAccount.accountType) {
|
||||
throw new ServiceError(ERRORS.ACCOUNT_TYPE_NOT_ALLOWED_TO_CHANGE);
|
||||
@@ -155,13 +155,13 @@ export class CommandAccountValidators {
|
||||
* Validates the account DTO currency code whether equals the currency code of
|
||||
* parent account.
|
||||
* @param {CreateAccountDTO | EditAccountDTO} accountDTO
|
||||
* @param {AccountModel} parentAccount
|
||||
* @param {Account} parentAccount
|
||||
* @param {string} baseCurrency -
|
||||
* @throws {ServiceError(ERRORS.ACCOUNT_CURRENCY_NOT_SAME_PARENT_ACCOUNT)}
|
||||
*/
|
||||
public validateCurrentSameParentAccount = (
|
||||
accountDTO: CreateAccountDTO | EditAccountDTO,
|
||||
parentAccount: AccountModel,
|
||||
parentAccount: Account,
|
||||
baseCurrency: string,
|
||||
) => {
|
||||
// If the account DTO currency not assigned and the parent account has no base currency.
|
||||
@@ -187,7 +187,7 @@ export class CommandAccountValidators {
|
||||
*/
|
||||
public throwErrorIfParentHasDiffType(
|
||||
accountDTO: CreateAccountDTO | EditAccountDTO,
|
||||
parentAccount: AccountModel,
|
||||
parentAccount: Account,
|
||||
) {
|
||||
if (accountDTO.accountType !== parentAccount.accountType) {
|
||||
throw new ServiceError(ERRORS.PARENT_ACCOUNT_HAS_DIFFERENT_TYPE);
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
CreateAccountParams,
|
||||
} from './Accounts.types';
|
||||
import { CommandAccountValidators } from './CommandAccountValidators.service';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||
import { events } from '@/common/events/events';
|
||||
@@ -20,8 +20,8 @@ import { CreateAccountDTO } from './CreateAccount.dto';
|
||||
@Injectable()
|
||||
export class CreateAccountService {
|
||||
constructor(
|
||||
@Inject(AccountModel.name)
|
||||
private readonly accountModel: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly validator: CommandAccountValidators,
|
||||
@@ -102,7 +102,7 @@ export class CreateAccountService {
|
||||
accountDTO: CreateAccountDTO,
|
||||
trx?: Knex.Transaction,
|
||||
params: CreateAccountParams = { ignoreUniqueName: false },
|
||||
): Promise<AccountModel> => {
|
||||
): Promise<Account> => {
|
||||
// Retrieves the given tenant metadata.
|
||||
const tenant = await this.tenancyContext.getTenant(true);
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
// import { IAccountEventDeletedPayload } from '@/interfaces';
|
||||
import { CommandAccountValidators } from './CommandAccountValidators.service';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
@@ -11,7 +11,7 @@ import { IAccountEventDeletedPayload } from './Accounts.types';
|
||||
@Injectable()
|
||||
export class DeleteAccount {
|
||||
constructor(
|
||||
@Inject(AccountModel.name) private accountModel: typeof AccountModel,
|
||||
@Inject(Account.name) private accountModel: typeof Account,
|
||||
private eventEmitter: EventEmitter2,
|
||||
private uow: UnitOfWork,
|
||||
private validator: CommandAccountValidators,
|
||||
@@ -21,7 +21,7 @@ export class DeleteAccount {
|
||||
* Authorize account delete.
|
||||
* @param {number} accountId - Account id.
|
||||
*/
|
||||
private authorize = async (accountId: number, oldAccount: AccountModel) => {
|
||||
private authorize = async (accountId: number, oldAccount: Account) => {
|
||||
// Throw error if the account was predefined.
|
||||
this.validator.throwErrorIfAccountPredefined(oldAccount);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import { CommandAccountValidators } from './CommandAccountValidators.service';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
@@ -14,8 +14,8 @@ export class EditAccount {
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly validator: CommandAccountValidators,
|
||||
|
||||
@Inject(AccountModel.name)
|
||||
private readonly accountModel: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ export class EditAccount {
|
||||
private authorize = async (
|
||||
accountId: number,
|
||||
accountDTO: EditAccountDTO,
|
||||
oldAccount: AccountModel,
|
||||
oldAccount: Account,
|
||||
) => {
|
||||
// Validate account name uniquiness.
|
||||
await this.validator.validateAccountNameUniquiness(
|
||||
@@ -64,7 +64,7 @@ export class EditAccount {
|
||||
public async editAccount(
|
||||
accountId: number,
|
||||
accountDTO: EditAccountDTO,
|
||||
): Promise<AccountModel> {
|
||||
): Promise<Account> {
|
||||
// Retrieve the old account or throw not found service error.
|
||||
const oldAccount = await this.accountModel
|
||||
.query()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { AccountTransformer } from './Account.transformer';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { AccountRepository } from './repositories/Account.repository';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
@@ -9,8 +9,8 @@ import { events } from '@/common/events/events';
|
||||
@Injectable()
|
||||
export class GetAccount {
|
||||
constructor(
|
||||
@Inject(AccountModel.name)
|
||||
private readonly accountModel: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly accountModel: typeof Account,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
private readonly transformer: TransformerInjectable,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
} from './Accounts.types';
|
||||
import { AccountTransactionTransformer } from './AccountTransaction.transformer';
|
||||
import { AccountTransaction } from './models/AccountTransaction.model';
|
||||
import { AccountModel } from './models/Account.model';
|
||||
import { Account } from './models/Account.model';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
|
||||
@@ -16,8 +16,8 @@ export class GetAccountTransactionsService {
|
||||
@Inject(AccountTransaction.name)
|
||||
private readonly accountTransaction: typeof AccountTransaction,
|
||||
|
||||
@Inject(AccountModel.name)
|
||||
private readonly account: typeof AccountModel,
|
||||
@Inject(Account.name)
|
||||
private readonly account: typeof Account,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
// import { DynamicListService } from '../DynamicListing/DynamicListService';
|
||||
// import { AccountTransformer } from './Account.transformer';
|
||||
// import { TransformerInjectable } from '../Transformer/TransformerInjectable.service';
|
||||
// import { AccountModel } from './models/Account.model';
|
||||
// import { Account } from './models/Account.model';
|
||||
// import { AccountRepository } from './repositories/Account.repository';
|
||||
|
||||
// @Injectable()
|
||||
@@ -16,7 +16,7 @@
|
||||
// constructor(
|
||||
// private readonly dynamicListService: DynamicListService,
|
||||
// private readonly transformerService: TransformerInjectable,
|
||||
// private readonly accountModel: typeof AccountModel,
|
||||
// private readonly accountModel: typeof Account,
|
||||
// private readonly accountRepository: AccountRepository,
|
||||
// ) {}
|
||||
|
||||
|
||||
@@ -18,13 +18,13 @@ import { Model } from 'objection';
|
||||
// import { flatToNestedArray } from 'utils';
|
||||
|
||||
// @ts-expect-error
|
||||
// export class AccountModel extends mixin(TenantModel, [
|
||||
// export class Account extends mixin(TenantModel, [
|
||||
// ModelSettings,
|
||||
// CustomViewBaseModel,
|
||||
// SearchableModel,
|
||||
// ]) {
|
||||
|
||||
export class AccountModel extends TenantModel {
|
||||
export class Account extends TenantModel {
|
||||
name: string;
|
||||
slug: string;
|
||||
code: string;
|
||||
@@ -126,7 +126,7 @@ export class AccountModel extends TenantModel {
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
const TABLE_NAME = AccountModel.tableName;
|
||||
const TABLE_NAME = Account.tableName;
|
||||
|
||||
return {
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Knex } from 'knex';
|
||||
import { Inject, Injectable, Scope } from '@nestjs/common';
|
||||
import { TenantRepository } from '@/common/repository/TenantRepository';
|
||||
import { TENANCY_DB_CONNECTION } from '@/modules/Tenancy/TenancyDB/TenancyDB.constants';
|
||||
import { AccountModel } from '../models/Account.model';
|
||||
import { Account } from '../models/Account.model';
|
||||
// import { TenantMetadata } from '@/modules/System/models/TenantMetadataModel';
|
||||
// import { IAccount } from '../Accounts.types';
|
||||
// import {
|
||||
@@ -20,8 +20,8 @@ export class AccountRepository extends TenantRepository {
|
||||
/**
|
||||
* Gets the repository's model.
|
||||
*/
|
||||
get model(): typeof AccountModel {
|
||||
return AccountModel.bindKnex(this.tenantDBKnex);
|
||||
get model(): typeof Account {
|
||||
return Account.bindKnex(this.tenantDBKnex);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user