refactor: nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-02-18 19:26:58 +02:00
parent 5c0bb52b59
commit 95bb4fc8e3
30 changed files with 249 additions and 114 deletions

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import { GConstructor } from '@/common/types/Constructor';
import { isEmpty } from 'lodash';
import { FinancialSheet } from './FinancialSheet';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as moment from 'moment';
import { ITableColumn, ITableColumnAccessor } from '../types/Table.types';
import { IDateRange } from '../types/Report.types';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as moment from 'moment';
import { ITableColumn, ITableColumnAccessor } from '../types/Table.types';
import { IDateRange } from '../types/Report.types';

View File

@@ -5,13 +5,14 @@ import { Vendor } from '@/modules/Vendors/models/Vendor';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { IAPAgingSummaryQuery } from './APAgingSummary.types';
import { ModelObject } from 'objection';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
export class APAgingSummaryRepository {
@Inject(Vendor.name)
private readonly vendorModel: typeof Vendor;
private readonly vendorModel: TenantModelProxy<typeof Vendor>;
@Inject(Bill.name)
private readonly billModel: typeof Bill;
private readonly billModel: TenantModelProxy<typeof Bill>;
@Inject(TenancyContext)
private readonly tenancyContext: TenancyContext;
@@ -93,8 +94,8 @@ export class APAgingSummaryRepository {
// Retrieve all vendors from the storage.
const vendors =
this.filter.vendorsIds.length > 0
? await this.vendorModel.query().whereIn('id', this.filter.vendorsIds)
: await this.vendorModel.query();
? await this.vendorModel().query().whereIn('id', this.filter.vendorsIds)
: await this.vendorModel().query();
this.vendors = vendors;
}
@@ -108,7 +109,7 @@ export class APAgingSummaryRepository {
query.modify('filterByBranches', this.filter.branchesIds);
}
};
const overdueBills = await this.billModel
const overdueBills = await this.billModel()
.query()
.modify('overdueBillsFromDate', this.filter.asDate)
.onBuild(commonQuery);
@@ -127,7 +128,7 @@ export class APAgingSummaryRepository {
}
};
// Retrieve all due vendors bills.
const dueBills = await this.billModel
const dueBills = await this.billModel()
.query()
.modify('dueBillsFromDate', this.filter.asDate)
.onBuild(commonQuery);

View File

