feat: inactive associated Stripe payment link on invoice deleting

This commit is contained in:
Ahmed Bouhuolia
2024-09-18 23:41:59 +02:00
parent 4665f529e6
commit d2cd32a735
11 changed files with 214 additions and 38 deletions

View File

@@ -4,6 +4,7 @@ import {
ISystemUser,
ISaleInvoiceDeletePayload,
ISaleInvoiceDeletedPayload,
ISaleInvoiceDeletingPayload,
} from '@/interfaces';
import events from '@/subscribers/events';
import UnitOfWork from '@/services/UnitOfWork';
@@ -82,10 +83,10 @@ export class DeleteSaleInvoice {
) {
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
const saleInvoice = await saleInvoiceRepository.findOneById(
saleInvoiceId,
'entries'
);
const saleInvoice = await saleInvoiceRepository.findOneById(saleInvoiceId, [
'entries',
'paymentMethods',
]);
if (!saleInvoice) {
throw new ServiceError(ERRORS.SALE_INVOICE_NOT_FOUND);
}
@@ -118,15 +119,22 @@ export class DeleteSaleInvoice {
// Validate the sale invoice has applied to credit note transaction.
await this.validateInvoiceHasNoAppliedToCredit(tenantId, saleInvoiceId);
// Triggers `onSaleInvoiceDelete` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onDelete, {
tenantId,
oldSaleInvoice,
saleInvoiceId,
} as ISaleInvoiceDeletePayload);
// Deletes sale invoice transaction and associate transactions with UOW env.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onSaleInvoiceDelete` event.
// Triggers `onSaleInvoiceDeleting` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onDeleting, {
tenantId,
saleInvoice: oldSaleInvoice,
oldSaleInvoice,
saleInvoiceId,
trx,
} as ISaleInvoiceDeletePayload);
} as ISaleInvoiceDeletingPayload);
// Unlink the converted sale estimates from the given sale invoice.
await this.unlockEstimateFromInvoice.unlinkConvertedEstimateFromInvoice(

View File

@@ -3,7 +3,9 @@ import { omit } from 'lodash';
import events from '@/subscribers/events';
import {
ISaleInvoiceCreatedPayload,
ISaleInvoiceDeletingPayload,
PaymentIntegrationTransactionLink,
PaymentIntegrationTransactionLinkDeleteEventPayload,
PaymentIntegrationTransactionLinkEventPayload,
} from '@/interfaces';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@@ -21,6 +23,10 @@ export class InvoicePaymentIntegrationSubscriber {
events.saleInvoice.onCreated,
this.handleCreatePaymentIntegrationEvents
);
bus.subscribe(
events.saleInvoice.onDeleting,
this.handleCreatePaymentIntegrationEventsOnDeleteInvoice
);
return bus;
};
@@ -54,4 +60,34 @@ export class InvoicePaymentIntegrationSubscriber {
}
);
};
/**
*
* @param {ISaleInvoiceDeletingPayload} payload
*/
private handleCreatePaymentIntegrationEventsOnDeleteInvoice = ({
tenantId,
oldSaleInvoice,
trx,
}: ISaleInvoiceDeletingPayload) => {
const paymentMethods =
oldSaleInvoice.paymentMethods?.filter((method) => method.enable) || [];
paymentMethods.map(
async (paymentMethod: PaymentIntegrationTransactionLink) => {
const payload = {
...omit(paymentMethod, ['id']),
tenantId,
oldSaleInvoiceId: oldSaleInvoice.id,
trx,
} as PaymentIntegrationTransactionLinkDeleteEventPayload;
// Triggers `onPaymentIntegrationDeleteLink` event.
await this.eventPublisher.emitAsync(
events.paymentIntegrationLink.onPaymentIntegrationDeleteLink,
payload
);
}
);
};
}