From ef9d77312e8c2d5c217548cc40250a4f013e8b4d Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 11 Feb 2024 12:33:34 +0100 Subject: [PATCH] Introduce renewal-early-bird (#2982) --- apps/api/src/app/admin/admin.service.ts | 5 +++- .../app/subscription/subscription.service.ts | 25 +++++++++++++------ apps/api/src/app/user/user.service.ts | 6 +++-- .../src/lib/types/subscription-offer.type.ts | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/apps/api/src/app/admin/admin.service.ts b/apps/api/src/app/admin/admin.service.ts index b77d77355..2fe450c9d 100644 --- a/apps/api/src/app/admin/admin.service.ts +++ b/apps/api/src/app/admin/admin.service.ts @@ -455,7 +455,10 @@ export class AdminService { const subscription = this.configurationService.get( 'ENABLE_FEATURE_SUBSCRIPTION' ) - ? this.subscriptionService.getSubscription(Subscription) + ? this.subscriptionService.getSubscription({ + createdAt, + subscriptions: Subscription + }) : undefined; return { diff --git a/apps/api/src/app/subscription/subscription.service.ts b/apps/api/src/app/subscription/subscription.service.ts index 2e7c16a39..668cf3510 100644 --- a/apps/api/src/app/subscription/subscription.service.ts +++ b/apps/api/src/app/subscription/subscription.service.ts @@ -1,7 +1,8 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; -import { UserWithSettings } from '@ghostfolio/common/types'; +import { parseDate } from '@ghostfolio/common/helper'; +import { SubscriptionOffer, UserWithSettings } from '@ghostfolio/common/types'; import { SubscriptionType } from '@ghostfolio/common/types/subscription-type.type'; import { Injectable, Logger } from '@nestjs/common'; import { Subscription } from '@prisma/client'; @@ -107,17 +108,27 @@ export class SubscriptionService { } } - public getSubscription( - aSubscriptions: Subscription[] - ): UserWithSettings['subscription'] { - if (aSubscriptions.length > 0) { - const { expiresAt, price } = aSubscriptions.reduce((a, b) => { + public getSubscription({ + createdAt, + subscriptions + }: { + createdAt: UserWithSettings['createdAt']; + subscriptions: Subscription[]; + }): UserWithSettings['subscription'] { + if (subscriptions.length > 0) { + const { expiresAt, price } = subscriptions.reduce((a, b) => { return new Date(a.expiresAt) > new Date(b.expiresAt) ? a : b; }); + let offer: SubscriptionOffer = price ? 'renewal' : 'default'; + + if (isBefore(createdAt, parseDate('2023-01-01'))) { + offer = 'renewal-early-bird'; + } + return { expiresAt, - offer: price ? 'renewal' : 'default', + offer, type: isBefore(new Date(), expiresAt) ? SubscriptionType.Premium : SubscriptionType.Basic diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 734495474..a6710b9a0 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -211,8 +211,10 @@ export class UserService { } if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { - user.subscription = - this.subscriptionService.getSubscription(Subscription); + user.subscription = this.subscriptionService.getSubscription({ + createdAt: user.createdAt, + subscriptions: Subscription + }); if (user.subscription?.type === 'Basic') { const daysSinceRegistration = differenceInDays( diff --git a/libs/common/src/lib/types/subscription-offer.type.ts b/libs/common/src/lib/types/subscription-offer.type.ts index b7b2760ec..180088b1e 100644 --- a/libs/common/src/lib/types/subscription-offer.type.ts +++ b/libs/common/src/lib/types/subscription-offer.type.ts @@ -1 +1 @@ -export type SubscriptionOffer = 'default' | 'renewal'; +export type SubscriptionOffer = 'default' | 'renewal' | 'renewal-early-bird';