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

@@ -23,7 +23,7 @@ export class BillsApplication {
private deleteBillService: DeleteBill,
private getDueBillsService: GetDueBills,
private openBillService: OpenBillService,
private getBillPaymentsService: GetBillPayments,
// private getBillPaymentsService: GetBillPayments,
) {}
/**
@@ -99,7 +99,7 @@ export class BillsApplication {
* @param {number} tenantId
* @param {number} billId
*/
public getBillPayments(billId: number) {
return this.getBillPaymentsService.getBillPayments(billId);
}
// public getBillPayments(billId: number) {
// return this.getBillPaymentsService.getBillPayments(billId);
// }
}

View File

@@ -0,0 +1,48 @@
import {
Controller,
Post,
Body,
Put,
Param,
Delete,
Get,
} from '@nestjs/common';
import { BillsApplication } from './Bills.application';
import { IBillDTO, IBillEditDTO } from './Bills.types';
import { PublicRoute } from '../Auth/Jwt.guard';
@Controller('bills')
@PublicRoute()
export class BillsController {
constructor(private billsApplication: BillsApplication) {}
@Post()
createBill(@Body() billDTO: IBillDTO) {
return this.billsApplication.createBill(billDTO);
}
@Put(':id')
editBill(@Param('id') billId: number, @Body() billDTO: IBillEditDTO) {
return this.billsApplication.editBill(billId, billDTO);
}
@Delete(':id')
deleteBill(@Param('id') billId: number) {
return this.billsApplication.deleteBill(billId);
}
@Get(':id')
getBill(@Param('id') billId: number) {
return this.billsApplication.getBill(billId);
}
@Post(':id/open')
openBill(@Param('id') billId: number) {
return this.billsApplication.openBill(billId);
}
@Get('due')
getDueBills(@Body('vendorId') vendorId?: number) {
return this.billsApplication.getDueBills(vendorId);
}
}

View File

@@ -4,14 +4,40 @@ import { CreateBill } from './commands/CreateBill.service';
import { DeleteBill } from './commands/DeleteBill.service';
import { GetBill } from './queries/GetBill';
import { BillDTOTransformer } from './commands/BillDTOTransformer.service';
import { EditBillService } from './commands/EditBill.service';
import { GetDueBills } from './queries/GetDueBills.service';
import { OpenBillService } from './commands/OpenBill.service';
import { BillsValidators } from './commands/BillsValidators.service';
import { ItemsEntriesService } from '../Items/ItemsEntries.service';
import { BranchTransactionDTOTransformer } from '../Branches/integrations/BranchTransactionDTOTransform';
import { BranchesSettingsService } from '../Branches/BranchesSettings';
import { WarehouseTransactionDTOTransform } from '../Warehouses/Integrations/WarehouseTransactionDTOTransform';
import { WarehousesSettings } from '../Warehouses/WarehousesSettings';
import { ItemEntriesTaxTransactions } from '../TaxRates/ItemEntriesTaxTransactions.service';
import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { BillsController } from './Bills.controller';
import { BillLandedCostsModule } from '../BillLandedCosts/BillLandedCosts.module';
@Module({
imports: [BillLandedCostsModule],
providers: [
TenancyContext,
BillsApplication,
BranchTransactionDTOTransformer,
WarehouseTransactionDTOTransform,
WarehousesSettings,
ItemEntriesTaxTransactions,
BranchesSettingsService,
CreateBill,
EditBillService,
GetDueBills,
OpenBillService,
GetBill,
DeleteBill,
BillDTOTransformer,
BillsValidators,
ItemsEntriesService
],
controllers: [BillsController],
})
export class BillsModule {}

View File

@@ -53,7 +53,7 @@ export interface IBillCreatedPayload {
// tenantId: number;
bill: Bill;
billDTO: IBillDTO;
billId: number;
// billId: number;
trx?: Knex.Transaction;
}

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
);

View File

