feat: wip migrate to nestjs

This commit is contained in:
Ahmed Bouhuolia
2024-12-26 15:40:29 +02:00
parent a6932d76f3
commit cd84872a61
96 changed files with 2051 additions and 745 deletions

View File

@@ -1,16 +1,16 @@
import { Inject, Injectable } from '@nestjs/common';
import { omit, sumBy } from 'lodash';
import moment from 'moment';
import { Inject, Injectable } from '@nestjs/common';
import * as R from 'ramda';
import * as composeAsync from 'async/compose';
import { formatDateFields } from '@/utils/format-date-fields';
import composeAsync from 'async/compose';
import { BranchTransactionDTOTransformer } from '@/modules/Branches/integrations/BranchTransactionDTOTransform';
import { WarehouseTransactionDTOTransform } from '@/modules/Warehouses/Integrations/WarehouseTransactionDTOTransform';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { Item } from '@/modules/Items/models/Item';
import { Vendor } from '@/modules/Vendors/models/Vendor';
import { ItemEntriesTaxTransactions } from '@/modules/TaxRates/ItemEntriesTaxTransactions.service';
import { IBillDTO } from '../Bills.types';
import { Vendor } from '@/modules/Vendors/models/Vendor';
import { Bill } from '../models/Bill';
import { assocItemEntriesDefaultIndex } from '@/utils/associate-item-entries-index';
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
@@ -23,8 +23,8 @@ export class BillDTOTransformer {
private taxDTOTransformer: ItemEntriesTaxTransactions,
private tenancyContext: TenancyContext,
@Inject(ItemEntry) private itemEntryModel: typeof ItemEntry,
@Inject(Item) private itemModel: typeof Item,
@Inject(ItemEntry.name) private itemEntryModel: typeof ItemEntry,
@Inject(Item.name) private itemModel: typeof Item,
) {}
/**
@@ -44,7 +44,9 @@ export class BillDTOTransformer {
private getBillLandedCostAmount(billDTO: IBillDTO): number {
const costEntries = billDTO.entries.filter((entry) => entry.landedCost);
return this.getBillEntriesTotal(costEntries);
// return this.getBillEntriesTotal(costEntries);
return 0;
}
/**
@@ -57,7 +59,7 @@ export class BillDTOTransformer {
billDTO: IBillDTO,
vendor: Vendor,
oldBill?: Bill,
) {
): Promise<Bill> {
const amount = sumBy(billDTO.entries, (e) =>
this.itemEntryModel.calcAmount(e),
);
@@ -112,9 +114,9 @@ export class BillDTOTransformer {
return R.compose(
// Associates tax amount withheld to the model.
this.taxDTOTransformer.assocTaxAmountWithheldFromEntries,
this.branchDTOTransform.transformDTO,
this.warehouseDTOTransform.transformDTO,
)(initialDTO);
this.branchDTOTransform.transformDTO<Bill>,
this.warehouseDTOTransform.transformDTO<Bill>,
)(initialDTO) as Bill;
}
/**

View File

@@ -6,6 +6,8 @@ import { IItemEntryDTO } from '@/modules/TransactionItemEntry/ItemEntry.types';
import { Item } from '@/modules/Items/models/Item';
import { BillPaymentEntry } from '@/modules/BillPayments/models/BillPaymentEntry';
import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost';
import { VendorCreditAppliedBill } from '@/modules/VendorCredit/models/VendorCreditAppliedBill';
import { transformToMap } from '@/utils/transform-to-key';
@Injectable()
export class BillsValidators {

View File

@@ -27,7 +27,7 @@ export class CreateBill {
private billModel: typeof Bill,
@Inject(Vendor.name)
private contactModel: typeof Vendor,
private vendorModel: typeof Vendor,
) {}
/**
@@ -49,9 +49,8 @@ export class CreateBill {
trx?: Knex.Transaction,
): Promise<Bill> {
// Retrieves the given bill vendor or throw not found error.
const vendor = await this.contactModel
const vendor = await this.vendorModel
.query()
.modify('vendor')
.findById(billDTO.vendorId)
.throwIfNotFound();
@@ -70,10 +69,7 @@ export class CreateBill {
billDTO.entries,
);
// Transform the bill DTO to model object.
const billObj = await this.transformerDTO.billDTOToModel(
billDTO,
vendor,
);
const billObj = await this.transformerDTO.billDTOToModel(billDTO, vendor);
// Write new bill transaction with associated transactions under UOW env.
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
@@ -84,12 +80,11 @@ export class CreateBill {
} as IBillCreatingPayload);
// Inserts the bill graph object to the storage.
const bill = await this.billModel.query(trx).upsertGraph(billObj);
const bill = await this.billModel.query(trx).upsertGraphAndFetch(billObj);
// Triggers `onBillCreated` event.
await this.eventPublisher.emitAsync(events.bill.onCreated, {
bill,
billId: bill.id,
billDTO,
trx,
} as IBillCreatedPayload);

View File

@@ -55,7 +55,7 @@ export class DeleteBill {
} as IBillEventDeletingPayload);
// Delete all associated bill entries.
await ItemEntry.query(trx)
await this.itemEntryModel.query(trx)
.where('reference_type', 'Bill')
.where('reference_id', billId)
.delete();

View File

@@ -12,7 +12,8 @@ import { BillDTOTransformer } from './BillDTOTransformer.service';
import { Bill } from '../models/Bill';
import { events } from '@/common/events/events';
import { Vendor } from '@/modules/Vendors/models/Vendor';
import { Knex } from 'knex';
import { TransactionLandedCostEntriesService } from '@/modules/BillLandedCosts/TransactionLandedCostEntries.service';
@Injectable()
export class EditBillService {
@@ -21,7 +22,7 @@ export class EditBillService {
private itemsEntriesService: ItemsEntriesService,
private uow: UnitOfWork,
private eventPublisher: EventEmitter2,
private entriesService: ItemEntries,
private transactionLandedCostEntries: TransactionLandedCostEntriesService,
private transformerDTO: BillDTOTransformer,
@Inject(Bill.name) private billModel: typeof Bill,
@Inject(Vendor.name) private contactModel: typeof Vendor,
@@ -97,12 +98,12 @@ export class EditBillService {
oldBill.paymentAmount
);
// Validate landed cost entries that have allocated cost could not be deleted.
await this.entriesService.validateLandedCostEntriesNotDeleted(
await this.transactionLandedCostEntries.validateLandedCostEntriesNotDeleted(
oldBill.entries,
billObj.entries
);
// Validate new landed cost entries should be bigger than new entries.
await this.entriesService.validateLocatedCostEntriesSmallerThanNewEntries(
await this.transactionLandedCostEntries.validateLocatedCostEntriesSmallerThanNewEntries(
oldBill.entries,
billObj.entries
);