From 83fbb7225d2d95e2526541b10463530e6ce1af01 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Thu, 7 Mar 2024 20:58:44 +0200 Subject: [PATCH] feat: remove uncategorized transaction from expenses --- .../Cashflow/GetCashflowAccounts.ts | 4 +- .../Cashflow/GetCashflowTransaction.ts | 2 - ...6_add_categorized_transaction_id_column.js | 11 --- packages/server/src/models/Expense.ts | 13 --- .../UncategorizedCashflowTransaction.ts | 80 ++++++++++++------- .../src/services/Banking/Plaid/PlaidSyncDB.ts | 4 - .../Cashflow/UncategorizeTransactionByRef.ts | 9 --- .../AccountTransactionsList.tsx | 1 - .../drawers/CategorizeTransactionDrawer.tsx | 33 -------- .../CategorizeTransactionForm.tsx | 1 - .../CategorizeTransactionFormContent.tsx | 2 +- .../src/hooks/query/cashflowAccounts.tsx | 3 +- 12 files changed, 53 insertions(+), 110 deletions(-) delete mode 100644 packages/server/src/database/migrations/20240228224316_add_categorized_transaction_id_column.js delete mode 100644 packages/server/src/services/Cashflow/UncategorizeTransactionByRef.ts delete mode 100644 packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer.tsx diff --git a/packages/server/src/api/controllers/Cashflow/GetCashflowAccounts.ts b/packages/server/src/api/controllers/Cashflow/GetCashflowAccounts.ts index d1bc97e0a..559a5f4f2 100644 --- a/packages/server/src/api/controllers/Cashflow/GetCashflowAccounts.ts +++ b/packages/server/src/api/controllers/Cashflow/GetCashflowAccounts.ts @@ -1,9 +1,7 @@ import { Service, Inject } from 'typedi'; import { Router, Request, Response, NextFunction } from 'express'; -import { param, query } from 'express-validator'; -import GetCashflowAccountsService from '@/services/Cashflow/GetCashflowAccountsService'; +import { query } from 'express-validator'; import BaseController from '../BaseController'; -import GetCashflowTransactionsService from '@/services/Cashflow/GetCashflowTransactionsService'; import { ServiceError } from '@/exceptions'; import CheckPolicies from '@/api/middleware/CheckPolicies'; import { AbilitySubject, CashflowAction } from '@/interfaces'; diff --git a/packages/server/src/api/controllers/Cashflow/GetCashflowTransaction.ts b/packages/server/src/api/controllers/Cashflow/GetCashflowTransaction.ts index 9e7169859..2625a1cb9 100644 --- a/packages/server/src/api/controllers/Cashflow/GetCashflowTransaction.ts +++ b/packages/server/src/api/controllers/Cashflow/GetCashflowTransaction.ts @@ -2,7 +2,6 @@ import { Service, Inject } from 'typedi'; import { Router, Request, Response, NextFunction } from 'express'; import { param } from 'express-validator'; import BaseController from '../BaseController'; -import GetCashflowTransactionsService from '@/services/Cashflow/GetCashflowTransactionsService'; import { ServiceError } from '@/exceptions'; import CheckPolicies from '@/api/middleware/CheckPolicies'; import { AbilitySubject, CashflowAction } from '@/interfaces'; @@ -47,7 +46,6 @@ export default class GetCashflowAccounts extends BaseController { const cashflowTransaction = await this.cashflowApplication.getTransaction( tenantId, transactionId - ); return res.status(200).send({ diff --git a/packages/server/src/database/migrations/20240228224316_add_categorized_transaction_id_column.js b/packages/server/src/database/migrations/20240228224316_add_categorized_transaction_id_column.js deleted file mode 100644 index 749cc53b6..000000000 --- a/packages/server/src/database/migrations/20240228224316_add_categorized_transaction_id_column.js +++ /dev/null @@ -1,11 +0,0 @@ -exports.up = function (knex) { - return knex.schema.table('expenses_transactions', (table) => { - table - .integer('categorized_transaction_id') - .unsigned() - .references('id') - .inTable('uncategorized_cashflow_transactions'); - }); -}; - -exports.down = function (knex) {}; diff --git a/packages/server/src/models/Expense.ts b/packages/server/src/models/Expense.ts index 967c9c734..b2fce9a65 100644 --- a/packages/server/src/models/Expense.ts +++ b/packages/server/src/models/Expense.ts @@ -182,7 +182,6 @@ export default class Expense extends mixin(TenantModel, [ const ExpenseCategory = require('models/ExpenseCategory'); const Media = require('models/Media'); const Branch = require('models/Branch'); - const UncategorizedCashflowTransaction = require('models/UncategorizedCashflowTransaction'); return { paymentAccount: { @@ -235,18 +234,6 @@ export default class Expense extends mixin(TenantModel, [ query.where('model_name', 'Expense'); }, }, - - /** - * Retrieves the related uncategorized cashflow transaction. - */ - categorized: { - relation: Model.BelongsToOneRelation, - modelClass: UncategorizedCashflowTransaction.default, - join: { - from: 'expenses_transactions.categorizedTransactionId', - to: 'uncategorized_cashflow_transactions.id', - }, - } }; } diff --git a/packages/server/src/models/UncategorizedCashflowTransaction.ts b/packages/server/src/models/UncategorizedCashflowTransaction.ts index cb5ebfeef..928db9a4d 100644 --- a/packages/server/src/models/UncategorizedCashflowTransaction.ts +++ b/packages/server/src/models/UncategorizedCashflowTransaction.ts @@ -4,7 +4,11 @@ import { Model, ModelOptions, QueryContext } from 'objection'; import Account from './Account'; export default class UncategorizedCashflowTransaction extends TenantModel { - amount: number; + id!: number; + amount!: number; + categorized!: boolean; + accountId!: number; + /** * Table name. */ @@ -19,6 +23,18 @@ export default class UncategorizedCashflowTransaction extends TenantModel { return ['createdAt', 'updatedAt']; } + /** + * Virtual attributes. + */ + static get virtualAttributes() { + return [ + 'withdrawal', + 'deposit', + 'isDepositTransaction', + 'isWithdrawalTransaction', + ]; + } + /** * Retrieves the withdrawal amount. * @returns {number} @@ -49,18 +65,6 @@ export default class UncategorizedCashflowTransaction extends TenantModel { return 0 < this.withdrawal; } - /** - * Virtual attributes. - */ - static get virtualAttributes() { - return [ - 'withdrawal', - 'deposit', - 'isDepositTransaction', - 'isWithdrawalTransaction', - ]; - } - /** * Relationship mapping. */ @@ -83,40 +87,54 @@ export default class UncategorizedCashflowTransaction extends TenantModel { } /** - * - * @param queryContext + * Updates the count of uncategorized transactions for the associated account + * based on the specified operation. + * @param {QueryContext} queryContext - The query context for the transaction. + * @param {boolean} increment - Indicates whether to increment or decrement the count. + */ + private async updateUncategorizedTransactionCount( + queryContext: QueryContext, + increment: boolean + ) { + const operation = increment ? 'increment' : 'decrement'; + const amount = increment ? 1 : -1; + + await Account.query(queryContext.transaction) + .findById(this.accountId) + [operation]('uncategorized_transactions', amount); + } + + /** + * Runs after insert. + * @param {QueryContext} queryContext */ public async $afterInsert(queryContext) { await super.$afterInsert(queryContext); - - // Increments the uncategorized transactions count of the associated account. - await Account.query(queryContext.transaction) - .findById(this.accountId) - .increment('uncategorized_transactions', 1); + await this.updateUncategorizedTransactionCount(queryContext, true); } + /** + * Runs after update. + * @param {ModelOptions} opt + * @param {QueryContext} queryContext + */ public async $afterUpdate( opt: ModelOptions, queryContext: QueryContext - ): void | Promise { + ): Promise { await super.$afterUpdate(opt, queryContext); if (this.id && this.categorized) { - await Account.query(queryContext.transaction) - .findById(this.accountId) - .decrement('uncategorized_transactions', 1); + await this.updateUncategorizedTransactionCount(queryContext, false); } } /** - * - * @param queryContext + * Runs after delete. + * @param {QueryContext} queryContext */ - public async $afterDelete(queryContext) { + public async $afterDelete(queryContext: QueryContext) { await super.$afterDelete(queryContext); - - await Account.query(queryContext.transaction) - .findById(this.accountId) - .decrement('uncategorized_transactions', 1); + await this.updateUncategorizedTransactionCount(queryContext, false); } } diff --git a/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts b/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts index b313d7f3e..b3bf85ddc 100644 --- a/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts +++ b/packages/server/src/services/Banking/Plaid/PlaidSyncDB.ts @@ -8,7 +8,6 @@ import { transformPlaidAccountToCreateAccount, transformPlaidTrxsToCashflowCreate, } from './utils'; -import NewCashflowTransactionService from '@/services/Cashflow/NewCashflowTransactionService'; import { DeleteCashflowTransaction } from '@/services/Cashflow/DeleteCashflowTransactionService'; import HasTenancyService from '@/services/Tenancy/TenancyService'; import { CashflowApplication } from '@/services/Cashflow/CashflowApplication'; @@ -26,9 +25,6 @@ export class PlaidSyncDb { @Inject() private cashflowApp: CashflowApplication; - @Inject() - private createCashflowTransactionService: NewCashflowTransactionService; - @Inject() private deleteCashflowTransactionService: DeleteCashflowTransaction; diff --git a/packages/server/src/services/Cashflow/UncategorizeTransactionByRef.ts b/packages/server/src/services/Cashflow/UncategorizeTransactionByRef.ts deleted file mode 100644 index f5590fb83..000000000 --- a/packages/server/src/services/Cashflow/UncategorizeTransactionByRef.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Service } from 'typedi'; -import { UncategorizeCashflowTransaction } from './UncategorizeCashflowTransaction'; - -@Service() -export class UncategorizeTransactionByRef { - private uncategorizeTransactionService: UncategorizeCashflowTransaction; - - public uncategorize(tenantId: number, refId: number, refType: string) {} -} diff --git a/packages/webapp/src/containers/CashFlow/AccountTransactions/AccountTransactionsList.tsx b/packages/webapp/src/containers/CashFlow/AccountTransactions/AccountTransactionsList.tsx index 5a5b33d3f..43b4b706d 100644 --- a/packages/webapp/src/containers/CashFlow/AccountTransactions/AccountTransactionsList.tsx +++ b/packages/webapp/src/containers/CashFlow/AccountTransactions/AccountTransactionsList.tsx @@ -1,6 +1,5 @@ // @ts-nocheck import React, { Suspense } from 'react'; -import styled from 'styled-components'; import { Spinner } from '@blueprintjs/core'; import '@/style/pages/CashFlow/AccountTransactions/List.scss'; diff --git a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer.tsx b/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer.tsx deleted file mode 100644 index 1a54468c0..000000000 --- a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer.tsx +++ /dev/null @@ -1,33 +0,0 @@ -// @ts-nocheck -import React, { lazy } from 'react'; -import { Drawer, DrawerSuspense } from '@/components'; -import withDrawers from '@/containers/Drawer/withDrawers'; - -import { compose } from '@/utils'; - -const AccountDrawerContent = lazy(() => import('./AccountDrawerContent')); - -/** - * Categorize the uncategorized transaction drawer. - */ -function CategorizeTransactionDrawer({ - name, - // #withDrawer - isOpen, - payload: { uncategorizedTranasctionId }, -}) { - return ( - - - - - - ); -} - -export default compose(withDrawers())(AccountDrawer); diff --git a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionForm.tsx b/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionForm.tsx index 364c4111c..a10387af6 100644 --- a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionForm.tsx +++ b/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionForm.tsx @@ -1,7 +1,6 @@ // @ts-nocheck import { Formik, Form } from 'formik'; import styled from 'styled-components'; -import withDialogActions from '@/containers/Dialog/withDialogActions'; import { CreateCategorizeTransactionSchema } from './CategorizeTransactionForm.schema'; import { CategorizeTransactionFormContent } from './CategorizeTransactionFormContent'; import { CategorizeTransactionFormFooter } from './CategorizeTransactionFormFooter'; diff --git a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionFormContent.tsx b/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionFormContent.tsx index dfe1db6b0..95d2bc974 100644 --- a/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionFormContent.tsx +++ b/packages/webapp/src/containers/CashFlow/CategorizeTransaction/drawers/CategorizeTransactionDrawer/CategorizeTransactionFormContent.tsx @@ -2,7 +2,7 @@ import React from 'react'; import styled from 'styled-components'; import { FormGroup } from '@blueprintjs/core'; -import { FFormGroup, FSelect, FSuggest } from '@/components'; +import { FFormGroup, FSelect, } from '@/components'; import { getAddMoneyInOptions, getAddMoneyOutOptions } from '@/constants'; import { useFormikContext } from 'formik'; import { useCategorizeTransactionBoot } from './CategorizeTransactionBoot'; diff --git a/packages/webapp/src/hooks/query/cashflowAccounts.tsx b/packages/webapp/src/hooks/query/cashflowAccounts.tsx index 3a225db58..0bafef8bb 100644 --- a/packages/webapp/src/hooks/query/cashflowAccounts.tsx +++ b/packages/webapp/src/hooks/query/cashflowAccounts.tsx @@ -213,7 +213,8 @@ export function useRefreshCashflowTransactions() { } /** - * + * Retrieves specific uncategorized transaction. + * @param {number} uncategorizedTranasctionId - */ export function useUncategorizedTransaction( uncategorizedTranasctionId: nunber,