mirror of
https://github.com/standardnotes/server
synced 2026-07-14 00:01:54 -04:00
59 lines
2.8 KiB
TypeScript
59 lines
2.8 KiB
TypeScript
import { DomainEventHandlerInterface, SubscriptionExpiredEvent } from '@standardnotes/domain-events'
|
|
import { inject, injectable } from 'inversify'
|
|
import { Logger } from 'winston'
|
|
import { Email } from '@standardnotes/domain-core'
|
|
|
|
import TYPES from '../../Bootstrap/Types'
|
|
import { AnalyticsActivity } from '../Analytics/AnalyticsActivity'
|
|
import { AnalyticsStoreInterface } from '../Analytics/AnalyticsStoreInterface'
|
|
import { StatisticsMeasure } from '../Statistics/StatisticsMeasure'
|
|
import { StatisticsStoreInterface } from '../Statistics/StatisticsStoreInterface'
|
|
import { SubscriptionEventType } from '../Subscription/SubscriptionEventType'
|
|
import { SubscriptionPlanName } from '../Subscription/SubscriptionPlanName'
|
|
import { Period } from '../Time/Period'
|
|
import { GetUserAnalyticsId } from '../UseCase/GetUserAnalyticsId/GetUserAnalyticsId'
|
|
import { SaveRevenueModification } from '../UseCase/SaveRevenueModification/SaveRevenueModification'
|
|
|
|
@injectable()
|
|
export class SubscriptionExpiredEventHandler implements DomainEventHandlerInterface {
|
|
constructor(
|
|
@inject(TYPES.GetUserAnalyticsId) private getUserAnalyticsId: GetUserAnalyticsId,
|
|
@inject(TYPES.AnalyticsStore) private analyticsStore: AnalyticsStoreInterface,
|
|
@inject(TYPES.StatisticsStore) private statisticsStore: StatisticsStoreInterface,
|
|
@inject(TYPES.SaveRevenueModification) private saveRevenueModification: SaveRevenueModification,
|
|
@inject(TYPES.Logger) private logger: Logger,
|
|
) {}
|
|
|
|
async handle(event: SubscriptionExpiredEvent): Promise<void> {
|
|
const { analyticsId, userUuid } = await this.getUserAnalyticsId.execute({ userEmail: event.payload.userEmail })
|
|
await this.analyticsStore.markActivity(
|
|
[AnalyticsActivity.SubscriptionExpired, AnalyticsActivity.ExistingCustomersChurn],
|
|
analyticsId,
|
|
[Period.Today, Period.ThisWeek, Period.ThisMonth],
|
|
)
|
|
|
|
await this.statisticsStore.setMeasure(
|
|
StatisticsMeasure.TotalCustomers,
|
|
event.payload.totalActiveSubscriptionsCount,
|
|
[Period.Today, Period.ThisWeek, Period.ThisMonth, Period.ThisYear],
|
|
)
|
|
|
|
const result = await this.saveRevenueModification.execute({
|
|
billingFrequency: event.payload.billingFrequency,
|
|
eventType: SubscriptionEventType.create(event.type).getValue(),
|
|
newSubscriber: event.payload.userExistingSubscriptionsCount === 1,
|
|
payedAmount: event.payload.payAmount,
|
|
planName: SubscriptionPlanName.create(event.payload.subscriptionName).getValue(),
|
|
subscriptionId: event.payload.subscriptionId,
|
|
userEmail: Email.create(event.payload.userEmail).getValue(),
|
|
userUuid,
|
|
})
|
|
|
|
if (result.isFailed()) {
|
|
this.logger.error(
|
|
`[${event.type}][${event.payload.subscriptionId}] Could not save revenue modification: ${result.getError()}`,
|
|
)
|
|
}
|
|
}
|
|
}
|