feat: Track events of pdf documents views

This commit is contained in:
Ahmed Bouhuolia
2024-10-19 13:38:28 +02:00
parent de50b89e5c
commit bb299aa595
10 changed files with 128 additions and 8 deletions

View File

@@ -6,6 +6,8 @@ import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SaleEstimatePdfTemplate } from '../Invoices/SaleEstimatePdfTemplate';
import { transformEstimateToPdfTemplate } from './utils';
import { EstimatePdfBrandingAttributes } from './constants';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export class SaleEstimatesPdf {
@@ -24,6 +26,9 @@ export class SaleEstimatesPdf {
@Inject()
private estimatePdfTemplate: SaleEstimatePdfTemplate;
@Inject()
private eventPublisher: EventPublisher;
/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId -
@@ -50,6 +55,13 @@ export class SaleEstimatesPdf {
tenantId,
htmlContent
);
const eventPayload = { tenantId, saleEstimateId };
// Triggers the `onSaleEstimatePdfViewed` event.
await this.eventPublisher.emitAsync(
events.saleEstimate.onPdfViewed,
eventPayload
);
return [content, filename];
}

View File

@@ -6,6 +6,8 @@ import HasTenancyService from '@/services/Tenancy/TenancyService';
import { transformInvoiceToPdfTemplate } from './utils';
import { InvoicePdfTemplateAttributes } from '@/interfaces';
import { SaleInvoicePdfTemplate } from './SaleInvoicePdfTemplate';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
@Service()
export class SaleInvoicePdf {
@@ -24,6 +26,9 @@ export class SaleInvoicePdf {
@Inject()
private invoiceBrandingTemplateService: SaleInvoicePdfTemplate;
@Inject()
private eventPublisher: EventPublisher;
/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId - Tenant Id.
@@ -50,7 +55,13 @@ export class SaleInvoicePdf {
tenantId,
htmlContent
);
const eventPayload = { tenantId, saleInvoiceId: invoiceId };
// Triggers the `onSaleInvoicePdfViewed` event.
await this.eventPublisher.emitAsync(
events.saleInvoice.onPdfViewed,
eventPayload
);
return [buffer, filename];
}

View File

@@ -6,6 +6,8 @@ import HasTenancyService from '@/services/Tenancy/TenancyService';
import { PaymentReceivedBrandingTemplate } from './PaymentReceivedBrandingTemplate';
import { transformPaymentReceivedToPdfTemplate } from './utils';
import { PaymentReceivedPdfTemplateAttributes } from '@/interfaces';
import events from '@/subscribers/events';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
@Service()
export default class GetPaymentReceivedPdf {
@@ -24,6 +26,9 @@ export default class GetPaymentReceivedPdf {
@Inject()
private paymentBrandingTemplateService: PaymentReceivedBrandingTemplate;
@Inject()
private eventPublisher: EventPublisher;
/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId -
@@ -32,11 +37,11 @@ export default class GetPaymentReceivedPdf {
*/
async getPaymentReceivePdf(
tenantId: number,
paymentReceiveId: number
paymentReceivedId: number
): Promise<[Buffer, string]> {
const brandingAttributes = await this.getPaymentBrandingAttributes(
tenantId,
paymentReceiveId
paymentReceivedId
);
const htmlContent = await this.templateInjectable.render(
tenantId,
@@ -45,13 +50,20 @@ export default class GetPaymentReceivedPdf {
);
const filename = await this.getPaymentReceivedFilename(
tenantId,
paymentReceiveId
paymentReceivedId
);
// Converts the given html content to pdf document.
const content = await this.chromiumlyTenancy.convertHtmlContent(
tenantId,
htmlContent
);
const eventPayload = { tenantId, paymentReceivedId };
// Triggers the `onCreditNotePdfViewed` event.
await this.eventPublisher.emitAsync(
events.paymentReceive.onPdfViewed,
eventPayload
);
return [content, filename];
}

View File

@@ -6,6 +6,8 @@ import HasTenancyService from '@/services/Tenancy/TenancyService';
import { SaleReceiptBrandingTemplate } from './SaleReceiptBrandingTemplate';
import { transformReceiptToBrandingTemplateAttributes } from './utils';
import { ISaleReceiptBrandingTemplateAttributes } from '@/interfaces';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';
@Service()
export class SaleReceiptsPdf {
@@ -24,6 +26,9 @@ export class SaleReceiptsPdf {
@Inject()
private saleReceiptBrandingTemplate: SaleReceiptBrandingTemplate;
@Inject()
private eventPublisher: EventPublisher;
/**
* Retrieves sale invoice pdf content.
* @param {number} tenantId -
@@ -48,6 +53,13 @@ export class SaleReceiptsPdf {
tenantId,
htmlContent
);
const eventPayload = { tenantId, saleReceiptId };
// Triggers the `onSaleReceiptPdfViewed` event.
await this.eventPublisher.emitAsync(
events.saleReceipt.onPdfViewed,
eventPayload
);
return [content, filename];
}