Files
bigcapital/packages/server/src/subscribers/SaleReceipt/SendSmsNotificationToCustomer.ts
2023-02-03 11:57:50 +02:00

46 lines
1.2 KiB
TypeScript

import { Inject, Service } from 'typedi';
import events from '@/subscribers/events';
import SaleReceiptNotifyBySms from '@/services/Sales/SaleReceiptNotifyBySms';
import { ISaleReceiptCreatedPayload } from '@/interfaces';
import { runAfterTransaction } from '@/services/UnitOfWork/TransactionsHooks';
@Service()
export default class SendSmsNotificationSaleReceipt {
@Inject()
saleReceiptNotifyBySms: SaleReceiptNotifyBySms;
/**
* Attaches events with handlers.
*/
public attach(bus) {
bus.subscribe(
events.saleReceipt.onCreated,
this.handleNotifyViaSmsAfterReceiptCreation
);
}
/**
* Notify via SMS message after receipt transaction creation.
* @param {ISaleReceiptCreatedPayload} payload -
*/
private handleNotifyViaSmsAfterReceiptCreation = ({
tenantId,
saleReceiptId,
saleReceipt,
trx,
}: ISaleReceiptCreatedPayload) => {
// Can't continue if the sale receipt is not closed.
if (!saleReceipt.isClosed) return;
// Notify via sms after transaction complete running.
runAfterTransaction(trx, async () => {
try {
await this.saleReceiptNotifyBySms.notifyViaSmsAfterCreation(
tenantId,
saleReceiptId
);
} catch (error) {}
});
};
}