mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
refactor: tenant proxy providers
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '../../CreditNotes/models/CreditNote';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplySyncCredit {
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private creditNoteModel: typeof CreditNote,
|
||||
private creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -20,7 +21,7 @@ export class CreditNoteApplySyncCredit {
|
||||
invoicesAppliedAmount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.creditNoteModel
|
||||
await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.increment('invoicesAmount', invoicesAppliedAmount);
|
||||
@@ -37,7 +38,7 @@ export class CreditNoteApplySyncCredit {
|
||||
invoicesAppliedAmount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> {
|
||||
await this.creditNoteModel
|
||||
await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.decrement('invoicesAmount', invoicesAppliedAmount);
|
||||
|
||||
@@ -4,6 +4,7 @@ import Bluebird from 'bluebird';
|
||||
import { ICreditNoteAppliedToInvoice } from '../types/CreditNoteApplyInvoice.types';
|
||||
import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
|
||||
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplySyncInvoicesCreditedAmount {
|
||||
@@ -12,7 +13,7 @@ export class CreditNoteApplySyncInvoicesCreditedAmount {
|
||||
*/
|
||||
constructor(
|
||||
@Inject(SaleInvoice.name)
|
||||
private readonly saleInvoiceModel: typeof SaleInvoice,
|
||||
private readonly saleInvoiceModel: TenantModelProxy<typeof SaleInvoice>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -22,30 +23,32 @@ export class CreditNoteApplySyncInvoicesCreditedAmount {
|
||||
*/
|
||||
public incrementInvoicesCreditedAmount = async (
|
||||
creditNoteAppliedInvoices: ICreditNoteAppliedToInvoice[],
|
||||
trx?: Knex.Transaction
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
await Bluebird.each(
|
||||
creditNoteAppliedInvoices,
|
||||
(creditNoteAppliedInvoice: CreditNoteAppliedInvoice) => {
|
||||
return this.saleInvoiceModel.query(trx)
|
||||
return this.saleInvoiceModel()
|
||||
.query(trx)
|
||||
.where('id', creditNoteAppliedInvoice.invoiceId)
|
||||
.increment('creditedAmount', creditNoteAppliedInvoice.amount);
|
||||
}
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} invoicesIds
|
||||
* @param {number} amount -
|
||||
* @param {Knex.Transaction} knex -
|
||||
* @param {number} amount -
|
||||
* @param {Knex.Transaction} knex -
|
||||
*/
|
||||
public decrementInvoiceCreditedAmount = async (
|
||||
invoiceId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
await this.saleInvoiceModel.query(trx)
|
||||
await this.saleInvoiceModel()
|
||||
.query(trx)
|
||||
.findById(invoiceId)
|
||||
.decrement('creditedAmount', amount);
|
||||
};
|
||||
|
||||
@@ -16,6 +16,7 @@ import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { CreditNote } from '@/modules/CreditNotes/models/CreditNote';
|
||||
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
|
||||
import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteApplyToInvoices {
|
||||
@@ -32,10 +33,12 @@ export class CreditNoteApplyToInvoices {
|
||||
private readonly creditNoteDTOTransform: CommandCreditNoteDTOTransform,
|
||||
|
||||
@Inject(CreditNoteAppliedInvoice.name)
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
private readonly creditNoteAppliedInvoiceModel: TenantModelProxy<
|
||||
typeof CreditNoteAppliedInvoice
|
||||
>,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -48,7 +51,7 @@ export class CreditNoteApplyToInvoices {
|
||||
applyCreditToInvoicesDTO: IApplyCreditToInvoicesDTO,
|
||||
): Promise<CreditNoteAppliedInvoice[]> {
|
||||
// Saves the credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
@@ -78,9 +81,10 @@ export class CreditNoteApplyToInvoices {
|
||||
// Creates credit note apply to invoice transaction.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Saves the credit note apply to invoice graph to the storage layer.
|
||||
const creditNoteAppliedInvoices = await this.creditNoteAppliedInvoiceModel
|
||||
.query()
|
||||
.insertGraph(creditNoteAppliedModel.entries);
|
||||
const creditNoteAppliedInvoices =
|
||||
await this.creditNoteAppliedInvoiceModel()
|
||||
.query()
|
||||
.insertGraph(creditNoteAppliedModel.entries);
|
||||
|
||||
// Triggers `onCreditNoteApplyToInvoiceCreated` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
|
||||
@@ -8,6 +8,7 @@ import { events } from '@/common/events/events';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { CreditNote } from '../../CreditNotes/models/CreditNote';
|
||||
import { ERRORS } from '../../CreditNotes/constants';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export default class DeleteCreditNoteApplyToInvoices {
|
||||
@@ -20,10 +21,14 @@ export default class DeleteCreditNoteApplyToInvoices {
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
|
||||
@Inject(CreditNoteAppliedInvoice.name)
|
||||
private readonly creditNoteAppliedInvoiceModel: TenantModelProxy<
|
||||
typeof CreditNoteAppliedInvoice
|
||||
>,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -34,15 +39,16 @@ export default class DeleteCreditNoteApplyToInvoices {
|
||||
public deleteApplyCreditNoteToInvoices = async (
|
||||
applyCreditToInvoicesId: number,
|
||||
): Promise<void> => {
|
||||
const creditNoteAppliedToInvoice = await this.creditNoteAppliedInvoiceModel
|
||||
.query()
|
||||
.findById(applyCreditToInvoicesId);
|
||||
const creditNoteAppliedToInvoice =
|
||||
await this.creditNoteAppliedInvoiceModel()
|
||||
.query()
|
||||
.findById(applyCreditToInvoicesId);
|
||||
|
||||
if (!creditNoteAppliedToInvoice) {
|
||||
throw new ServiceError(ERRORS.CREDIT_NOTE_APPLY_TO_INVOICES_NOT_FOUND);
|
||||
}
|
||||
// Retrieve the credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteAppliedToInvoice.creditNoteId)
|
||||
.throwIfNotFound();
|
||||
@@ -50,7 +56,7 @@ export default class DeleteCreditNoteApplyToInvoices {
|
||||
// Creates credit note apply to invoice transaction.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Delete credit note applied to invoices.
|
||||
await this.creditNoteAppliedInvoiceModel
|
||||
await this.creditNoteAppliedInvoiceModel()
|
||||
.query(trx)
|
||||
.findById(applyCreditToInvoicesId)
|
||||
.delete();
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '../../CreditNotes/models/CreditNote';
|
||||
import { ERRORS } from '../../CreditNotes/constants';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteCustomerLinkedCreditNoteService {
|
||||
@@ -10,7 +11,7 @@ export class DeleteCustomerLinkedCreditNoteService {
|
||||
*/
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -18,7 +19,7 @@ export class DeleteCustomerLinkedCreditNoteService {
|
||||
* @param {number} customerId - The customer identifier.
|
||||
*/
|
||||
public async validateCustomerHasNoCreditTransaction(customerId: number) {
|
||||
const associatedCredits = await this.creditNoteModel
|
||||
const associatedCredits = await this.creditNoteModel()
|
||||
.query()
|
||||
.where('customerId', customerId);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { CreditNoteAppliedInvoiceTransformer } from './CreditNoteAppliedInvoiceT
|
||||
import { CreditNote } from '../../CreditNotes/models/CreditNote';
|
||||
import { TransformerInjectable } from '../../Transformer/TransformerInjectable.service';
|
||||
import { CreditNoteAppliedInvoice } from '../models/CreditNoteAppliedInvoice';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetCreditNoteAssociatedAppliedInvoices {
|
||||
@@ -11,9 +12,8 @@ export class GetCreditNoteAssociatedAppliedInvoices {
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote
|
||||
) {
|
||||
}
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve credit note associated invoices to apply.
|
||||
@@ -21,14 +21,16 @@ export class GetCreditNoteAssociatedAppliedInvoices {
|
||||
* @returns {Promise<CreditNoteAppliedInvoice[]>}
|
||||
*/
|
||||
public async getCreditAssociatedAppliedInvoices(
|
||||
creditNoteId: number
|
||||
creditNoteId: number,
|
||||
): Promise<CreditNoteAppliedInvoice[]> {
|
||||
// Retrieve credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel.query()
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const appliedToInvoices = await this.creditNoteAppliedInvoiceModel.query()
|
||||
const appliedToInvoices = await this.creditNoteAppliedInvoiceModel
|
||||
.query()
|
||||
.where('credit_note_id', creditNoteId)
|
||||
.withGraphFetched('saleInvoice')
|
||||
.withGraphFetched('creditNote');
|
||||
@@ -36,7 +38,7 @@ export class GetCreditNoteAssociatedAppliedInvoices {
|
||||
// Transforms credit note applied to invoices.
|
||||
return this.transformer.transform(
|
||||
appliedToInvoices,
|
||||
new CreditNoteAppliedInvoiceTransformer()
|
||||
new CreditNoteAppliedInvoiceTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectab
|
||||
import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
|
||||
import { GetCreditNote } from '../../CreditNotes/queries/GetCreditNote.service';
|
||||
import { CreditNoteWithInvoicesToApplyTransformer } from './CreditNoteWithInvoicesToApplyTransformer';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class GetCreditNoteAssociatedInvoicesToApply {
|
||||
@@ -16,7 +17,7 @@ export class GetCreditNoteAssociatedInvoicesToApply {
|
||||
private getCreditNote: GetCreditNote,
|
||||
|
||||
@Inject(SaleInvoice.name)
|
||||
private saleInvoiceModel: typeof SaleInvoice,
|
||||
private saleInvoiceModel: TenantModelProxy<typeof SaleInvoice>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -31,7 +32,7 @@ export class GetCreditNoteAssociatedInvoicesToApply {
|
||||
const creditNote = await this.getCreditNote.getCreditNote(creditNoteId);
|
||||
|
||||
// Retrieves the published due invoices that associated to the given customer.
|
||||
const saleInvoices = await this.saleInvoiceModel
|
||||
const saleInvoices = await this.saleInvoiceModel()
|
||||
.query()
|
||||
.where('customerId', creditNote.customerId)
|
||||
.modify('dueInvoices')
|
||||
|
||||
Reference in New Issue
Block a user