mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
ICreditNoteRefundDTO,
|
||||
IRefundCreditNoteCreatedPayload,
|
||||
IRefundCreditNoteCreatingPayload,
|
||||
} from '../types/CreditNoteRefunds.types';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { RefundCreditNote } from '@/modules/CreditNoteRefunds/models/RefundCreditNote';
|
||||
import { CommandCreditNoteDTOTransform } from '@/modules/CreditNotes/commands/CommandCreditNoteDTOTransform.service';
|
||||
import { CreditNote } from '@/modules/CreditNotes/models/CreditNote';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
import { CreditNoteRefundDto } from '../dto/CreditNoteRefund.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CreateRefundCreditNoteService {
|
||||
/**
|
||||
* @param {UnitOfWork} uow - The unit of work service.
|
||||
* @param {EventEmitter2} eventPublisher - The event emitter service.
|
||||
* @param {CommandCreditNoteDTOTransform} commandCreditNoteDTOTransform - The command credit note DTO transform service.
|
||||
* @param {TenantModelProxy<typeof RefundCreditNote>} refundCreditNoteModel - The refund credit note model.
|
||||
* @param {TenantModelProxy<typeof Account>} accountModel - The account model.
|
||||
* @param {TenantModelProxy<typeof CreditNote>} creditNoteModel - The credit note model.
|
||||
*/
|
||||
constructor(
|
||||
private uow: UnitOfWork,
|
||||
private eventPublisher: EventEmitter2,
|
||||
private commandCreditNoteDTOTransform: CommandCreditNoteDTOTransform,
|
||||
|
||||
@Inject(RefundCreditNote.name)
|
||||
private refundCreditNoteModel: TenantModelProxy<typeof RefundCreditNote>,
|
||||
|
||||
@Inject(Account.name)
|
||||
private accountModel: TenantModelProxy<typeof Account>,
|
||||
|
||||
@Inject(CreditNote.name)
|
||||
private creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} creditNoteId
|
||||
* @param {ICreditNoteRefundDTO} newCreditNoteDTO
|
||||
* @returns {Promise<IRefundCreditNote>}
|
||||
*/
|
||||
public async createCreditNoteRefund(
|
||||
creditNoteId: number,
|
||||
newCreditNoteDTO: CreditNoteRefundDto,
|
||||
): Promise<RefundCreditNote> {
|
||||
// Retrieve the credit note or throw not found service error.
|
||||
const creditNote = await this.creditNoteModel()
|
||||
.query()
|
||||
.findById(creditNoteId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Retrieve the withdrawal account or throw not found service error.
|
||||
const fromAccount = await this.accountModel()
|
||||
.query()
|
||||
.findById(newCreditNoteDTO.fromAccountId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Validate the credit note remaining amount.
|
||||
this.commandCreditNoteDTOTransform?.validateCreditRemainingAmount(
|
||||
creditNote,
|
||||
newCreditNoteDTO.amount,
|
||||
);
|
||||
// Validate the refund withdrawal account type.
|
||||
// this.commandCreditNoteDTOTransform.validateRefundWithdrawwalAccountType(
|
||||
// fromAccount,
|
||||
// );
|
||||
// Creates a refund credit note transaction.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
// Triggers `onCreditNoteRefundCreating` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundCreating, {
|
||||
trx,
|
||||
creditNote,
|
||||
newCreditNoteDTO,
|
||||
} as IRefundCreditNoteCreatingPayload);
|
||||
|
||||
// Stores the refund credit note graph to the storage layer.
|
||||
const refundCreditNote = await this.refundCreditNoteModel()
|
||||
.query(trx)
|
||||
.insertAndFetch({
|
||||
...this.transformDTOToModel(creditNote, newCreditNoteDTO),
|
||||
});
|
||||
// Triggers `onCreditNoteRefundCreated` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundCreated, {
|
||||
trx,
|
||||
refundCreditNote,
|
||||
creditNote,
|
||||
} as IRefundCreditNoteCreatedPayload);
|
||||
|
||||
return refundCreditNote;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transformes the refund credit note DTO to model.
|
||||
* @param {CreditNote} creditNote - The credit note.
|
||||
* @param {CreditNoteRefundDto} creditNoteDTO - The credit note refund DTO.
|
||||
* @returns {Partial<RefundCreditNote>}
|
||||
*/
|
||||
private transformDTOToModel = (
|
||||
creditNote: CreditNote,
|
||||
creditNoteDTO: CreditNoteRefundDto,
|
||||
): Partial<RefundCreditNote> => {
|
||||
return {
|
||||
creditNoteId: creditNote.id,
|
||||
currencyCode: creditNote.currencyCode,
|
||||
...creditNoteDTO,
|
||||
exchangeRate: creditNoteDTO.exchangeRate || 1,
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Knex } from 'knex';
|
||||
import {
|
||||
IRefundCreditNoteDeletedPayload,
|
||||
IRefundCreditNoteDeletingPayload,
|
||||
} from '../types/CreditNoteRefunds.types';
|
||||
import { RefundCreditNote } from '../models/RefundCreditNote';
|
||||
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
|
||||
import { events } from '@/common/events/events';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class DeleteRefundCreditNoteService {
|
||||
/**
|
||||
* @param {UnitOfWork} uow - Unit of work.
|
||||
* @param {EventEmitter2} eventPublisher - Event emitter.
|
||||
* @param {typeof RefundCreditNote} refundCreditNoteModel - Refund credit note model.
|
||||
*/
|
||||
constructor(
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly eventPublisher: EventEmitter2,
|
||||
|
||||
@Inject(RefundCreditNote.name)
|
||||
private readonly refundCreditNoteModel: TenantModelProxy<
|
||||
typeof RefundCreditNote
|
||||
>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} refundCreditId - Refund credit note ID.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
public deleteCreditNoteRefund = async (refundCreditId: number) => {
|
||||
// Retrieve the old credit note or throw not found service error.
|
||||
const oldRefundCredit = await this.refundCreditNoteModel()
|
||||
.query()
|
||||
.findById(refundCreditId)
|
||||
.throwIfNotFound();
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleted` event.
|
||||
await this.eventPublisher.emitAsync(events.creditNote.onRefundDelete, {
|
||||
refundCreditId,
|
||||
oldRefundCredit,
|
||||
} as IRefundCreditNoteDeletedPayload);
|
||||
|
||||
// Deletes refund credit note transactions with associated entries.
|
||||
return this.uow.withTransaction(async (trx: Knex.Transaction) => {
|
||||
const eventPayload = {
|
||||
trx,
|
||||
refundCreditId,
|
||||
oldRefundCredit,
|
||||
} as IRefundCreditNoteDeletedPayload | IRefundCreditNoteDeletingPayload;
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleting` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onRefundDeleting,
|
||||
eventPayload,
|
||||
);
|
||||
// Deletes the refund credit note graph from the storage.
|
||||
await this.refundCreditNoteModel()
|
||||
.query(trx)
|
||||
.findById(refundCreditId)
|
||||
.delete();
|
||||
|
||||
// Triggers `onCreditNoteRefundDeleted` event.
|
||||
await this.eventPublisher.emitAsync(
|
||||
events.creditNote.onRefundDeleted,
|
||||
eventPayload as IRefundCreditNoteDeletedPayload,
|
||||
);
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ERRORS } from '../../CreditNotes/constants';
|
||||
import { RefundCreditNote } from '../models/RefundCreditNote';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { Account } from '@/modules/Accounts/models/Account.model';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class RefundCreditNoteService {
|
||||
/**
|
||||
* @param {TenantModelProxy<typeof RefundCreditNote>} refundCreditNoteModel - The refund credit note model.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(RefundCreditNote.name)
|
||||
private readonly refundCreditNoteModel: TenantModelProxy<
|
||||
typeof RefundCreditNote
|
||||
>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Retrieve the credit note graph.
|
||||
* @param {number} refundCreditId
|
||||
* @returns {Promise<RefundCreditNote>}
|
||||
*/
|
||||
public getCreditNoteRefundOrThrowError = async (
|
||||
refundCreditId: number,
|
||||
): Promise<RefundCreditNote> => {
|
||||
const refundCreditNote = await this.refundCreditNoteModel()
|
||||
.query()
|
||||
.findById(refundCreditId);
|
||||
if (!refundCreditNote) {
|
||||
throw new ServiceError(ERRORS.REFUND_CREDIT_NOTE_NOT_FOUND);
|
||||
}
|
||||
return refundCreditNote;
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate the refund account type.
|
||||
* @param {Account} account
|
||||
*/
|
||||
public validateRefundWithdrawwalAccountType = (account: Account): void => {
|
||||
const supportedTypes = ['bank', 'cash', 'fixed-asset'];
|
||||
|
||||
if (supportedTypes.indexOf(account.accountType) === -1) {
|
||||
throw new ServiceError(ERRORS.ACCOUNT_INVALID_TYPE);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { AccountNormal, ILedgerEntry, IRefundCreditNote } from '@/interfaces';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
||||
// import Ledger from '@/services/Accounting/Ledger';
|
||||
|
||||
// @Service()
|
||||
// export default class RefundCreditNoteGLEntries {
|
||||
// @Inject()
|
||||
// ledgerStorage: LedgerStorageService;
|
||||
|
||||
// @Inject()
|
||||
// tenancy: HasTenancyService;
|
||||
|
||||
// /**
|
||||
// * Retrieves the refund credit common GL entry.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @returns
|
||||
// */
|
||||
// private getRefundCreditCommonGLEntry = (
|
||||
// refundCreditNote: IRefundCreditNote
|
||||
// ) => {
|
||||
// return {
|
||||
// currencyCode: refundCreditNote.currencyCode,
|
||||
// exchangeRate: refundCreditNote.exchangeRate,
|
||||
|
||||
// transactionType: 'RefundCreditNote',
|
||||
// transactionId: refundCreditNote.id,
|
||||
// date: refundCreditNote.date,
|
||||
// userId: refundCreditNote.userId,
|
||||
|
||||
// referenceNumber: refundCreditNote.referenceNo,
|
||||
|
||||
// createdAt: refundCreditNote.createdAt,
|
||||
// indexGroup: 10,
|
||||
|
||||
// credit: 0,
|
||||
// debit: 0,
|
||||
|
||||
// note: refundCreditNote.description,
|
||||
// branchId: refundCreditNote.branchId,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the refudn credit receivable GL entry.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {number} ARAccountId
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getRefundCreditGLReceivableEntry = (
|
||||
// refundCreditNote: IRefundCreditNote,
|
||||
// ARAccountId: number
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// debit: refundCreditNote.amount,
|
||||
// accountId: ARAccountId,
|
||||
// contactId: refundCreditNote.creditNote.customerId,
|
||||
// index: 1,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the refund credit withdrawal GL entry.
|
||||
// * @param {number} refundCreditNote
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getRefundCreditGLWithdrawalEntry = (
|
||||
// refundCreditNote: IRefundCreditNote
|
||||
// ): ILedgerEntry => {
|
||||
// const commonEntry = this.getRefundCreditCommonGLEntry(refundCreditNote);
|
||||
|
||||
// return {
|
||||
// ...commonEntry,
|
||||
// credit: refundCreditNote.amount,
|
||||
// accountId: refundCreditNote.fromAccountId,
|
||||
// index: 2,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieve the refund credit note GL entries.
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {number} receivableAccount
|
||||
// * @returns {ILedgerEntry[]}
|
||||
// */
|
||||
// public getRefundCreditGLEntries(
|
||||
// refundCreditNote: IRefundCreditNote,
|
||||
// ARAccountId: number
|
||||
// ): ILedgerEntry[] {
|
||||
// const receivableEntry = this.getRefundCreditGLReceivableEntry(
|
||||
// refundCreditNote,
|
||||
// ARAccountId
|
||||
// );
|
||||
// const withdrawalEntry =
|
||||
// this.getRefundCreditGLWithdrawalEntry(refundCreditNote);
|
||||
|
||||
// return [receivableEntry, withdrawalEntry];
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Creates refund credit GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {IRefundCreditNote} refundCreditNote
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public createRefundCreditGLEntries = async (
|
||||
// tenantId: number,
|
||||
// refundCreditNoteId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// const { Account, RefundCreditNote } = this.tenancy.models(tenantId);
|
||||
|
||||
// // Retrieve the refund with associated credit note.
|
||||
// const refundCreditNote = await RefundCreditNote.query(trx)
|
||||
// .findById(refundCreditNoteId)
|
||||
// .withGraphFetched('creditNote');
|
||||
|
||||
// // Receivable account A/R.
|
||||
// const receivableAccount = await Account.query().findOne(
|
||||
// 'slug',
|
||||
// 'accounts-receivable'
|
||||
// );
|
||||
// // Retrieve refund credit GL entries.
|
||||
// const refundGLEntries = this.getRefundCreditGLEntries(
|
||||
// refundCreditNote,
|
||||
// receivableAccount.id
|
||||
// );
|
||||
// const ledger = new Ledger(refundGLEntries);
|
||||
|
||||
// // Saves refund ledger entries.
|
||||
// await this.ledgerStorage.commit(tenantId, ledger, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Reverts refund credit note GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} refundCreditNoteId
|
||||
// * @param {number} receivableAccount
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public revertRefundCreditGLEntries = async (
|
||||
// tenantId: number,
|
||||
// refundCreditNoteId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// await this.ledgerStorage.deleteByReference(
|
||||
// tenantId,
|
||||
// refundCreditNoteId,
|
||||
// 'RefundCreditNote',
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Knex } from 'knex';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { CreditNote } from '@/modules/CreditNotes/models/CreditNote';
|
||||
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
|
||||
|
||||
@Injectable()
|
||||
export class RefundSyncCreditNoteBalanceService {
|
||||
/**
|
||||
* @param {TenantModelProxy<typeof CreditNote>} creditNoteModel - The credit note model.
|
||||
*/
|
||||
constructor(
|
||||
@Inject(CreditNote.name)
|
||||
private readonly creditNoteModel: TenantModelProxy<typeof CreditNote>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Increments the refund amount of the credit note.
|
||||
* @param {number} creditNoteId - The credit note ID.
|
||||
* @param {number} amount - The amount to increment.
|
||||
* @param {Knex.Transaction} trx - The knex transaction.
|
||||
*/
|
||||
public incrementCreditNoteRefundAmount = async (
|
||||
creditNoteId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.increment('refunded_amount', amount);
|
||||
};
|
||||
|
||||
/**
|
||||
* Decrements the refund amount of the credit note.
|
||||
* @param {number} creditNoteId - The credit note ID.
|
||||
* @param {number} amount - The amount to decrement.
|
||||
* @param {Knex.Transaction} trx - The knex transaction.
|
||||
*/
|
||||
public decrementCreditNoteRefundAmount = async (
|
||||
creditNoteId: number,
|
||||
amount: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
await this.creditNoteModel()
|
||||
.query(trx)
|
||||
.findById(creditNoteId)
|
||||
.decrement('refunded_amount', amount);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user