Files
bigcapital/packages/server/src/modules/SaleInvoices/queries/GetInvoicePayments.service.ts
2025-04-07 11:51:24 +02:00

37 lines
1.3 KiB
TypeScript

import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
import { InvoicePaymentTransactionTransformer } from './InvoicePaymentTransaction.transformer';
import { Inject, Injectable } from '@nestjs/common';
import { PaymentReceivedEntry } from '@/modules/PaymentReceived/models/PaymentReceivedEntry';
import { TenantModelProxy } from '@/modules/System/models/TenantBaseModel';
@Injectable()
export class GetInvoicePaymentsService {
constructor(
private readonly transformer: TransformerInjectable,
@Inject(PaymentReceivedEntry.name)
private readonly paymentReceivedEntryModel: TenantModelProxy<
typeof PaymentReceivedEntry
>,
) {}
/**
* Retrieve the invoice associated payments transactions.
* @param {number} tenantId - Tenant id.
* @param {number} invoiceId - Invoice id.
*/
public getInvoicePayments = async (invoiceId: number) => {
const paymentsEntries = await this.paymentReceivedEntryModel()
.query()
.where('invoiceId', invoiceId)
.withGraphJoined('payment.depositAccount')
.withGraphJoined('invoice')
.orderBy('payment:paymentDate', 'ASC');
return this.transformer.transform(
paymentsEntries,
new InvoicePaymentTransactionTransformer(),
);
};
}