feat: wip categorized transactions

This commit is contained in:
Ahmed Bouhuolia
2024-03-04 21:01:36 +02:00
parent f23e8d98f6
commit 68f2f4ee84
12 changed files with 115 additions and 30 deletions

View File

@@ -7,6 +7,7 @@ exports.up = function (knex) {
table.decimal('amount');
table.string('currency_code');
table.string('reference_no').index();
table.string('payee');
table
.integer('account_id')
.unsigned()

View File

@@ -0,0 +1,7 @@
exports.up = function (knex) {
return knex.schema.table('accounts', (table) => {
table.integer('uncategorized_transactions').defaultTo(0);
});
};
exports.down = function (knex) {};

View File

@@ -264,6 +264,7 @@ export interface CreateUncategorizedTransactionDTO {
accountId: number;
amount: number;
currencyCode: string;
payee?: string;
description?: string;
referenceNo?: string | null;
plaidTransactionId?: string | null;

View File

@@ -38,7 +38,7 @@ export interface PlaidTransaction {
iso_currency_code: string;
transaction_id: string;
transaction_type: string;
payment_meta: { reference_number: string | null };
payment_meta: { reference_number: string | null; payee: string | null };
}
export interface PlaidFetchedTransactionsUpdates {

View File

@@ -196,6 +196,7 @@ export default class Account extends mixin(TenantModel, [
const Expense = require('models/Expense');
const ExpenseEntry = require('models/ExpenseCategory');
const ItemEntry = require('models/ItemEntry');
const UncategorizedTransaction = require('models/UncategorizedCashflowTransaction');
return {
/**
@@ -305,6 +306,21 @@ export default class Account extends mixin(TenantModel, [
to: 'items_entries.sellAccountId',
},
},
/**
* Associated uncategorized transactions.
*/
uncategorizedTransactions: {
relation: Model.HasManyRelation,
modelClass: UncategorizedTransaction.default,
join: {
from: 'accounts.id',
to: 'uncategorized_cashflow_transactions.accountId',
},
filter: (query) => {
query.filter('categorized', false);
},
},
};
}

View File

@@ -1,6 +1,7 @@
/* eslint-disable global-require */
import TenantModel from 'models/TenantModel';
import { Model } from 'objection';
import Account from './Account';
export default class UncategorizedCashflowTransaction extends TenantModel {
amount: number;
@@ -80,4 +81,29 @@ export default class UncategorizedCashflowTransaction extends TenantModel {
},
};
}
/**
*
* @param 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);
}
/**
*
* @param queryContext
*/
public async $afterDelete(queryContext) {
await super.$afterDelete(queryContext);
await Account.query()
.findById(this.accountId)
.decrement('uncategorized_transactions', 1);
}
}

View File

@@ -90,7 +90,7 @@ export class PlaidSyncDb {
tenantId,
uncategoriedDTO
),
{ concurrency: CONCURRENCY_ASYNC }
{ concurrency: 1 }
);
}

View File

@@ -44,8 +44,9 @@ export const transformPlaidTrxsToCashflowCreate = R.curry(
): CreateUncategorizedTransactionDTO => {
return {
date: plaidTranasction.date,
description: plaidTranasction.name,
amount: plaidTranasction.amount,
description: plaidTranasction.name,
payee: plaidTranasction.payment_meta?.payee,
currencyCode: plaidTranasction.iso_currency_code,
accountId: cashflowAccountId,
referenceNo: plaidTranasction.payment_meta?.reference_number,

View File

@@ -1,6 +1,6 @@
import { Inject, Service } from 'typedi';
import HasTenancyService from '../Tenancy/TenancyService';
import UnitOfWork from '../UnitOfWork';
import UnitOfWork, { IsolationLevel } from '../UnitOfWork';
import { Knex } from 'knex';
import { CreateUncategorizedTransactionDTO } from '@/interfaces';
@@ -21,15 +21,20 @@ export class CreateUncategorizedTransaction {
tenantId: number,
createDTO: CreateUncategorizedTransactionDTO
) {
const { UncategorizedCashflowTransaction } = this.tenancy.models(tenantId);
const { UncategorizedCashflowTransaction, Account } =
this.tenancy.models(tenantId);
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
const transaction = await UncategorizedCashflowTransaction.query(
trx
).insertAndFetch({
...createDTO,
});
return transaction;
});
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
const transaction = await UncategorizedCashflowTransaction.query(
trx
).insertAndFetch({
...createDTO,
});
return transaction;
},
);
}
}

View File

@@ -29,9 +29,12 @@ export class UncategorizedTransactionTransformer extends Transformer {
* @returns {string}
*/
protected formattetDepositAmount(transaction) {
return formatNumber(transaction.deposit, {
currencyCode: transaction.currencyCode,
});
if (transaction.isDepositTransaction) {
return formatNumber(transaction.deposit, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
/**
@@ -40,8 +43,11 @@ export class UncategorizedTransactionTransformer extends Transformer {
* @returns {string}
*/
protected formattedWithdrawalAmount(transaction) {
return formatNumber(transaction.withdrawal, {
currencyCode: transaction.currencyCode,
});
if (transaction.isWithdrawalTransaction) {
return formatNumber(transaction.withdrawal, {
currencyCode: transaction.currencyCode,
});
}
return '';
}
}