mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
refactor: tenant proxy providers
This commit is contained in:
@@ -12,6 +12,7 @@ import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreateCreditNoteService {
|
||||
@@ -30,10 +31,10 @@ export class CreateCreditNoteService {
|
||||
private readonly commandCreditNoteDTOTransform: CommandCreditNoteDTOTransform,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
|
||||
@Inject(Contact.name)
|
||||
private readonly contactModel: typeof Contact,
|
||||
private readonly contactModel: TenantModelProxy<typeof Contact>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -49,7 +50,7 @@ export class CreateCreditNoteService {
|
||||
creditNoteDTO,
|
||||
});
|
||||
// Validate customer existance.
|
||||
const customer = await this.contactModel
|
||||
const customer = await this.contactModel()
|
||||
.query()
|
||||
.modify('customer')
|
||||
.findById(creditNoteDTO.customerId)
|
||||
@@ -78,9 +79,11 @@ export class CreateCreditNoteService {
|
||||
} as ICreditNoteCreatingPayload);
|
||||
|
||||
// Upsert the credit note graph.
|
||||
const creditNote = await this.creditNoteModel.query(trx).upsertGraph({
|
||||
...creditNoteModel,
|
||||
});
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.upsertGraph({
|
||||
...creditNoteModel,
|
||||
});
|
||||
// Triggers `onCreditNoteCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onCreated, {
|
||||
creditNoteDTO,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Inject, Injectable } from '@nestjs/common';
|
||||
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class CreditNoteGLEntries {
|
||||
@@ -12,7 +13,7 @@ export class CreditNoteGLEntries {
|
||||
private readonly accountRepository: AccountRepository,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,14 +7,13 @@ import {
|
||||
import { ERRORS } from '../constants';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
import { CreditNoteAppliedInvoice } from '../../CreditNotesApplyInvoice/models/CreditNoteAppliedInvoice';
|
||||
import {
|
||||
RefundCreditNote as RefundCreditNoteModel,
|
||||
} from '../../CreditNoteRefunds/models/RefundCreditNote';
|
||||
import { RefundCreditNote as RefundCreditNoteModel } from '../../CreditNoteRefunds/models/RefundCreditNote';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteCreditNoteService {
|
||||
@@ -31,16 +30,20 @@ export class DeleteCreditNoteService {
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
|
||||
@Inject(ItemEntry.name)
|
||||
private readonly itemEntryModel: typeof ItemEntry,
|
||||
private readonly itemEntryModel: TenantModelProxy<typeof ItemEntry>,
|
||||
|
||||
@Inject(CreditNoteAppliedInvoice.name)
|
||||
private readonly creditNoteAppliedInvoiceModel: typeof CreditNoteAppliedInvoice,
|
||||
private readonly creditNoteAppliedInvoiceModel: TenantModelProxy<
|
||||
typeof CreditNoteAppliedInvoice
|
||||
>,
|
||||
|
||||
@Inject(RefundCreditNoteModel.name)
|
||||
private readonly refundCreditNoteModel: typeof RefundCreditNoteModel,
|
||||
private readonly refundCreditNoteModel: TenantModelProxy<
|
||||
typeof RefundCreditNoteModel
|
||||
>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -50,7 +53,7 @@ export class DeleteCreditNoteService {
|
||||
*/
|
||||
public async deleteCreditNote(creditNoteId: number): Promise<void> {
|
||||
// Retrieve the credit note or throw not found service error.
|
||||
const oldCreditNote = await this.creditNoteModel
|
||||
const oldCreditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
@@ -70,14 +73,14 @@ export class DeleteCreditNoteService {
|
||||
} as ICreditNoteDeletingPayload);
|
||||
|
||||
// Deletes the associated credit note entries.
|
||||
await this.itemEntryModel
|
||||
await this.itemEntryModel()
|
||||
.query(trx)
|
||||
.where('reference_id', creditNoteId)
|
||||
.where('reference_type', 'CreditNote')
|
||||
.delete();
|
||||
|
||||
// Deletes the credit note transaction.
|
||||
await this.creditNoteModel.query(trx).findById(creditNoteId).delete();
|
||||
await this.creditNoteModel().query(trx).findById(creditNoteId).delete();
|
||||
|
||||
// Triggers `onCreditNoteDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onDeleted, {
|
||||
@@ -96,7 +99,7 @@ export class DeleteCreditNoteService {
|
||||
private async validateCreditNoteHasNoRefundTransactions(
|
||||
creditNoteId: number,
|
||||
): Promise<void> {
|
||||
const refundTransactions = await this.refundCreditNoteModel
|
||||
const refundTransactions = await this.refundCreditNoteModel()
|
||||
.query()
|
||||
.where('creditNoteId', creditNoteId);
|
||||
|
||||
@@ -113,7 +116,7 @@ export class DeleteCreditNoteService {
|
||||
private async validateCreditNoteHasNoApplyInvoiceTransactions(
|
||||
creditNoteId: number,
|
||||
): Promise<void> {
|
||||
const appliedTransactions = await this.creditNoteAppliedInvoiceModel
|
||||
const appliedTransactions = await this.creditNoteAppliedInvoiceModel()
|
||||
.query()
|
||||
.where('creditNoteId', creditNoteId);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { UnitOfWork } from '../../Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
import { CommandCreditNoteDTOTransform } from './CommandCreditNoteDTOTransform.service';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class EditCreditNoteService {
|
||||
@@ -24,8 +25,11 @@ export class EditCreditNoteService {
|
||||
* @param {UnitOfWork} uow - The unit of work.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(CreditNote.name) private creditNoteModel: typeof CreditNote,
|
||||
@Inject(Contact.name) private contactModel: typeof Contact,
|
||||
@Inject(CreditNote.name)
|
||||
private creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
@Inject(Contact.name)
|
||||
private contactModel: TenantModelProxy<typeof Contact>,
|
||||
|
||||
private commandCreditNoteDTOTransform: CommandCreditNoteDTOTransform,
|
||||
private itemsEntriesService: ItemsEntriesService,
|
||||
private eventPublisher: EventEmitter2,
|
||||
@@ -41,13 +45,13 @@ export class EditCreditNoteService {
|
||||
creditNoteEditDTO: ICreditNoteEditDTO,
|
||||
) {
|
||||
// Retrieve the sale invoice or throw not found service error.
|
||||
const oldCreditNote = await this.creditNoteModel
|
||||
const oldCreditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate customer existance.
|
||||
const customer = await this.contactModel
|
||||
const customer = await this.contactModel()
|
||||
.query()
|
||||
.findById(creditNoteEditDTO.customerId);
|
||||
|
||||
@@ -82,10 +86,12 @@ export class EditCreditNoteService {
|
||||
} as ICreditNoteEditingPayload);
|
||||
|
||||
// Saves the credit note graph to the storage.
|
||||
const creditNote = await this.creditNoteModel.query(trx).upsertGraph({
|
||||
id: creditNoteId,
|
||||
...creditNoteModel,
|
||||
});
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.upsertGraph({
|
||||
id: creditNoteId,
|
||||
...creditNoteModel,
|
||||
});
|
||||
// Triggers `onCreditNoteEdited` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onEdited, {
|
||||
trx,
|
||||
|
||||
@@ -10,6 +10,7 @@ import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
import { events } from '@/common/events/events';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class OpenCreditNoteService {
|
||||
@@ -23,7 +24,7 @@ export class OpenCreditNoteService {
|
||||
private readonly uow: UnitOfWork,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: typeof CreditNote,
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -33,7 +34,7 @@ export class OpenCreditNoteService {
|
||||
*/
|
||||
public openCreditNote = async (creditNoteId: number): Promise<CreditNote> => {
|
||||
// Retrieve the sale invoice or throw not found service error.
|
||||
const oldCreditNote = await this.creditNoteModel
|
||||
const oldCreditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
@@ -59,7 +60,7 @@ export class OpenCreditNoteService {
|
||||
eventPayload,
|
||||
);
|
||||
// Saves the credit note graph to the storage.
|
||||
const creditNote = await this.creditNoteModel
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.updateAndFetchById(creditNoteId, {
|
||||
openedAt: new Date(),
|
||||
|
||||
Reference in New Issue
Block a user