mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-19 14:20:31 +00:00
refactor: dynamic list to nestjs
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
|
||||
import { CreateBill } from './commands/CreateBill.service';
|
||||
import { EditBillService } from './commands/EditBill.service';
|
||||
import { GetBill } from './queries/GetBill';
|
||||
// import { GetBills } from './queries/GetBills';
|
||||
import { DeleteBill } from './commands/DeleteBill.service';
|
||||
import {
|
||||
IBillDTO,
|
||||
IBillEditDTO,
|
||||
} from './Bills.types';
|
||||
import { IBillDTO, IBillEditDTO } from './Bills.types';
|
||||
import { GetDueBills } from './queries/GetDueBills.service';
|
||||
import { OpenBillService } from './commands/OpenBill.service';
|
||||
import { GetBillPayments } from './queries/GetBillPayments';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { GetBillsService } from './queries/GetBills.service';
|
||||
|
||||
@Injectable()
|
||||
export class BillsApplication {
|
||||
@@ -22,7 +19,7 @@ export class BillsApplication {
|
||||
private deleteBillService: DeleteBill,
|
||||
private getDueBillsService: GetDueBills,
|
||||
private openBillService: OpenBillService,
|
||||
// private getBillsService: GetBills,
|
||||
private getBillsService: GetBillsService,
|
||||
// private getBillPaymentsService: GetBillPayments,
|
||||
) {}
|
||||
|
||||
@@ -56,14 +53,11 @@ export class BillsApplication {
|
||||
|
||||
/**
|
||||
* Retrieve bills data table list.
|
||||
* @param {number} tenantId -
|
||||
* @param {IBillsFilter} billsFilter -
|
||||
*/
|
||||
// public getBills(
|
||||
// filterDTO: IBillsFilter,
|
||||
// ) {
|
||||
// return this.getBillsService.getBills(filterDTO);
|
||||
// }
|
||||
public getBills(filterDTO: IBillsFilter) {
|
||||
return this.getBillsService.getBills(filterDTO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the given bill details.
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
Param,
|
||||
Delete,
|
||||
Get,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { BillsApplication } from './Bills.application';
|
||||
import { IBillDTO, IBillEditDTO } from './Bills.types';
|
||||
@@ -31,6 +32,11 @@ export class BillsController {
|
||||
return this.billsApplication.deleteBill(billId);
|
||||
}
|
||||
|
||||
@Get()
|
||||
getBills(@Query() filterDTO: IBillsFilter) {
|
||||
return this.billsApplication.getBills(filterDTO);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getBill(@Param('id') billId: number) {
|
||||
return this.billsApplication.getBill(billId);
|
||||
|
||||
@@ -21,6 +21,8 @@ import { BillGLEntriesSubscriber } from './subscribers/BillGLEntriesSubscriber';
|
||||
import { BillGLEntries } from './commands/BillsGLEntries';
|
||||
import { LedgerModule } from '../Ledger/Ledger.module';
|
||||
import { AccountsModule } from '../Accounts/Accounts.module';
|
||||
import { BillWriteInventoryTransactionsSubscriber } from './subscribers/BillWriteInventoryTransactionsSubscriber';
|
||||
import { BillInventoryTransactions } from './commands/BillInventoryTransactions';
|
||||
|
||||
@Module({
|
||||
imports: [BillLandedCostsModule, LedgerModule, AccountsModule],
|
||||
@@ -43,6 +45,8 @@ import { AccountsModule } from '../Accounts/Accounts.module';
|
||||
BillGLEntries,
|
||||
ItemsEntriesService,
|
||||
BillGLEntriesSubscriber,
|
||||
BillInventoryTransactions,
|
||||
BillWriteInventoryTransactionsSubscriber,
|
||||
],
|
||||
controllers: [BillsController],
|
||||
})
|
||||
|
||||
@@ -70,8 +70,6 @@ export interface IBillEditingPayload {
|
||||
trx: Knex.Transaction;
|
||||
}
|
||||
export interface IBillEditedPayload {
|
||||
// tenantId: number;
|
||||
// billId: number;
|
||||
oldBill: Bill;
|
||||
bill: Bill;
|
||||
billDTO: IBillDTO;
|
||||
|
||||
@@ -1,82 +1,73 @@
|
||||
// import { Knex } from 'knex';
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import InventoryService from '@/services/Inventory/Inventory';
|
||||
// import ItemsEntriesService from '@/services/Items/ItemsEntriesService';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import { Knex } from 'knex';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service';
|
||||
import { InventoryService } from '@/modules/InventoryCost/Inventory';
|
||||
|
||||
// @Service()
|
||||
// export class BillInventoryTransactions {
|
||||
// @Inject()
|
||||
// private tenancy: HasTenancyService;
|
||||
@Injectable()
|
||||
export class BillInventoryTransactions {
|
||||
constructor(
|
||||
private readonly itemsEntriesService: ItemsEntriesService,
|
||||
private readonly inventoryService: InventoryService,
|
||||
|
||||
// @Inject()
|
||||
// private itemsEntriesService: ItemsEntriesService;
|
||||
private readonly bill: typeof Bill
|
||||
) {}
|
||||
|
||||
// @Inject()
|
||||
// private inventoryService: InventoryService;
|
||||
/**
|
||||
* Records the inventory transactions from the given bill input.
|
||||
* @param {number} billId - Bill id.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async recordInventoryTransactions(
|
||||
billId: number,
|
||||
override?: boolean,
|
||||
trx?: Knex.Transaction
|
||||
): Promise<void> {
|
||||
// Retireve bill with assocaited entries and allocated cost entries.
|
||||
|
||||
const bill = await this.bill.query(trx)
|
||||
.findById(billId)
|
||||
.withGraphFetched('entries.allocatedCostEntries');
|
||||
|
||||
// /**
|
||||
// * Records the inventory transactions from the given bill input.
|
||||
// * @param {Bill} bill - Bill model object.
|
||||
// * @param {number} billId - Bill id.
|
||||
// * @return {Promise<void>}
|
||||
// */
|
||||
// public async recordInventoryTransactions(
|
||||
// tenantId: number,
|
||||
// billId: number,
|
||||
// override?: boolean,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> {
|
||||
// const { Bill } = this.tenancy.models(tenantId);
|
||||
// Loads the inventory items entries of the given sale invoice.
|
||||
const inventoryEntries =
|
||||
await this.itemsEntriesService.filterInventoryEntries(
|
||||
bill.entries
|
||||
);
|
||||
const transaction = {
|
||||
transactionId: bill.id,
|
||||
transactionType: 'Bill',
|
||||
exchangeRate: bill.exchangeRate,
|
||||
|
||||
// // Retireve bill with assocaited entries and allocated cost entries.
|
||||
// const bill = await Bill.query(trx)
|
||||
// .findById(billId)
|
||||
// .withGraphFetched('entries.allocatedCostEntries');
|
||||
date: bill.billDate,
|
||||
direction: 'IN',
|
||||
entries: inventoryEntries,
|
||||
createdAt: bill.createdAt,
|
||||
|
||||
// // Loads the inventory items entries of the given sale invoice.
|
||||
// const inventoryEntries =
|
||||
// await this.itemsEntriesService.filterInventoryEntries(
|
||||
// tenantId,
|
||||
// bill.entries
|
||||
// );
|
||||
// const transaction = {
|
||||
// transactionId: bill.id,
|
||||
// transactionType: 'Bill',
|
||||
// exchangeRate: bill.exchangeRate,
|
||||
warehouseId: bill.warehouseId,
|
||||
};
|
||||
await this.inventoryService.recordInventoryTransactionsFromItemsEntries(
|
||||
transaction,
|
||||
override,
|
||||
trx
|
||||
);
|
||||
}
|
||||
|
||||
// date: bill.billDate,
|
||||
// direction: 'IN',
|
||||
// entries: inventoryEntries,
|
||||
// createdAt: bill.createdAt,
|
||||
|
||||
// warehouseId: bill.warehouseId,
|
||||
// };
|
||||
// await this.inventoryService.recordInventoryTransactionsFromItemsEntries(
|
||||
// tenantId,
|
||||
// transaction,
|
||||
// override,
|
||||
// trx
|
||||
// );
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Reverts the inventory transactions of the given bill id.
|
||||
// * @param {number} tenantId - Tenant id.
|
||||
// * @param {number} billId - Bill id.
|
||||
// * @return {Promise<void>}
|
||||
// */
|
||||
// public async revertInventoryTransactions(
|
||||
// tenantId: number,
|
||||
// billId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) {
|
||||
// // Deletes the inventory transactions by the given reference id and type.
|
||||
// await this.inventoryService.deleteInventoryTransactions(
|
||||
// tenantId,
|
||||
// billId,
|
||||
// 'Bill',
|
||||
// trx
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* Reverts the inventory transactions of the given bill id.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} billId - Bill id.
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async revertInventoryTransactions(
|
||||
billId: number,
|
||||
trx?: Knex.Transaction
|
||||
) {
|
||||
// Deletes the inventory transactions by the given reference id and type.
|
||||
await this.inventoryService.deleteInventoryTransactions(
|
||||
billId,
|
||||
'Bill',
|
||||
trx
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import * as R from 'ramda';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { DynamicListService } from '@/modules/DynamicListing/DynamicList.service';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { IFilterMeta, IPaginationMeta } from '@/interfaces/Model';
|
||||
import { BillTransformer } from './Bill.transformer';
|
||||
|
||||
@Injectable()
|
||||
export class GetBillsService {
|
||||
constructor(
|
||||
private transformer: TransformerInjectable,
|
||||
private dynamicListService: DynamicListService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve bills data table list.
|
||||
* @param {IBillsFilter} billsFilter -
|
||||
*/
|
||||
public async getBills(
|
||||
filterDTO: IBillsFilter,
|
||||
): Promise<{
|
||||
bills: Bill;
|
||||
pagination: IPaginationMeta;
|
||||
filterMeta: IFilterMeta;
|
||||
}> {
|
||||
// Parses bills list filter DTO.
|
||||
const filter = this.parseListFilterDTO(filterDTO);
|
||||
|
||||
// Dynamic list service.
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
Bill,
|
||||
filter,
|
||||
);
|
||||
const { results, pagination } = await Bill.query()
|
||||
.onBuild((builder) => {
|
||||
builder.withGraphFetched('vendor');
|
||||
builder.withGraphFetched('entries.item');
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
|
||||
// Filter query.
|
||||
filterDTO?.filterQuery && filterDTO?.filterQuery(builder);
|
||||
})
|
||||
.pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// Tranform the bills to POJO.
|
||||
const bills = await this.transformer.transform(
|
||||
results,
|
||||
new BillTransformer(),
|
||||
);
|
||||
return {
|
||||
bills,
|
||||
pagination,
|
||||
filterMeta: dynamicFilter.getResponseMeta(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses bills list filter DTO.
|
||||
* @param filterDTO -
|
||||
*/
|
||||
private parseListFilterDTO(filterDTO) {
|
||||
return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
// import { Injectable } from '@nestjs/common';
|
||||
// import * as R from 'ramda';
|
||||
// import {
|
||||
// IBill,
|
||||
// IBillsFilter,
|
||||
// IFilterMeta,
|
||||
// IPaginationMeta,
|
||||
// } from '@/interfaces';
|
||||
// import { BillTransformer } from './Bill.transformer';
|
||||
// import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
// // import { DynamicListingService } from '@/modules/DynamicListing/DynamicListService';
|
||||
|
||||
// @Injectable()
|
||||
// export class GetBills {
|
||||
// constructor(
|
||||
// private transformer: TransformerInjectable,
|
||||
// private dynamicListService: DynamicListingService,
|
||||
// ) {}
|
||||
|
||||
// /**
|
||||
// * Retrieve bills data table list.
|
||||
// * @param {number} tenantId -
|
||||
// * @param {IBillsFilter} billsFilter -
|
||||
// */
|
||||
// public async getBills(
|
||||
// tenantId: number,
|
||||
// filterDTO: IBillsFilter,
|
||||
// ): Promise<{
|
||||
// bills: IBill;
|
||||
// pagination: IPaginationMeta;
|
||||
// filterMeta: IFilterMeta;
|
||||
// }> {
|
||||
// // Parses bills list filter DTO.
|
||||
// const filter = this.parseListFilterDTO(filterDTO);
|
||||
|
||||
// // Dynamic list service.
|
||||
// const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
// tenantId,
|
||||
// Bill,
|
||||
// filter,
|
||||
// );
|
||||
// const { results, pagination } = await Bill.query()
|
||||
// .onBuild((builder) => {
|
||||
// builder.withGraphFetched('vendor');
|
||||
// builder.withGraphFetched('entries.item');
|
||||
// dynamicFilter.buildQuery()(builder);
|
||||
|
||||
// // Filter query.
|
||||
// filterDTO?.filterQuery && filterDTO?.filterQuery(builder);
|
||||
// })
|
||||
// .pagination(filter.page - 1, filter.pageSize);
|
||||
|
||||
// // Tranform the bills to POJO.
|
||||
// const bills = await this.transformer.transform(
|
||||
// results,
|
||||
// new PurchaseInvoiceTransformer(),
|
||||
// );
|
||||
// return {
|
||||
// bills,
|
||||
// pagination,
|
||||
// filterMeta: dynamicFilter.getResponseMeta(),
|
||||
// };
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Parses bills list filter DTO.
|
||||
// * @param filterDTO -
|
||||
// */
|
||||
// private parseListFilterDTO(filterDTO) {
|
||||
// return R.compose(this.dynamicListService.parseStringifiedFilter)(filterDTO);
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
IBillCreatedPayload,
|
||||
IBillEditedPayload,
|
||||
IBIllEventDeletedPayload,
|
||||
IBillOpenedPayload,
|
||||
} from '../Bills.types';
|
||||
import { BillInventoryTransactions } from '../commands/BillInventoryTransactions';
|
||||
import { events } from '@/common/events/events';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
|
||||
@Injectable()
|
||||
export class BillWriteInventoryTransactionsSubscriber {
|
||||
constructor(private readonly billsInventory: BillInventoryTransactions) {}
|
||||
|
||||
/**
|
||||
* Handles writing the inventory transactions once bill created.
|
||||
* @param {IBillCreatedPayload | IBillOpenedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.bill.onCreated)
|
||||
@OnEvent(events.bill.onOpened)
|
||||
public async handleWritingInventoryTransactions({
|
||||
bill,
|
||||
trx,
|
||||
}: IBillCreatedPayload | IBillOpenedPayload) {
|
||||
// Can't continue if the bill is not opened yet.
|
||||
if (!bill.openedAt) return null;
|
||||
|
||||
await this.billsInventory.recordInventoryTransactions(bill.id, false, trx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the overwriting the inventory transactions once bill edited.
|
||||
* @param {IBillEditedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.bill.onEdited)
|
||||
public async handleOverwritingInventoryTransactions({
|
||||
bill,
|
||||
trx,
|
||||
}: IBillEditedPayload) {
|
||||
// Can't continue if the bill is not opened yet.
|
||||
if (!bill.openedAt) return null;
|
||||
|
||||
await this.billsInventory.recordInventoryTransactions(bill.id, true, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handles the reverting the inventory transactions once the bill deleted.
|
||||
* @param {IBIllEventDeletedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.bill.onDeleted)
|
||||
public async handleRevertInventoryTransactions({
|
||||
billId,
|
||||
trx,
|
||||
}: IBIllEventDeletedPayload) {
|
||||
await this.billsInventory.revertInventoryTransactions(billId, trx);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user