Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Track events of pdf documents views #719

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/server/src/constants/event-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ export const SALE_INVOICE_EDITED = 'Sale invoice edited';
export const SALE_INVOICE_DELETED = 'Sale invoice deleted';
export const SALE_INVOICE_MAIL_DELIVERED = 'Sale invoice mail delivered';
export const SALE_INVOICE_VIEWED = 'Sale invoice viewed';
export const SALE_INVOICE_PDF_VIEWED = 'Sale invoice PDF viewed';

export const SALE_ESTIMATE_CREATED = 'Sale estimate created';
export const SALE_ESTIMATE_EDITED = 'Sale estimate edited';
export const SALE_ESTIMATE_DELETED = 'Sale estimate deleted';
export const SALE_ESTIMATE_PDF_VIEWED = 'Sale estimate PDF viewed';

export const PAYMENT_RECEIVED_CREATED = 'Payment received created';
export const PAYMENT_RECEIVED_EDITED = 'payment received edited';
export const PAYMENT_RECEIVED_DELETED = 'Payment received deleted';
export const PAYMENT_RECEIVED_PDF_VIEWED = 'Payment received PDF viewed';

export const SALE_RECEIPT_PDF_VIEWED = 'Sale credit PDF viewed';

export const CREDIT_NOTE_PDF_VIEWED = 'Credit note PDF viewed';

export const BILL_CREATED = 'Bill created';
export const BILL_EDITED = 'Bill edited';
Expand Down Expand Up @@ -82,7 +89,8 @@ export const PAYMENT_METHOD_DELETED = 'Payment method deleted';

export const INVOICE_PAYMENT_LINK_GENERATED = 'Invoice payment link generated';

export const STRIPE_INTEGRAION_CONNECTED = 'Stripe integration oauth2 connected';
export const STRIPE_INTEGRAION_CONNECTED =
'Stripe integration oauth2 connected';

// # Event Groups
export const ACCOUNT_GROUP = 'Account';
Expand Down
12 changes: 12 additions & 0 deletions packages/server/src/services/CreditNotes/GetCreditNotePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { CreditNoteBrandingTemplate } from './CreditNoteBrandingTemplate';
import { CreditNotePdfTemplateAttributes } from '@/interfaces';
import HasTenancyService from '../Tenancy/TenancyService';
import { transformCreditNoteToPdfTemplate } from './utils';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import events from '@/subscribers/events';

