mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
refactor: credit notes and vendor credits to Nestjs
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
|
||||
import { VendorCreditApplyBillsApplicationService } from './VendorCreditApplyBillsApplication.service';
|
||||
import { IVendorCreditApplyToInvoicesDTO } from './types/VendorCreditApplyBills.types';
|
||||
|
||||
@Controller('vendor-credits')
|
||||
export class VendorCreditApplyBillsController {
|
||||
constructor(
|
||||
private readonly vendorCreditApplyBillsApplication: VendorCreditApplyBillsApplicationService,
|
||||
) {}
|
||||
|
||||
@Get(':vendorCreditId/bills-to-apply')
|
||||
async getVendorCreditToApplyBills(
|
||||
@Param('vendorCreditId') vendorCreditId: number,
|
||||
) {
|
||||
return this.vendorCreditApplyBillsApplication.getVendorCreditToApplyBills(
|
||||
vendorCreditId,
|
||||
);
|
||||
}
|
||||
|
||||
@Post(':vendorCreditId/apply-to-bills')
|
||||
async applyVendorCreditToBills(
|
||||
@Param('vendorCreditId') vendorCreditId: number,
|
||||
@Body() applyCreditToBillsDTO: IVendorCreditApplyToInvoicesDTO,
|
||||
) {
|
||||
return this.vendorCreditApplyBillsApplication.applyVendorCreditToBills(
|
||||
vendorCreditId,
|
||||
applyCreditToBillsDTO,
|
||||
);
|
||||
}
|
||||
|
||||
@Delete('applied-bills/:vendorCreditAppliedBillId')
|
||||
async deleteAppliedBillToVendorCredit(
|
||||
@Param('vendorCreditAppliedBillId') vendorCreditAppliedBillId: number,
|
||||
) {
|
||||
return this.vendorCreditApplyBillsApplication.deleteAppliedBillToVendorCredit(
|
||||
vendorCreditAppliedBillId,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':vendorCreditId/applied-bills')
|
||||
async getAppliedBillsToVendorCredit(
|
||||
@Param('vendorCreditId') vendorCreditId: number,
|
||||
) {
|
||||
return this.vendorCreditApplyBillsApplication.getAppliedBillsToVendorCredit(
|
||||
vendorCreditId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,38 @@
|
||||
|
||||
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ApplyVendorCreditSyncBillsService } from "./command/ApplyVendorCreditSyncBills.service";
|
||||
import { ApplyVendorCreditSyncInvoicedService } from "./command/ApplyVendorCreditSyncInvoiced.service";
|
||||
import { DeleteApplyVendorCreditToBillService } from "./command/DeleteApplyVendorCreditToBill.service";
|
||||
import { ApplyVendorCreditToBillsService } from "./command/ApplyVendorCreditToBills.service";
|
||||
import { GetAppliedBillsToVendorCreditService } from "./queries/GetAppliedBillsToVendorCredit.service";
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ApplyVendorCreditSyncBillsService } from './command/ApplyVendorCreditSyncBills.service';
|
||||
import { ApplyVendorCreditSyncInvoicedService } from './command/ApplyVendorCreditSyncInvoiced.service';
|
||||
import { DeleteApplyVendorCreditToBillService } from './command/DeleteApplyVendorCreditToBill.service';
|
||||
import { ApplyVendorCreditToBillsService } from './command/ApplyVendorCreditToBills.service';
|
||||
import { GetAppliedBillsToVendorCreditService } from './queries/GetAppliedBillsToVendorCredit.service';
|
||||
import { GetVendorCreditToApplyBills } from './queries/GetVendorCreditToApplyBills.service';
|
||||
import { VendorCreditApplyBillsApplicationService } from './VendorCreditApplyBillsApplication.service';
|
||||
import { VendorCreditApplyBillsController } from './VendorCreditApplyBills.controller';
|
||||
import { BillsModule } from '../Bills/Bills.module';
|
||||
import { BillPaymentsModule } from '../BillPayments/BillPayments.module';
|
||||
import { VendorCreditDTOTransformService } from '../VendorCredit/commands/VendorCreditDTOTransform.service';
|
||||
import { ItemsModule } from '../Items/items.module';
|
||||
import { BranchesModule } from '../Branches/Branches.module';
|
||||
import { WarehousesModule } from '../Warehouses/Warehouses.module';
|
||||
import { VendorCreditsModule } from '../VendorCredit/VendorCredits.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
BillsModule,
|
||||
BillPaymentsModule,
|
||||
ItemsModule,
|
||||
BranchesModule,
|
||||
WarehousesModule,
|
||||
VendorCreditsModule
|
||||
],
|
||||
providers: [
|
||||
ApplyVendorCreditSyncBillsService,
|
||||
ApplyVendorCreditSyncInvoicedService,
|
||||
ApplyVendorCreditToBillsService,
|
||||
DeleteApplyVendorCreditToBillService,
|
||||
GetAppliedBillsToVendorCreditService
|
||||
GetAppliedBillsToVendorCreditService,
|
||||
GetVendorCreditToApplyBills,
|
||||
VendorCreditApplyBillsApplicationService,
|
||||
],
|
||||
controllers: [],
|
||||
controllers: [VendorCreditApplyBillsController],
|
||||
})
|
||||
export class VendorCreditApplyBillsModule {}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import { ApplyVendorCreditToBillsService } from './command/ApplyVendorCreditToBills.service';
|
||||
import { DeleteApplyVendorCreditToBillService } from './command/DeleteApplyVendorCreditToBill.service';
|
||||
import { GetAppliedBillsToVendorCreditService } from './queries/GetAppliedBillsToVendorCredit.service';
|
||||
import { GetVendorCreditToApplyBills } from './queries/GetVendorCreditToApplyBills.service';
|
||||
import { IVendorCreditApplyToInvoicesDTO } from './types/VendorCreditApplyBills.types';
|
||||
|
||||
export class VendorCreditApplyBillsApplicationService {
|
||||
/**
|
||||
* @param {ApplyVendorCreditToBillsService} applyVendorCreditToBillsService
|
||||
* @param {DeleteApplyVendorCreditToBillService} deleteApplyVendorCreditToBillService
|
||||
* @param {GetVendorCreditToApplyBills} getVendorCreditToApplyBillsService
|
||||
* @param {GetAppliedBillsToVendorCreditService} getAppliedBillsToVendorCreditService
|
||||
*/
|
||||
constructor(
|
||||
private readonly applyVendorCreditToBillsService: ApplyVendorCreditToBillsService,
|
||||
private readonly deleteApplyVendorCreditToBillService: DeleteApplyVendorCreditToBillService,
|
||||
private readonly getVendorCreditToApplyBillsService: GetVendorCreditToApplyBills,
|
||||
private readonly getAppliedBillsToVendorCreditService: GetAppliedBillsToVendorCreditService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve bills that valid apply to the given vendor credit.
|
||||
* @param {number} vendorCreditId
|
||||
* @returns {Promise<any[]>}
|
||||
*/
|
||||
async getVendorCreditToApplyBills(vendorCreditId: number) {
|
||||
return this.getVendorCreditToApplyBillsService.getCreditToApplyBills(
|
||||
vendorCreditId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply credit note to the given bills.
|
||||
* @param {number} vendorCreditId
|
||||
* @param {IVendorCreditApplyToInvoicesDTO} applyCreditToBillsDTO
|
||||
*/
|
||||
async applyVendorCreditToBills(
|
||||
vendorCreditId: number,
|
||||
applyCreditToBillsDTO: IVendorCreditApplyToInvoicesDTO,
|
||||
) {
|
||||
return this.applyVendorCreditToBillsService.applyVendorCreditToBills(
|
||||
vendorCreditId,
|
||||
applyCreditToBillsDTO,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete applied bill to the given vendor credit.
|
||||
* @param {number} vendorCreditAppliedBillId
|
||||
*/
|
||||
async deleteAppliedBillToVendorCredit(vendorCreditAppliedBillId: number) {
|
||||
return this.deleteApplyVendorCreditToBillService.deleteApplyVendorCreditToBills(
|
||||
vendorCreditAppliedBillId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve applied bills to the given vendor credit.
|
||||
* @param {number} vendorCreditId
|
||||
* @returns {Promise<any[]>}
|
||||
*/
|
||||
async getAppliedBillsToVendorCredit(vendorCreditId: number) {
|
||||
return this.getAppliedBillsToVendorCreditService.getAppliedBills(
|
||||
vendorCreditId,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit';
|
||||
|
||||
@Injectable()
|
||||
export class ApplyVendorCreditSyncInvoicedService {
|
||||
/**
|
||||
* @param {typeof VendorCredit} vendorCreditModel - The vendor credit model.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(VendorCredit.name)
|
||||
private readonly vendorCreditModel: typeof VendorCredit,
|
||||
|
||||
@@ -14,6 +14,8 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { Bill } from '@/modules/Bills/models/Bill';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { events } from '@/common/events/events';
|
||||
import { VendorCreditDTOTransformService } from '@/modules/VendorCredit/commands/VendorCreditDTOTransform.service';
|
||||
|
||||
@Injectable()
|
||||
export class ApplyVendorCreditToBillsService {
|
||||
@@ -28,6 +30,9 @@ export class ApplyVendorCreditToBillsService {
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly billPaymentValidators: BillPaymentValidators,
|
||||
private readonly vendorCreditDTOTransform: VendorCreditDTOTransformService,
|
||||
|
||||
@Inject(VendorCreditAppliedBill.name)
|
||||
private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill,
|
||||
|
||||
@Inject(VendorCredit.name)
|
||||
@@ -68,11 +73,10 @@ export class ApplyVendorCreditToBillsService {
|
||||
vendorCreditAppliedModel.amount,
|
||||
);
|
||||
// Validate vendor credit remaining credit amount.
|
||||
this.validateCreditRemainingAmount(
|
||||
this.vendorCreditDTOTransform.validateCreditRemainingAmount(
|
||||
vendorCredit,
|
||||
vendorCreditAppliedModel.amount,
|
||||
);
|
||||
|
||||
// Saves vendor credit applied to bills under unit-of-work envirement.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Inserts vendor credit applied to bills graph to the storage layer.
|
||||
|
||||
@@ -19,6 +19,8 @@ export class DeleteApplyVendorCreditToBillService {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(VendorCreditAppliedBill.name)
|
||||
private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill,
|
||||
|
||||
@Inject(VendorCredit.name)
|
||||
|
||||
@@ -8,6 +8,8 @@ import { VendorCredit } from '@/modules/VendorCredit/models/VendorCredit';
|
||||
export class GetAppliedBillsToVendorCreditService {
|
||||
constructor(
|
||||
private readonly transformer: TransformerInjectable,
|
||||
|
||||
@Inject(VendorCreditAppliedBill.name)
|
||||
private readonly vendorCreditAppliedBillModel: typeof VendorCreditAppliedBill,
|
||||
|
||||
@Inject(VendorCredit.name)
|
||||
|
||||
@@ -1,61 +1,61 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/objection';
|
||||
import events from '@/subscribers/events';
|
||||
import {
|
||||
IVendorCreditApplyToBillDeletedPayload,
|
||||
IVendorCreditApplyToBillsCreatedPayload,
|
||||
} from '@/interfaces';
|
||||
import { ApplyVendorCreditSyncBillsService } from '../command/ApplyVendorCreditSyncBills.service';
|
||||
import { VendorCreditApplyToBill } from '../models/VendorCreditApplyToBill';
|
||||
// import { Injectable } from '@nestjs/common';
|
||||
// import { InjectModel } from '@nestjs/objection';
|
||||
// import events from '@/subscribers/events';
|
||||
// import {
|
||||
// IVendorCreditApplyToBillDeletedPayload,
|
||||
// IVendorCreditApplyToBillsCreatedPayload,
|
||||
// } from '@/interfaces';
|
||||
// import { ApplyVendorCreditSyncBillsService } from '../command/ApplyVendorCreditSyncBills.service';
|
||||
// import { VendorCreditApplyToBill } from '../models/VendorCreditApplyToBill';
|
||||
|
||||
@Injectable()
|
||||
export default class ApplyVendorCreditSyncBillsSubscriber {
|
||||
constructor(
|
||||
private readonly syncBillsWithVendorCredit: ApplyVendorCreditSyncBillsService,
|
||||
@InjectModel(VendorCreditApplyToBill)
|
||||
private readonly vendorCreditApplyToBillModel: typeof VendorCreditApplyToBill,
|
||||
) {}
|
||||
// @Injectable()
|
||||
// export default class ApplyVendorCreditSyncBillsSubscriber {
|
||||
// constructor(
|
||||
// private readonly syncBillsWithVendorCredit: ApplyVendorCreditSyncBillsService,
|
||||
// @InjectModel(VendorCreditApplyToBill)
|
||||
// private readonly vendorCreditApplyToBillModel: typeof VendorCreditApplyToBill,
|
||||
// ) {}
|
||||
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
*/
|
||||
attach(bus) {
|
||||
bus.subscribe(
|
||||
events.vendorCredit.onApplyToInvoicesCreated,
|
||||
this.incrementAppliedBillsOnceCreditCreated
|
||||
);
|
||||
bus.subscribe(
|
||||
events.vendorCredit.onApplyToInvoicesDeleted,
|
||||
this.decrementAppliedBillsOnceCreditDeleted
|
||||
);
|
||||
}
|
||||
// /**
|
||||
// * Attaches events with handlers.
|
||||
// */
|
||||
// attach(bus) {
|
||||
// bus.subscribe(
|
||||
// events.vendorCredit.onApplyToInvoicesCreated,
|
||||
// this.incrementAppliedBillsOnceCreditCreated
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.vendorCredit.onApplyToInvoicesDeleted,
|
||||
// this.decrementAppliedBillsOnceCreditDeleted
|
||||
// );
|
||||
// }
|
||||
|
||||
/**
|
||||
* Increment credited amount of applied bills once the vendor credit transaction created.
|
||||
* @param {IVendorCreditApplyToBillsCreatedPayload} paylaod -
|
||||
*/
|
||||
private incrementAppliedBillsOnceCreditCreated = async ({
|
||||
vendorCreditAppliedBills,
|
||||
trx,
|
||||
}: IVendorCreditApplyToBillsCreatedPayload) => {
|
||||
await this.syncBillsWithVendorCredit.incrementBillsCreditedAmount(
|
||||
vendorCreditAppliedBills,
|
||||
trx
|
||||
);
|
||||
};
|
||||
// /**
|
||||
// * Increment credited amount of applied bills once the vendor credit transaction created.
|
||||
// * @param {IVendorCreditApplyToBillsCreatedPayload} paylaod -
|
||||
// */
|
||||
// private incrementAppliedBillsOnceCreditCreated = async ({
|
||||
// vendorCreditAppliedBills,
|
||||
// trx,
|
||||
// }: IVendorCreditApplyToBillsCreatedPayload) => {
|
||||
// await this.syncBillsWithVendorCredit.incrementBillsCreditedAmount(
|
||||
// vendorCreditAppliedBills,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
|
||||
/**
|
||||
* Decrement credited amount of applied bills once the vendor credit
|
||||
* transaction delted.
|
||||
* @param {IVendorCreditApplyToBillDeletedPayload} payload
|
||||
*/
|
||||
private decrementAppliedBillsOnceCreditDeleted = async ({
|
||||
oldCreditAppliedToBill,
|
||||
trx,
|
||||
}: IVendorCreditApplyToBillDeletedPayload) => {
|
||||
await this.syncBillsWithVendorCredit.decrementBillCreditedAmount(
|
||||
oldCreditAppliedToBill,
|
||||
trx
|
||||
);
|
||||
};
|
||||
}
|
||||
// /**
|
||||
// * Decrement credited amount of applied bills once the vendor credit
|
||||
// * transaction delted.
|
||||
// * @param {IVendorCreditApplyToBillDeletedPayload} payload
|
||||
// */
|
||||
// private decrementAppliedBillsOnceCreditDeleted = async ({
|
||||
// oldCreditAppliedToBill,
|
||||
// trx,
|
||||
// }: IVendorCreditApplyToBillDeletedPayload) => {
|
||||
// await this.syncBillsWithVendorCredit.decrementBillCreditedAmount(
|
||||
// oldCreditAppliedToBill,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
|
||||
@@ -1,70 +1,70 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { sumBy } from 'lodash';
|
||||
import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
import ApplyVendorCreditSyncInvoiced from '../command/ApplyVendorCreditSyncInvoiced.service';
|
||||
import events from '@/subscribers/events';
|
||||
import {
|
||||
IVendorCreditApplyToBillDeletedPayload,
|
||||
IVendorCreditApplyToBillsCreatedPayload,
|
||||
} from '@/interfaces';
|
||||
// import { Service, Inject } from 'typedi';
|
||||
// import { sumBy } from 'lodash';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
// import ApplyVendorCreditSyncInvoiced from '../command/ApplyVendorCreditSyncInvoiced.service';
|
||||
// import events from '@/subscribers/events';
|
||||
// import {
|
||||
// IVendorCreditApplyToBillDeletedPayload,
|
||||
// IVendorCreditApplyToBillsCreatedPayload,
|
||||
// } from '../types/VendorCreditApplyBills.types';
|
||||
|
||||
@Service()
|
||||
export default class ApplyVendorCreditSyncInvoicedSubscriber {
|
||||
@Inject()
|
||||
tenancy: HasTenancyService;
|
||||
// @Service()
|
||||
// export default class ApplyVendorCreditSyncInvoicedSubscriber {
|
||||
// @Inject()
|
||||
// tenancy: HasTenancyService;
|
||||
|
||||
@Inject()
|
||||
syncCreditWithInvoiced: ApplyVendorCreditSyncInvoiced;
|
||||
// @Inject()
|
||||
// syncCreditWithInvoiced: ApplyVendorCreditSyncInvoiced;
|
||||
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
*/
|
||||
attach(bus) {
|
||||
bus.subscribe(
|
||||
events.vendorCredit.onApplyToInvoicesCreated,
|
||||
this.incrementBillInvoicedOnceCreditApplied
|
||||
);
|
||||
bus.subscribe(
|
||||
events.vendorCredit.onApplyToInvoicesDeleted,
|
||||
this.decrementBillInvoicedOnceCreditApplyDeleted
|
||||
);
|
||||
}
|
||||
// /**
|
||||
// * Attaches events with handlers.
|
||||
// */
|
||||
// attach(bus) {
|
||||
// bus.subscribe(
|
||||
// events.vendorCredit.onApplyToInvoicesCreated,
|
||||
// this.incrementBillInvoicedOnceCreditApplied
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.vendorCredit.onApplyToInvoicesDeleted,
|
||||
// this.decrementBillInvoicedOnceCreditApplyDeleted
|
||||
// );
|
||||
// }
|
||||
|
||||
/**
|
||||
* Increment vendor credit invoiced amount once the apply transaction created.
|
||||
* @param {IVendorCreditApplyToBillsCreatedPayload} payload -
|
||||
*/
|
||||
private incrementBillInvoicedOnceCreditApplied = async ({
|
||||
vendorCredit,
|
||||
tenantId,
|
||||
vendorCreditAppliedBills,
|
||||
trx,
|
||||
}: IVendorCreditApplyToBillsCreatedPayload) => {
|
||||
const amount = sumBy(vendorCreditAppliedBills, 'amount');
|
||||
// /**
|
||||
// * Increment vendor credit invoiced amount once the apply transaction created.
|
||||
// * @param {IVendorCreditApplyToBillsCreatedPayload} payload -
|
||||
// */
|
||||
// private incrementBillInvoicedOnceCreditApplied = async ({
|
||||
// vendorCredit,
|
||||
// tenantId,
|
||||
// vendorCreditAppliedBills,
|
||||
// trx,
|
||||
// }: IVendorCreditApplyToBillsCreatedPayload) => {
|
||||
// const amount = sumBy(vendorCreditAppliedBills, 'amount');
|
||||
|
||||
await this.syncCreditWithInvoiced.incrementVendorCreditInvoicedAmount(
|
||||
tenantId,
|
||||
vendorCredit.id,
|
||||
amount,
|
||||
trx
|
||||
);
|
||||
};
|
||||
// await this.syncCreditWithInvoiced.incrementVendorCreditInvoicedAmount(
|
||||
// tenantId,
|
||||
// vendorCredit.id,
|
||||
// amount,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
|
||||
/**
|
||||
* Decrement vendor credit invoiced amount once the apply transaction deleted.
|
||||
* @param {IVendorCreditApplyToBillDeletedPayload} payload -
|
||||
*/
|
||||
private decrementBillInvoicedOnceCreditApplyDeleted = async ({
|
||||
tenantId,
|
||||
vendorCredit,
|
||||
oldCreditAppliedToBill,
|
||||
trx,
|
||||
}: IVendorCreditApplyToBillDeletedPayload) => {
|
||||
await this.syncCreditWithInvoiced.decrementVendorCreditInvoicedAmount(
|
||||
tenantId,
|
||||
oldCreditAppliedToBill.vendorCreditId,
|
||||
oldCreditAppliedToBill.amount,
|
||||
trx
|
||||
);
|
||||
};
|
||||
}
|
||||
// /**
|
||||
// * Decrement vendor credit invoiced amount once the apply transaction deleted.
|
||||
// * @param {IVendorCreditApplyToBillDeletedPayload} payload -
|
||||
// */
|
||||
// private decrementBillInvoicedOnceCreditApplyDeleted = async ({
|
||||
// tenantId,
|
||||
// vendorCredit,
|
||||
// oldCreditAppliedToBill,
|
||||
// trx,
|
||||
// }: IVendorCreditApplyToBillDeletedPayload) => {
|
||||
// await this.syncCreditWithInvoiced.decrementVendorCreditInvoicedAmount(
|
||||
// tenantId,
|
||||
// oldCreditAppliedToBill.vendorCreditId,
|
||||
// oldCreditAppliedToBill.amount,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user