@@ -8,6 +8,7 @@ import moment from 'moment';
// import { DEFAULT_VIEWS } from '@/services/Purchases/Bills/constants';
// import ModelSearchable from './ModelSearchable';
import { BaseModel } from '@/models/Model';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
export class Bill extends BaseModel{
public amount: number;
@@ -31,9 +32,14 @@ export class Bill extends BaseModel{
public openedAt: Date | string;
public userId: number;
public branchId: number;
public warehouseId: number;
public createdAt: Date;
public updatedAt: Date | null;
public entries?: ItemEntry[];
/**
* Timestamps columns.
*/
@@ -410,125 +416,125 @@ export class Bill extends BaseModel{
/**
* Relationship mapping.
*/
static get relationMappings() {
const Vendor = require('models/Vendor');
const ItemEntry = require('models/ItemEntry');
const BillLandedCost = require('models/BillLandedCost');
const Branch = require('models/Branch');
const Warehouse = require('models/Warehouse');
const TaxRateTransaction = require('models/TaxRateTransaction');
const Document = require('models/Document');
const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
// static get relationMappings() {
// const Vendor = require('models/Vendor');
// const ItemEntry = require('models/ItemEntry');
// const BillLandedCost = require('models/BillLandedCost');
// const Branch = require('models/Branch');
// const Warehouse = require('models/Warehouse');
// const TaxRateTransaction = require('models/TaxRateTransaction');
// const Document = require('models/Document');
// const { MatchedBankTransaction } = require('models/MatchedBankTransaction');
return {
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: Vendor.default,
join: {
from: 'bills.vendorId',
to: 'contacts.id',
},
filter(query) {
query.where('contact_service', 'vendor');
},
},
// return {
// vendor: {
// relation: Model.BelongsToOneRelation,
// modelClass: Vendor.default,
// join: {
// from: 'bills.vendorId',
// to: 'contacts.id',
// },
// filter(query) {
// query.where('contact_service', 'vendor');
// },
// },
entries: {
relation: Model.HasManyRelation,
modelClass: ItemEntry.default,
join: {
from: 'bills.id',
to: 'items_entries.referenceId',
},
filter(builder) {
builder.where('reference_type', 'Bill');
builder.orderBy('index', 'ASC');
},
},
// entries: {
// relation: Model.HasManyRelation,
// modelClass: ItemEntry.default,
// join: {
// from: 'bills.id',
// to: 'items_entries.referenceId',
// },
// filter(builder) {
// builder.where('reference_type', 'Bill');
// builder.orderBy('index', 'ASC');
// },
// },
locatedLandedCosts: {
relation: Model.HasManyRelation,
modelClass: BillLandedCost.default,
join: {
from: 'bills.id',
to: 'bill_located_costs.billId',
},
},
// locatedLandedCosts: {
// relation: Model.HasManyRelation,
// modelClass: BillLandedCost.default,
// join: {
// from: 'bills.id',
// to: 'bill_located_costs.billId',
// },
// },
/**
* Bill may belongs to associated branch.
*/
branch: {
relation: Model.BelongsToOneRelation,
modelClass: Branch.default,
join: {
from: 'bills.branchId',
to: 'branches.id',
},
},
// /**
// * Bill may belongs to associated branch.
// */
// branch: {
// relation: Model.BelongsToOneRelation,
// modelClass: Branch.default,
// join: {
// from: 'bills.branchId',
// to: 'branches.id',
// },
// },
/**
* Bill may has associated warehouse.
*/
warehouse: {
relation: Model.BelongsToOneRelation,
modelClass: Warehouse.default,
join: {
from: 'bills.warehouseId',
to: 'warehouses.id',
},
},
// /**
// * Bill may has associated warehouse.
// */
// warehouse: {
// relation: Model.BelongsToOneRelation,
// modelClass: Warehouse.default,
// join: {
// from: 'bills.warehouseId',
// to: 'warehouses.id',
// },
// },
/**
* Bill may has associated tax rate transactions.
*/
taxes: {
relation: Model.HasManyRelation,
modelClass: TaxRateTransaction.default,
join: {
from: 'bills.id',
to: 'tax_rate_transactions.referenceId',
},
filter(builder) {
builder.where('reference_type', 'Bill');
},
},
// /**
// * Bill may has associated tax rate transactions.
// */
// taxes: {
// relation: Model.HasManyRelation,
// modelClass: TaxRateTransaction.default,
// join: {
// from: 'bills.id',
// to: 'tax_rate_transactions.referenceId',
// },
// filter(builder) {
// builder.where('reference_type', 'Bill');
// },
// },
/**
* Bill may has many attached attachments.
*/
attachments: {
relation: Model.ManyToManyRelation,
modelClass: Document.default,
join: {
from: 'bills.id',
through: {
from: 'document_links.modelId',
to: 'document_links.documentId',
},
to: 'documents.id',
},
filter(query) {
query.where('model_ref', 'Bill');
},
},
// /**
// * Bill may has many attached attachments.
// */
// attachments: {
// relation: Model.ManyToManyRelation,
// modelClass: Document.default,
// join: {
// from: 'bills.id',
// through: {
// from: 'document_links.modelId',
// to: 'document_links.documentId',
// },
// to: 'documents.id',
// },
// filter(query) {
// query.where('model_ref', 'Bill');
// },
// },
/**
* Bill may belongs to matched bank transaction.
*/
matchedBankTransaction: {
relation: Model.HasManyRelation,
modelClass: MatchedBankTransaction,
join: {
from: 'bills.id',
to: 'matched_bank_transactions.referenceId',
},
filter(query) {
query.where('reference_type', 'Bill');
},
},
};
}
// /**
// * Bill may belongs to matched bank transaction.
// */
// matchedBankTransaction: {
// relation: Model.HasManyRelation,
// modelClass: MatchedBankTransaction,
// join: {
// from: 'bills.id',
// to: 'matched_bank_transactions.referenceId',
// },
// filter(query) {
// query.where('reference_type', 'Bill');
// },
// },
// };
// }
/**
* Retrieve the not found bills ids as array that associated to the given vendor.

View File

@@ -1,9 +1,12 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { Bill } from '../models/Bill';
@Injectable()
export class GetDueBills {
constructor(private billModel: typeof Bill) {}
constructor(
@Inject(Bill.name)
private billModel: typeof Bill,
) {}
/**
* Retrieve all due bills or for specific given vendor id.