@Service()
export default class GetCreditNotePdf {
Expand All @@ -24,6 +26,9 @@ export default class GetCreditNotePdf {
@Inject()
private creditNoteBrandingTemplate: CreditNoteBrandingTemplate;

@Inject()
private eventPublisher: EventPublisher;

/**
* Retrieves sale invoice pdf content.
* @param {number} tenantId - Tenant id.
Expand All @@ -49,6 +54,13 @@ export default class GetCreditNotePdf {
tenantId,
htmlContent
);
const eventPayload = { tenantId, creditNoteId };

// Triggers the `onCreditNotePdfViewed` event.
await this.eventPublisher.emitAsync(
events.creditNote.onPdfViewed,
eventPayload
);
return [document, filename];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
PAYMENT_RECEIVED_CREATED,
PAYMENT_RECEIVED_EDITED,
PAYMENT_RECEIVED_DELETED,
PAYMENT_RECEIVED_PDF_VIEWED,
} from '@/constants/event-tracker';

@Service()
Expand All @@ -34,6 +35,10 @@ export class PaymentReceivedEventsTracker extends EventSubscriber {
events.paymentReceive.onDeleted,
this.handleTrackDeletedPaymentReceivedEvent
);
bus.subscribe(
events.paymentReceive.onPdfViewed,
this.handleTrackPdfViewedPaymentReceivedEvent
);
}

private handleTrackPaymentReceivedCreatedEvent = ({
Expand Down Expand Up @@ -65,4 +70,14 @@ export class PaymentReceivedEventsTracker extends EventSubscriber {
properties: {},
});
};

private handleTrackPdfViewedPaymentReceivedEvent = ({
tenantId,
}: IPaymentReceivedDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: PAYMENT_RECEIVED_PDF_VIEWED,
properties: {},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SALE_ESTIMATE_CREATED,
SALE_ESTIMATE_EDITED,
SALE_ESTIMATE_DELETED,
SALE_ESTIMATE_PDF_VIEWED,
} from '@/constants/event-tracker';

@Service()
Expand All @@ -34,6 +35,10 @@ export class SaleEstimateEventsTracker extends EventSubscriber {
events.saleEstimate.onDeleted,
this.handleTrackDeletedEstimateEvent
);
bus.subscribe(
events.saleEstimate.onPdfViewed,
this.handleTrackPdfViewedEstimateEvent
);
}

private handleTrackEstimateCreatedEvent = ({
Expand Down Expand Up @@ -65,4 +70,14 @@ export class SaleEstimateEventsTracker extends EventSubscriber {
properties: {},
});
};

private handleTrackPdfViewedEstimateEvent = ({
tenantId,
}: ISaleEstimateDeletedPayload) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SALE_ESTIMATE_PDF_VIEWED,
properties: {},
});
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SALE_INVOICE_CREATED,
SALE_INVOICE_DELETED,
SALE_INVOICE_EDITED,
SALE_INVOICE_PDF_VIEWED,
SALE_INVOICE_VIEWED,
} from '@/constants/event-tracker';

Expand Down Expand Up @@ -38,6 +39,10 @@ export class SaleInvoiceEventsTracker extends EventSubscriber {
events.saleInvoice.onViewed,
this.handleTrackViewedInvoiceEvent
);
bus.subscribe(
events.saleInvoice.onPdfViewed,
this.handleTrackPdfViewedInvoiceEvent
);
}

private handleTrackInvoiceCreatedEvent = ({
Expand Down Expand Up @@ -77,4 +82,12 @@ export class SaleInvoiceEventsTracker extends EventSubscriber {
properties: {},
});
};

private handleTrackPdfViewedInvoiceEvent = ({ tenantId }) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SALE_INVOICE_PDF_VIEWED,
properties: {},
});
};
}
12 changes: 12 additions & 0 deletions packages/server/src/services/Sales/Estimates/SaleEstimatesPdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +26,9 @@ export class SaleEstimatesPdf {
@Inject()
private estimatePdfTemplate: SaleEstimatePdfTemplate;

@Inject()
private eventPublisher: EventPublisher;

/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId -
Expand All @@ -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];
}

Expand Down
11 changes: 11 additions & 0 deletions packages/server/src/services/Sales/Invoices/SaleInvoicePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +26,9 @@ export default class GetPaymentReceivedPdf {
@Inject()
private paymentBrandingTemplateService: PaymentReceivedBrandingTemplate;

@Inject()
private eventPublisher: EventPublisher;

/**
* Retrieve sale invoice pdf content.
* @param {number} tenantId -
Expand All @@ -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,
Expand All @@ -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];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +26,9 @@ export class SaleReceiptsPdf {
@Inject()
private saleReceiptBrandingTemplate: SaleReceiptBrandingTemplate;

@Inject()
private eventPublisher: EventPublisher;

/**
* Retrieves sale invoice pdf content.
* @param {number} tenantId -
Expand All @@ -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];
}

Expand Down
18 changes: 14 additions & 4 deletions packages/server/src/subscribers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export default {
onViewed: 'onSaleInvoiceItemViewed',
onListViewed: 'onSaleInvoiceListViewed',

onPdfViewed: 'onSaleInvoicePdfViewed',

onCreate: 'onSaleInvoiceCreate',
onCreating: 'onSaleInvoiceCreating',
onCreated: 'onSaleInvoiceCreated',
Expand Down Expand Up @@ -178,6 +180,8 @@ export default {
* Sales estimates service.
*/
saleEstimate: {
onPdfViewed: 'onSaleEstimatePdfViewed',

onCreating: 'onSaleEstimateCreating',
onCreated: 'onSaleEstimateCreated',

Expand Down Expand Up @@ -215,6 +219,8 @@ export default {
* Sales receipts service.
*/
saleReceipt: {
onPdfViewed: 'onSaleReceiptPdfViewed',

onCreating: 'onSaleReceiptsCreating',
onCreated: 'onSaleReceiptsCreated',

Expand Down Expand Up @@ -242,6 +248,8 @@ export default {
* Payment receipts service.
*/
paymentReceive: {
onPdfViewed: 'onPaymentReceivedPdfViewed',

onCreated: 'onPaymentReceiveCreated',
onCreating: 'onPaymentReceiveCreating',

Expand Down Expand Up @@ -345,7 +353,7 @@ export default {
*/
item: {
onViewed: 'onItemViewed',

onCreated: 'onItemCreated',
onCreating: 'onItemCreating',

Expand Down Expand Up @@ -464,6 +472,8 @@ export default {
* Credit note service.
*/
creditNote: {
onPdfViewed: 'onCreditNotePdfViewed',

onCreate: 'onCreditNoteCreate',
onCreating: 'onCreditNoteCreating',
onCreated: 'onCreditNoteCreated',
Expand Down Expand Up @@ -722,7 +732,7 @@ export default {
// Payment methods integrations
paymentIntegrationLink: {
onPaymentIntegrationLink: 'onPaymentIntegrationLink',
onPaymentIntegrationDeleteLink: 'onPaymentIntegrationDeleteLink'
onPaymentIntegrationDeleteLink: 'onPaymentIntegrationDeleteLink',
},

// Stripe Payment Integration
Expand All @@ -739,6 +749,6 @@ export default {
// Stripe Payment Webhooks
stripeWebhooks: {
onCheckoutSessionCompleted: 'onStripeCheckoutSessionCompleted',
onAccountUpdated: 'onStripeAccountUpdated'
}
onAccountUpdated: 'onStripeAccountUpdated',
},
};
Loading