diff --git a/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts b/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts index c5334da74..8824eda3f 100644 --- a/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts +++ b/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts @@ -6,17 +6,23 @@ import { SubscriptionRenewedEvent } from '@standardnotes/domain-events' import { SubscriptionRenewedEventHandler } from './SubscriptionRenewedEventHandler' import { GetUserAnalyticsId } from '../UseCase/GetUserAnalyticsId/GetUserAnalyticsId' import { AnalyticsStoreInterface } from '../Analytics/AnalyticsStoreInterface' +import { SaveRevenueModification } from '../UseCase/SaveRevenueModification/SaveRevenueModification' +import { RevenueModification } from '../Revenue/RevenueModification' +import { Result } from '../Core/Result' describe('SubscriptionRenewedEventHandler', () => { let event: SubscriptionRenewedEvent let getUserAnalyticsId: GetUserAnalyticsId let analyticsStore: AnalyticsStoreInterface + let saveRevenueModification: SaveRevenueModification - const createHandler = () => new SubscriptionRenewedEventHandler(getUserAnalyticsId, analyticsStore) + const createHandler = () => + new SubscriptionRenewedEventHandler(getUserAnalyticsId, analyticsStore, saveRevenueModification) beforeEach(() => { event = {} as jest.Mocked event.createdAt = new Date(1) + event.type = 'SUBSCRIPTION_RENEWED' event.payload = { subscriptionId: 1, userEmail: 'test@test.com', @@ -24,6 +30,8 @@ describe('SubscriptionRenewedEventHandler', () => { subscriptionExpiresAt: 2, timestamp: 1, offline: false, + billingFrequency: 1, + payAmount: 12.99, } getUserAnalyticsId = {} as jest.Mocked @@ -32,6 +40,9 @@ describe('SubscriptionRenewedEventHandler', () => { analyticsStore = {} as jest.Mocked analyticsStore.markActivity = jest.fn() analyticsStore.unmarkActivity = jest.fn() + + saveRevenueModification = {} as jest.Mocked + saveRevenueModification.execute = jest.fn().mockReturnValue(Result.ok()) }) it('should track subscription renewed statistics', async () => { @@ -39,5 +50,6 @@ describe('SubscriptionRenewedEventHandler', () => { expect(analyticsStore.markActivity).toHaveBeenCalled() expect(analyticsStore.unmarkActivity).toHaveBeenCalled() + expect(saveRevenueModification.execute).toHaveBeenCalled() }) }) diff --git a/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.ts b/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.ts index c734c4aa8..ca1f4c724 100644 --- a/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.ts +++ b/packages/analytics/src/Domain/Handler/SubscriptionRenewedEventHandler.ts @@ -6,16 +6,21 @@ import { GetUserAnalyticsId } from '../UseCase/GetUserAnalyticsId/GetUserAnalyti import { AnalyticsActivity } from '../Analytics/AnalyticsActivity' import { AnalyticsStoreInterface } from '../Analytics/AnalyticsStoreInterface' import { Period } from '../Time/Period' +import { SaveRevenueModification } from '../UseCase/SaveRevenueModification/SaveRevenueModification' +import { Email } from '../Common/Email' +import { SubscriptionEventType } from '../Subscription/SubscriptionEventType' +import { SubscriptionPlanName } from '../Subscription/SubscriptionPlanName' @injectable() export class SubscriptionRenewedEventHandler implements DomainEventHandlerInterface { constructor( @inject(TYPES.GetUserAnalyticsId) private getUserAnalyticsId: GetUserAnalyticsId, @inject(TYPES.AnalyticsStore) private analyticsStore: AnalyticsStoreInterface, + @inject(TYPES.SaveRevenueModification) private saveRevenueModification: SaveRevenueModification, ) {} async handle(event: SubscriptionRenewedEvent): Promise { - const { analyticsId } = await this.getUserAnalyticsId.execute({ userEmail: event.payload.userEmail }) + const { analyticsId, userUuid } = await this.getUserAnalyticsId.execute({ userEmail: event.payload.userEmail }) await this.analyticsStore.markActivity([AnalyticsActivity.SubscriptionRenewed], analyticsId, [ Period.Today, Period.ThisWeek, @@ -26,5 +31,16 @@ export class SubscriptionRenewedEventHandler implements DomainEventHandlerInterf analyticsId, [Period.Today, Period.ThisWeek, Period.ThisMonth], ) + + await this.saveRevenueModification.execute({ + billingFrequency: event.payload.billingFrequency, + eventType: SubscriptionEventType.create(event.type).getValue(), + newSubscriber: false, + payedAmount: event.payload.payAmount, + planName: SubscriptionPlanName.create(event.payload.subscriptionName).getValue(), + subscriptionId: event.payload.subscriptionId, + userEmail: Email.create(event.payload.userEmail).getValue(), + userUuid, + }) } } diff --git a/packages/auth/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts b/packages/auth/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts index 3a7300b9f..d775c23a8 100644 --- a/packages/auth/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts +++ b/packages/auth/src/Domain/Handler/SubscriptionRenewedEventHandler.spec.ts @@ -81,6 +81,8 @@ describe('SubscriptionRenewedEventHandler', () => { subscriptionExpiresAt, timestamp, offline: false, + billingFrequency: 1, + payAmount: 12.99, } logger = {} as jest.Mocked diff --git a/packages/domain-events/src/Domain/Event/SubscriptionRenewedEventPayload.ts b/packages/domain-events/src/Domain/Event/SubscriptionRenewedEventPayload.ts index 39171346f..557090403 100644 --- a/packages/domain-events/src/Domain/Event/SubscriptionRenewedEventPayload.ts +++ b/packages/domain-events/src/Domain/Event/SubscriptionRenewedEventPayload.ts @@ -7,4 +7,6 @@ export interface SubscriptionRenewedEventPayload { subscriptionExpiresAt: number timestamp: number offline: boolean + billingFrequency: number + payAmount: number }