Files
bigcapital/packages/server/src/modules/SaleInvoices/subscribers/InvoicePaymentIntegrationSubscriber.ts
Ahmed Bouhuolia edf12fb87a fix(server): resolve all TypeScript errors without unsafe type casts
Fix 20+ pre-existing TypeScript errors in the server package using
proper type-safe solutions — no `as any`, `as unknown`, or `any` types.

Key changes:
- Replace R.curry with regular curried arrow functions for proper inference
- Add return types to abstract methods (DynamicFilterRoleAbstractor)
- Add field declarations to empty models (ItemWarehouseQuantity)
- Add index signature to IMetadata for dynamic extra columns
- Use explicit field construction instead of pick()+cast patterns
- Convert moment format strings to Date objects where Date type expected
- Make interface properties optional where payloads don't include them
- Use native Array.reduce with proper typing instead of lodash chain

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-29 17:52:46 +02:00

77 lines
2.6 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { PaymentIntegrationTransactionLinkDeleteEventPayload } from '../SaleInvoice.types';
import { PaymentIntegrationTransactionLinkEventPayload } from '../SaleInvoice.types';
import { PaymentIntegrationTransactionLink } from '../SaleInvoice.types';
import { omit } from 'lodash';
import {
ISaleInvoiceCreatedPayload,
ISaleInvoiceDeletingPayload,
} from '../SaleInvoice.types';
import { events } from '@/common/events/events';
import { TransactionPaymentServiceEntry } from '@/modules/PaymentServices/models/TransactionPaymentServiceEntry.model';
@Injectable()
export class InvoicePaymentIntegrationSubscriber {
constructor(private readonly eventPublisher: EventEmitter2) {}
/**
* Handles the creation of payment integration events when a sale invoice is created.
* This method filters enabled payment methods from the invoice and emits a payment
* integration link event for each method.
* @param {ISaleInvoiceCreatedPayload} payload - The payload containing sale invoice creation details.
*/
@OnEvent(events.saleInvoice.onCreated)
public handleCreatePaymentIntegrationEvents({
saleInvoiceDTO,
saleInvoice,
trx,
}: ISaleInvoiceCreatedPayload) {
const paymentMethods =
saleInvoice.paymentMethods?.filter((method) => method.enable) || [];
paymentMethods.map(
async (paymentMethod: TransactionPaymentServiceEntry) => {
const payload: PaymentIntegrationTransactionLinkEventPayload = {
...omit(paymentMethod, ['id']),
saleInvoiceId: saleInvoice.id,
trx,
};
await this.eventPublisher.emitAsync(
events.paymentIntegrationLink.onPaymentIntegrationLink,
payload,
);
},
);
}
/**
*
* @param {ISaleInvoiceDeletingPayload} payload
*/
@OnEvent(events.saleInvoice.onDeleting)
public handleCreatePaymentIntegrationEventsOnDeleteInvoice({
oldSaleInvoice,
trx,
}: ISaleInvoiceDeletingPayload) {
const paymentMethods =
oldSaleInvoice.paymentMethods?.filter((method) => method.enable) || [];
paymentMethods.map(
async (paymentMethod: TransactionPaymentServiceEntry) => {
const payload: PaymentIntegrationTransactionLinkDeleteEventPayload = {
...omit(paymentMethod, ['id']),
oldSaleInvoiceId: oldSaleInvoice.id,
trx,
};
// Triggers `onPaymentIntegrationDeleteLink` event.
await this.eventPublisher.emitAsync(
events.paymentIntegrationLink.onPaymentIntegrationDeleteLink,
payload,
);
},
);
}
}