@@ -5,16 +5,17 @@ import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
import { ModelObject } from 'objection';
import { IARAgingSummaryQuery } from './ARAgingSummary.types';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
export class ARAgingSummaryRepository {
@Inject(TenancyContext)
private tenancyContext: TenancyContext;
@Inject(Customer.name)
private customerModel: typeof Customer;
private customerModel: TenantModelProxy<typeof Customer>;
@Inject(SaleInvoice.name)
private saleInvoiceModel: typeof SaleInvoice;
private saleInvoiceModel: TenantModelProxy<typeof SaleInvoice>;
/**
* Filter.
@@ -92,10 +93,10 @@ export class ARAgingSummaryRepository {
// Retrieve all customers from the storage.
const customers =
this.filter.customersIds.length > 0
? await this.customerModel
? await this.customerModel()
.query()
.whereIn('id', this.filter.customersIds)
: await this.customerModel.query();
: await this.customerModel().query();
this.customers = customers;
}
@@ -110,7 +111,7 @@ export class ARAgingSummaryRepository {
}
};
// Retrieve all overdue sale invoices.
const overdueSaleInvoices = await this.saleInvoiceModel
const overdueSaleInvoices = await this.saleInvoiceModel()
.query()
.modify('overdueInvoicesFromDate', this.filter.asDate)
.onBuild(commonQuery);
@@ -132,7 +133,7 @@ export class ARAgingSummaryRepository {
}
};
// Retrieve all due sale invoices.
const currentInvoices = await this.saleInvoiceModel
const currentInvoices = await this.saleInvoiceModel()
.query()
.modify('dueInvoicesFromDate', this.filter.asDate)
.onBuild(commonQuery);

View File

@@ -14,6 +14,7 @@ import { Ledger } from '@/modules/Ledger/Ledger';
import { transformToMapBy } from '@/utils/transform-to-map-by';
import { Account } from '@/modules/Accounts/models/Account.model';
import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction.model';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class BalanceSheetRepository extends R.compose(
@@ -24,13 +25,15 @@ export class BalanceSheetRepository extends R.compose(
* Account model.
*/
@Inject(Account.name)
public readonly accountModel: typeof Account;
public readonly accountModel: TenantModelProxy<typeof Account>;
/**
* Account transaction model.
*/
@Inject(AccountTransaction.name)
public readonly accountTransactionModel: typeof AccountTransaction;
public readonly accountTransactionModel: TenantModelProxy<
typeof AccountTransaction
>;
/**
* @description Balance sheet query.
@@ -216,7 +219,7 @@ export class BalanceSheetRepository extends R.compose(
* Initialize accounts graph.
*/
public initAccountsGraph = async () => {
this.accountsGraph = this.accountModel.toDependencyGraph(this.accounts);
this.accountsGraph = this.accountModel().toDependencyGraph(this.accounts);
};
// ----------------------------
@@ -338,7 +341,7 @@ export class BalanceSheetRepository extends R.compose(
* @return {Promise<IAccount[]>}
*/
public getAccounts = () => {
return this.accountModel.query();
return this.accountModel().query();
};
/**
@@ -353,18 +356,20 @@ export class BalanceSheetRepository extends R.compose(
toDate: Date,
datePeriodsType: string,
) => {
return this.accountTransactionModel.query().onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
return this.accountTransactionModel()
.query()
.onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
query.modify('groupByDateFormat', datePeriodsType);
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
query.modify('groupByDateFormat', datePeriodsType);
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
this.commonFilterBranchesQuery(query);
});
this.commonFilterBranchesQuery(query);
});
};
/**
@@ -372,17 +377,19 @@ export class BalanceSheetRepository extends R.compose(
* @param {Date|string} openingDate -
*/
public closingAccountsTotal = async (openingDate: Date | string) => {
return this.accountTransactionModel.query().onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
return this.accountTransactionModel()
.query()
.onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
query.modify('filterDateRange', null, openingDate);
query.withGraphFetched('account');
query.modify('filterDateRange', null, openingDate);
query.withGraphFetched('account');
this.commonFilterBranchesQuery(query);
});
this.commonFilterBranchesQuery(query);
});
};
/**

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { defaultTo, set, sumBy, isEmpty, mapValues, get } from 'lodash';
import * as mathjs from 'mathjs';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { sumBy, mapValues, get } from 'lodash';
import { ACCOUNT_ROOT_TYPE } from '@/constants/accounts';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import { Inject, Injectable } from '@nestjs/common';
import * as moment from 'moment';
import { Knex } from 'knex';
@@ -11,8 +12,8 @@ import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class CashFlowRepository {
/**
* @param {typeof Account} accountModel - Account model.
* @param {typeof AccountTransaction} accountTransactionModel - Account transaction model.
* @param {TenantModelProxy<typeof Account>} accountModel - Account model.
* @param {TenantModelProxy<typeof AccountTransaction>} accountTransactionModel - Account transaction model.
*/
constructor(
@Inject(Account.name)

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { isEmpty } from 'lodash';
import moment from 'moment';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { map } from 'lodash';
import { Account } from "@/modules/Accounts/models/Account.model";

View File

@@ -12,6 +12,7 @@ import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { transformToMap } from '@/utils/transform-to-key';
import { Ledger } from '@/modules/Ledger/Ledger';
import { TenantModel } from '@/modules/System/models/TenantModel';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class GeneralLedgerRepository {
@@ -41,10 +42,12 @@ export class GeneralLedgerRepository {
private readonly accountRepository: AccountRepository;
@Inject(AccountTransaction.name)
private readonly accountTransactionModel: typeof AccountTransaction;
private readonly accountTransactionModel: TenantModelProxy<
typeof AccountTransaction
>;
@Inject(Contact.name)
private readonly contactModel: typeof Contact;
private readonly contactModel: TenantModelProxy<typeof Contact>;
@Inject(TenancyContext)
private readonly tenancyContext: TenancyContext;
@@ -97,7 +100,7 @@ export class GeneralLedgerRepository {
* Initialize the contacts.
*/
public async initContacts() {
this.contacts = await this.contactModel.query();
this.contacts = await this.contactModel().query();
this.contactsById = transformToMap(this.contacts, 'id');
}
@@ -105,7 +108,7 @@ export class GeneralLedgerRepository {
* Initialize the G/L transactions from/to the given date.
*/
public async initTransactions() {
this.transactions = await this.accountTransactionModel
this.transactions = await this.accountTransactionModel()
.query()
.onBuild((query) => {
query.modify(
@@ -132,7 +135,7 @@ export class GeneralLedgerRepository {
*/
public async initAccountsOpeningBalance() {
// Retrieves opening balance credit/debit sumation.
this.openingBalanceTransactions = await this.accountTransactionModel
this.openingBalanceTransactions = await this.accountTransactionModel()
.query()
.onBuild((query) => {
const toDate = moment(this.filter.fromDate).subtract(1, 'day');

View File

@@ -8,14 +8,17 @@ import { Inject, Injectable, Scope } from '@nestjs/common';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { transformToMapKeyValue } from '@/utils/transform-to-map-key-value';
import { transformToMapBy } from '@/utils/transform-to-map-by';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class InventoryItemDetailsRepository {
@Inject(Item.name)
readonly itemModel: typeof Item;
readonly itemModel: TenantModelProxy<typeof Item>;
@Inject(InventoryTransaction.name)
readonly inventoryTransactionModel: typeof InventoryTransaction;
readonly inventoryTransactionModel: TenantModelProxy<
typeof InventoryTransaction
>;
@Inject(TenancyContext)
readonly tenancyContext: TenancyContext;
@@ -141,7 +144,7 @@ export class InventoryItemDetailsRepository {
public async getInventoryItems(
itemsIds?: number[],
): Promise<ModelObject<Item>[]> {
return this.itemModel.query().onBuild((q) => {
return this.itemModel().query().onBuild((q) => {
q.where('type', 'inventory');
if (!isEmpty(itemsIds)) {
@@ -163,7 +166,7 @@ export class InventoryItemDetailsRepository {
.toDate();
// Opening inventory transactions.
const openingTransactions = this.inventoryTransactionModel
const openingTransactions = this.inventoryTransactionModel()
.query()
.select('*')
.select(raw("IF(`DIRECTION` = 'IN', `QUANTITY`, 0) as 'QUANTITY_IN'"))
@@ -188,8 +191,7 @@ export class InventoryItemDetailsRepository {
if (!isEmpty(filter.branchesIds)) {
openingTransactions.modify('filterByBranches', filter.branchesIds);
}
const openingBalanceTransactions = await this.inventoryTransactionModel
const openingBalanceTransactions = await this.inventoryTransactionModel()
.query()
.from(openingTransactions)
.select('itemId')
@@ -214,7 +216,7 @@ export class InventoryItemDetailsRepository {
public async getItemInventoryTransactions(
filter: IInventoryDetailsQuery,
): Promise<ModelObject<InventoryTransaction>[]> {
const inventoryTransactions = this.inventoryTransactionModel
const inventoryTransactions = this.inventoryTransactionModel()
.query()
.modify('filterDateRange', filter.fromDate, filter.toDate)
.orderBy('date', 'ASC')

View File

@@ -6,6 +6,7 @@ import { ModelObject } from 'objection';
import { Item } from '@/modules/Items/models/Item';
import { transformToMap } from '@/utils/transform-to-key';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class InventoryValuationSheetRepository {
@@ -13,10 +14,12 @@ export class InventoryValuationSheetRepository {
private readonly tenancyContext: TenancyContext;
@Inject(InventoryCostLotTracker.name)
private readonly inventoryCostLotTracker: typeof InventoryCostLotTracker;
private readonly inventoryCostLotTracker: TenantModelProxy<
typeof InventoryCostLotTracker
>;
@Inject(Item.name)
private readonly itemModel: typeof Item;
private readonly itemModel: TenantModelProxy<typeof Item>;
/**
* The filter.
@@ -89,8 +92,10 @@ export class InventoryValuationSheetRepository {
* Retrieve the inventory items.
*/
async asyncItems() {
const inventoryItems = await this.itemModel.query().onBuild((q) => {
q.where('type', 'inventory');
const inventoryItems = await this.itemModel()
.query()
.onBuild((q) => {
q.where('type', 'inventory');
if (this.filter.itemsIds.length > 0) {
q.whereIn('id', this.filter.itemsIds);
@@ -121,13 +126,13 @@ export class InventoryValuationSheetRepository {
}
};
// Retrieve the inventory cost `IN` transactions.
const INTransactions = await this.inventoryCostLotTracker
const INTransactions = await this.inventoryCostLotTracker()
.query()
.onBuild(commonQuery)
.where('direction', 'IN');
// Retrieve the inventory cost `OUT` transactions.
const OUTTransactions = await this.inventoryCostLotTracker
const OUTTransactions = await this.inventoryCostLotTracker()
.query()
.onBuild(commonQuery)
.where('direction', 'OUT');

View File

@@ -2,6 +2,7 @@ import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
import { Contact } from '@/modules/Contacts/models/Contact';
import { Ledger } from '@/modules/Ledger/Ledger';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { transformToMap } from '@/utils/transform-to-key';
import { Inject } from '@nestjs/common';
@@ -15,10 +16,10 @@ export class JournalSheetRepository {
private accountRepository: AccountRepository;
@Inject(Contact.name)
private contactModel: typeof Contact;
private contactModel: TenantModelProxy<typeof Contact>;
@Inject(AccountTransaction.name)
private accountTransaction: typeof AccountTransaction;
private accountTransaction: TenantModelProxy<typeof AccountTransaction>;
@Inject(AccountTransaction.name)
private accountTransactions: Array<ModelObject<AccountTransaction>>;
@@ -88,7 +89,7 @@ export class JournalSheetRepository {
*/
async initAccountTransactions() {
// Retrieve all journal transactions based on the given query.
const transactions = await this.accountTransaction
const transactions = await this.accountTransaction()
.query()
.onBuild((query) => {
if (this.filter.fromRange || this.filter.toRange) {
@@ -119,7 +120,7 @@ export class JournalSheetRepository {
* Initialize contacts.
*/
async initContacts() {
const contacts = await this.contactModel.query();
const contacts = await this.contactModel().query();
this.contacts = contacts;
this.contactsById = transformToMap(contacts, 'id');

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { TOTAL_NODE_TYPES } from './constants';
import { FinancialSheet } from '../../common/FinancialSheet';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import * as R from 'ramda';
import { get } from 'lodash';
import { ProfitLossSheetBase } from './ProfitLossSheetBase';

View File

@@ -1,3 +1,4 @@
// @ts-nocheck
import { merge } from 'lodash';
import * as R from 'ramda';
import { IProfitLossSheetQuery } from './ProfitLossSheet.types';

View File

@@ -1,9 +1,11 @@
// @ts-nocheck
import { Inject, Injectable, Scope } from '@nestjs/common';
import { ModelObject } from 'objection';
import { castArray } from 'lodash';
import * as R from 'ramda';
import { Knex } from 'knex';
import { isEmpty } from 'lodash';
import * as moment from 'moment';
import { transformToMapBy } from '@/utils/transform-to-map-by';
import { ProfitLossSheetQuery } from './ProfitLossSheetQuery';
import { Ledger } from '@/modules/Ledger/Ledger';
@@ -13,16 +15,17 @@ import { Account } from '@/modules/Accounts/models/Account.model';
import { FinancialDatePeriods } from '../../common/FinancialDatePeriods';
import { AccountTransaction } from '@/modules/Accounts/models/AccountTransaction.model';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
class {},
) {
@Inject(Account.name)
public accountModel: typeof Account;
public accountModel: TenantModelProxy<typeof Account>;
@Inject(AccountTransaction.name)
public accountTransactionModel: typeof AccountTransaction;
public accountTransactionModel: TenantModelProxy<typeof AccountTransaction>;
@Inject(TenancyContext)
public tenancyContext: TenancyContext;
@@ -206,7 +209,7 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
* Initialize accounts graph.
*/
private initAccountsGraph = async () => {
this.accountsGraph = this.accountModel.toDependencyGraph(this.accounts);
this.accountsGraph = this.accountModel().toDependencyGraph(this.accounts);
};
// ----------------------------
@@ -217,8 +220,8 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
*/
private initAccountsTotalLedger = async (): Promise<void> => {
const totalByAccount = await this.accountsTotal(
this.query.fromDate,
this.query.toDate,
this.query.query.fromDate,
this.query.query.toDate,
);
// Inject to the repository.
this.totalAccountsLedger = Ledger.fromTransactions(totalByAccount);
@@ -306,18 +309,23 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
/**
* Retrieve the opening balance transactions of the report.
*/
public accountsTotal = async (fromDate: Date, toDate: Date) => {
return this.accountTransactionModel.query().onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
public accountsTotal = async (
fromDate: moment.MomentInput,
toDate: moment.MomentInput,
) => {
return this.accountTransactionModel()
.query()
.onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
this.commonFilterBranchesQuery(query);
});
this.commonFilterBranchesQuery(query);
});
};
/**
@@ -331,18 +339,20 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
toDate: moment.MomentInput,
datePeriodsType,
) => {
return this.accountTransactionModel.query().onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
return this.accountTransactionModel()
.query()
.onBuild((query) => {
query.sum('credit as credit');
query.sum('debit as debit');
query.groupBy('accountId');
query.select(['accountId']);
query.modify('groupByDateFormat', datePeriodsType);
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
query.modify('groupByDateFormat', datePeriodsType);
query.modify('filterDateRange', fromDate, toDate);
query.withGraphFetched('account');
this.commonFilterBranchesQuery(query);
});
this.commonFilterBranchesQuery(query);
});
};
/**
@@ -360,7 +370,7 @@ export class ProfitLossSheetRepository extends R.compose(FinancialDatePeriods)(
* @return {Promise<IAccount[]>}
*/
private getAccounts = () => {
return this.accountModel.query();
return this.accountModel().query();
};
/**

View File

@@ -9,17 +9,20 @@ import { Account } from '@/modules/Accounts/models/Account.model';
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
import { ModelObject } from 'objection';
import { ACCOUNT_TYPE } from '@/constants/accounts';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable({ scope: Scope.TRANSIENT })
export class VendorBalanceSummaryRepository {
@Inject(AccountTransaction.name)
private readonly accountTransactionModel: typeof AccountTransaction;
private readonly accountTransactionModel: TenantModelProxy<
typeof AccountTransaction
>;
@Inject(Vendor.name)
private readonly vendorModel: typeof Vendor;
private readonly vendorModel: TenantModelProxy<typeof Vendor>;
@Inject(Account.name)
private readonly accountModel: typeof Account;
private readonly accountModel: TenantModelProxy<typeof Account>;
/**
* Filter.
@@ -99,7 +102,7 @@ export class VendorBalanceSummaryRepository {
public async getVendors(
vendorsIds?: number[],
): Promise<ModelObject<Vendor>[]> {
const vendorQuery = this.vendorModel.query().orderBy('displayName');
const vendorQuery = this.vendorModel().query().orderBy('displayName');
if (!isEmpty(vendorsIds)) {
vendorQuery.whereIn('id', vendorsIds);
@@ -113,7 +116,7 @@ export class VendorBalanceSummaryRepository {
* @returns {Promise<IAccount[]>}
*/
public async getPayableAccounts(): Promise<ModelObject<Account>[]> {
return this.accountModel
return this.accountModel()
.query()
.where('accountType', ACCOUNT_TYPE.ACCOUNTS_PAYABLE);
}
@@ -130,7 +133,7 @@ export class VendorBalanceSummaryRepository {
const payableAccountsIds = map(payableAccounts, 'id');
// Retrieve the customers transactions of A/R accounts.
const customersTranasctions = await this.accountTransactionModel
const customersTranasctions = await this.accountTransactionModel()
.query()
.onBuild((query) => {
query.whereIn('accountId', payableAccountsIds);