fix(server): add invoices Map for validateInvoicesRemainingAmount

This commit is contained in:
Ahmed Bouhuolia
2026-02-24 02:52:28 +02:00
parent 0f8147daff
commit 688b1bfb56
2 changed files with 8 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ 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';
import { ApplyCreditNoteToInvoicesDto } from '../dtos/ApplyCreditNoteToInvoices.dto';
@Injectable()
export class CreditNoteApplyToInvoices {
@@ -49,7 +50,7 @@ export class CreditNoteApplyToInvoices {
*/
public async applyCreditNoteToInvoices(
creditNoteId: number,
applyCreditToInvoicesDTO: IApplyCreditToInvoicesDTO,
applyCreditToInvoicesDTO: ApplyCreditNoteToInvoicesDto,
): Promise<CreditNoteAppliedInvoice[]> {
// Saves the credit note or throw not found service error.
const creditNote = await this.creditNoteModel()
@@ -131,11 +132,12 @@ export class CreditNoteApplyToInvoices {
invoices: SaleInvoice[],
entries: ICreditNoteAppliedToInvoice[],
) => {
const invalidInvoices = invoices.filter((invoice) => {
const entry = entries.find((e) => e.invoiceId === invoice.id);
return entry && invoice.dueAmount < entry.amount;
const invoiceMap = new Map(invoices.map((inv) => [inv.id, inv]));
const invalidEntries = entries.filter((entry) => {
const invoice = invoiceMap.get(entry.invoiceId);
return invoice != null && invoice.dueAmount < entry.amount;
});
if (invalidInvoices.length > 0) {
if (invalidEntries.length > 0) {
throw new ServiceError(ERRORS.INVOICES_HAS_NO_REMAINING_AMOUNT);
}
};

View File

@@ -29,6 +29,7 @@ export interface IApplyCreditToInvoicesDeletedPayload {
export interface ICreditNoteAppliedToInvoice {
amount: number;
creditNoteId: number;
invoiceId: number;
}
export interface ICreditNoteAppliedToInvoiceModel {
amount: number;