Skip to content

Commit

Permalink
Merge pull request #721 from bigcapitalhq/track-move-events
Browse files Browse the repository at this point in the history
feat: track more services events
  • Loading branch information
abouolia authored Oct 26, 2024
2 parents b4d3ac2 + cadf6b8 commit 26088a7
Show file tree
Hide file tree
Showing 25 changed files with 530 additions and 23 deletions.
21 changes: 21 additions & 0 deletions packages/server/src/constants/event-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 SALE_ESTIMATE_VIEWED = 'Sale estimate viewed';

export const PAYMENT_RECEIVED_CREATED = 'Payment received created';
export const PAYMENT_RECEIVED_EDITED = 'payment received edited';
Expand Down Expand Up @@ -47,6 +48,8 @@ export const AUTH_RESET_PASSWORD = 'Auth reset password';
export const SUBSCRIPTION_CANCELLED = 'Subscription cancelled';
export const SUBSCRIPTION_RESUMED = 'Subscription resumed';
export const SUBSCRIPTION_PLAN_CHANGED = 'Subscription plan changed';
export const SUBSCRIPTION_PAYMENT_SUCCEED = 'Subscription payment succeed';
export const SUBSCRIPTION_PAYMENT_FAILED = 'Subscription payment failed';

export const CUSTOMER_CREATED = 'Customer created';
export const CUSTOMER_EDITED = 'Customer edited';
Expand Down Expand Up @@ -100,3 +103,21 @@ export const SALE_GROUP = 'Sale';
export const PAYMENT_GROUP = 'Payment';
export const BILL_GROUP = 'Bill';
export const EXPENSE_GROUP = 'Expense';

// # Reports
export const BALANCE_SHEET_VIEWED = 'Balance sheet viewed';
export const TRIAL_BALANCE_SHEET_VIEWED = 'Trial balance sheet viewed';
export const PROFIT_LOSS_SHEET_VIEWED = 'Profit loss sheet viewed';
export const CASHFLOW_STATEMENT_VIEWED = 'Cashflow statement viewed';
export const GENERAL_LEDGER_VIEWED = 'General ledger viewed';
export const JOURNAL_VIEWED = 'Journal viewed';
export const RECEIVABLE_AGING_VIEWED = 'Receivable aging viewed';
export const PAYABLE_AGING_VIEWED = 'Payable aging viewed';
export const CUSTOMER_BALANCE_SUMMARY_VIEWED =
'Customer balance summary viewed';
export const VENDOR_BALANCE_SUMMARY_VIEWED = 'Vendor balance summary viewed';
export const INVENTORY_VALUATION_VIEWED = 'Inventory valuation viewed';
export const CUSTOMER_TRANSACTIONS_VIEWED = 'Customer transactions viewed';
export const VENDOR_TRANSACTIONS_VIEWED = 'Vendor transactions viewed';
export const SALES_BY_ITEM_VIEWED = 'Sales by item viewed';
export const PURCHASES_BY_ITEM_VIEWED = 'Purchases by item viewed';
10 changes: 2 additions & 8 deletions packages/server/src/services/Accounts/GetAccount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Service, Inject } from 'typedi';
import I18nService from '@/services/I18n/I18nService';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { AccountTransformer } from './AccountTransform';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
Expand All @@ -11,9 +10,6 @@ export class GetAccount {
@Inject()
private tenancy: HasTenancyService;

@Inject()
private i18nService: I18nService;

@Inject()
private transformer: TransformerInjectable;

Expand Down Expand Up @@ -44,10 +40,8 @@ export class GetAccount {
new AccountTransformer(),
{ accountsGraph }
);
const eventPayload = {
tenantId,
accountId,
};
const eventPayload = { tenantId, accountId };

// Triggers `onAccountViewed` event.
await this.eventPublisher.emitAsync(events.accounts.onViewed, eventPayload);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default class NewCashflowTransactionService {
* @param {number} tenantId -
* @param {ICashflowOwnerContributionDTO} ownerContributionDTO
* @param {number} userId - User id.
* @returns {Promise<ICashflowTransaction>}
*/
public newCashflowTransaction = async (
tenantId: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Knex from 'knex';
import { IRefundCreditNote } from '@/interfaces';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Knex } from 'knex';
import { Inject, Service } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';

@Service()
export default class RefundSyncCreditNoteBalance {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import { Inject, Service } from 'typedi';
import { EventSubscriber } from '@/lib/EventPublisher/EventPublisher';
import { ReportsEvents } from '@/constants/event-tracker';
import { PosthogService } from '../PostHog';
import events from '@/subscribers/events';
import {
BALANCE_SHEET_VIEWED,
TRIAL_BALANCE_SHEET_VIEWED,
PROFIT_LOSS_SHEET_VIEWED,
CASHFLOW_STATEMENT_VIEWED,
GENERAL_LEDGER_VIEWED,
JOURNAL_VIEWED,
RECEIVABLE_AGING_VIEWED,
PAYABLE_AGING_VIEWED,
CUSTOMER_BALANCE_SUMMARY_VIEWED,
VENDOR_BALANCE_SUMMARY_VIEWED,
INVENTORY_VALUATION_VIEWED,
CUSTOMER_TRANSACTIONS_VIEWED,
VENDOR_TRANSACTIONS_VIEWED,
SALES_BY_ITEM_VIEWED,
PURCHASES_BY_ITEM_VIEWED,
} from '@/constants/event-tracker';

@Service()
export class ReportsEventsTracker extends EventSubscriber {
@Inject()
private posthog: PosthogService;

/**
* Constructor method.
*/
public attach(bus) {
bus.subscribe(
events.reports.onBalanceSheetViewed,
this.handleTrackBalanceSheetViewedEvent
);
bus.subscribe(
events.reports.onTrialBalanceSheetView,
this.handleTrackTrialBalanceSheetViewedEvent
);
bus.subscribe(
events.reports.onProfitLossSheetViewed,
this.handleTrackProfitLossSheetViewedEvent
);
bus.subscribe(
events.reports.onCashflowStatementViewed,
this.handleTrackCashflowStatementViewedEvent
);
bus.subscribe(
events.reports.onGeneralLedgerViewed,
this.handleTrackGeneralLedgerViewedEvent
);
bus.subscribe(
events.reports.onJournalViewed,
this.handleTrackJournalViewedEvent
);
bus.subscribe(
events.reports.onReceivableAgingViewed,
this.handleTrackReceivableAgingViewedEvent
);
bus.subscribe(
events.reports.onPayableAgingViewed,
this.handleTrackPayableAgingViewedEvent
);
bus.subscribe(
events.reports.onCustomerBalanceSummaryViewed,
this.handleTrackCustomerBalanceSummaryViewedEvent
);
bus.subscribe(
events.reports.onVendorBalanceSummaryViewed,
this.handleTrackVendorBalanceSummaryViewedEvent
);
bus.subscribe(
events.reports.onInventoryValuationViewed,
this.handleTrackInventoryValuationViewedEvent
);
bus.subscribe(
events.reports.onCustomerTransactionsViewed,
this.handleTrackCustomerTransactionsViewedEvent
);
bus.subscribe(
events.reports.onVendorTransactionsViewed,
this.handleTrackVendorTransactionsViewedEvent
);
bus.subscribe(
events.reports.onSalesByItemViewed,
this.handleTrackSalesByItemViewedEvent
);
bus.subscribe(
events.reports.onPurchasesByItemViewed,
this.handleTrackPurchasesByItemViewedEvent
);
}

private handleTrackBalanceSheetViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: BALANCE_SHEET_VIEWED,
properties: {},
});
};

private handleTrackTrialBalanceSheetViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: TRIAL_BALANCE_SHEET_VIEWED,
properties: {},
});
};

private handleTrackProfitLossSheetViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: PROFIT_LOSS_SHEET_VIEWED,
properties: {},
});
};

private handleTrackCashflowStatementViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CASHFLOW_STATEMENT_VIEWED,
properties: {},
});
};

private handleTrackGeneralLedgerViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: GENERAL_LEDGER_VIEWED,
properties: {},
});
};

private handleTrackJournalViewedEvent = ({ tenantId }: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: JOURNAL_VIEWED,
properties: {},
});
};

private handleTrackReceivableAgingViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: RECEIVABLE_AGING_VIEWED,
properties: {},
});
};

private handleTrackPayableAgingViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: PAYABLE_AGING_VIEWED,
properties: {},
});
};

private handleTrackCustomerBalanceSummaryViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CUSTOMER_BALANCE_SUMMARY_VIEWED,
properties: {},
});
};

private handleTrackVendorBalanceSummaryViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: VENDOR_BALANCE_SUMMARY_VIEWED,
properties: {},
});
};

private handleTrackInventoryValuationViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: INVENTORY_VALUATION_VIEWED,
properties: {},
});
};

private handleTrackCustomerTransactionsViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: CUSTOMER_TRANSACTIONS_VIEWED,
properties: {},
});
};

private handleTrackVendorTransactionsViewedEvent = ({
tenantId,
}: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: VENDOR_TRANSACTIONS_VIEWED,
properties: {},
});
};

private handleTrackSalesByItemViewedEvent = ({ tenantId }: ReportsEvents) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SALES_BY_ITEM_VIEWED,
properties: {},
});
};

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

@Service()
Expand Down Expand Up @@ -39,6 +40,10 @@ export class SaleEstimateEventsTracker extends EventSubscriber {
events.saleEstimate.onPdfViewed,
this.handleTrackPdfViewedEstimateEvent
);
bus.subscribe(
events.saleEstimate.onViewed,
this.handleTrackViewedEstimateEvent
);
}

private handleTrackEstimateCreatedEvent = ({
Expand Down Expand Up @@ -80,4 +85,12 @@ export class SaleEstimateEventsTracker extends EventSubscriber {
properties: {},
});
};

private handleTrackViewedEstimateEvent = ({ tenantId }) => {
this.posthog.trackEvent({
distinctId: `tenant-${tenantId}`,
event: SALE_ESTIMATE_VIEWED,
properties: {},
});
};
}
Loading

0 comments on commit 26088a7

Please sign in to comment.