Browse Source

Setup for multiple subscription offers

pull/1818/head
Thomas 3 years ago
parent
commit
1dcf5f2322
  1. 6
      apps/api/src/app/info/info.service.ts
  2. 6
      apps/api/src/app/subscription/subscription.service.ts
  3. 11
      apps/client/src/app/pages/account/account-page.component.ts
  4. 15
      apps/client/src/app/pages/pricing/pricing-page.component.ts
  5. 2
      libs/common/src/lib/interfaces/info-item.interface.ts
  6. 1
      libs/common/src/lib/interfaces/user-with-settings.ts
  7. 2
      libs/common/src/lib/interfaces/user.interface.ts

6
apps/api/src/app/info/info.service.ts

@ -304,18 +304,18 @@ export class InfoService {
return statistics; return statistics;
} }
private async getSubscriptions(): Promise<Subscription[]> { private async getSubscriptions(): Promise<{ [offer: string]: Subscription }> {
if (!this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { if (!this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
return undefined; return undefined;
} }
let subscriptions: Subscription[] = []; let subscriptions: { [offer: string]: Subscription } = {};
const stripeConfig = (await this.prismaService.property.findUnique({ const stripeConfig = (await this.prismaService.property.findUnique({
where: { key: PROPERTY_STRIPE_CONFIG } where: { key: PROPERTY_STRIPE_CONFIG }
})) ?? { value: '{}' }; })) ?? { value: '{}' };
subscriptions = [JSON.parse(stripeConfig.value)]; subscriptions = JSON.parse(stripeConfig.value);
return subscriptions; return subscriptions;
} }

6
apps/api/src/app/subscription/subscription.service.ts

@ -123,7 +123,9 @@ export class SubscriptionService {
} }
} }
public getSubscription(aSubscriptions: Subscription[]) { public getSubscription(
aSubscriptions: Subscription[]
): UserWithSettings['subscription'] {
if (aSubscriptions.length > 0) { if (aSubscriptions.length > 0) {
const latestSubscription = aSubscriptions.reduce((a, b) => { const latestSubscription = aSubscriptions.reduce((a, b) => {
return new Date(a.expiresAt) > new Date(b.expiresAt) ? a : b; return new Date(a.expiresAt) > new Date(b.expiresAt) ? a : b;
@ -131,12 +133,14 @@ export class SubscriptionService {
return { return {
expiresAt: latestSubscription.expiresAt, expiresAt: latestSubscription.expiresAt,
offer: 'renewal',
type: isBefore(new Date(), latestSubscription.expiresAt) type: isBefore(new Date(), latestSubscription.expiresAt)
? SubscriptionType.Premium ? SubscriptionType.Premium
: SubscriptionType.Basic : SubscriptionType.Basic
}; };
} else { } else {
return { return {
offer: 'default',
type: SubscriptionType.Basic type: SubscriptionType.Basic
}; };
} }

11
apps/client/src/app/pages/account/account-page.component.ts

@ -91,8 +91,6 @@ export class AccountPageComponent implements OnDestroy, OnInit {
this.dataService.fetchInfo(); this.dataService.fetchInfo();
this.baseCurrency = baseCurrency; this.baseCurrency = baseCurrency;
this.coupon = subscriptions?.[0]?.coupon;
this.couponId = subscriptions?.[0]?.couponId;
this.currencies = currencies; this.currencies = currencies;
this.hasPermissionForSubscription = hasPermission( this.hasPermissionForSubscription = hasPermission(
@ -105,9 +103,6 @@ export class AccountPageComponent implements OnDestroy, OnInit {
permissions.deleteAccess permissions.deleteAccess
); );
this.price = subscriptions?.[0]?.price;
this.priceId = subscriptions?.[0]?.priceId;
this.userService.stateChanged this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => { .subscribe((state) => {
@ -141,6 +136,12 @@ export class AccountPageComponent implements OnDestroy, OnInit {
this.locales.push(this.user.settings.locale); this.locales.push(this.user.settings.locale);
this.locales = uniq(this.locales.sort()); this.locales = uniq(this.locales.sort());
this.coupon = subscriptions?.[this.user.subscription.offer]?.coupon;
this.couponId =
subscriptions?.[this.user.subscription.offer]?.couponId;
this.price = subscriptions?.[this.user.subscription.offer]?.price;
this.priceId = subscriptions?.[this.user.subscription.offer]?.priceId;
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
}); });

15
apps/client/src/app/pages/pricing/pricing-page.component.ts

@ -34,21 +34,24 @@ export class PricingPageComponent implements OnDestroy, OnInit {
private changeDetectorRef: ChangeDetectorRef, private changeDetectorRef: ChangeDetectorRef,
private dataService: DataService, private dataService: DataService,
private userService: UserService private userService: UserService
) { ) {}
const { baseCurrency, subscriptions } = this.dataService.fetchInfo();
public ngOnInit() {
const { baseCurrency, subscriptions } = this.dataService.fetchInfo();
this.baseCurrency = baseCurrency; this.baseCurrency = baseCurrency;
this.coupon = subscriptions?.[0]?.coupon;
this.price = subscriptions?.[0]?.price;
}
public ngOnInit() { this.coupon = subscriptions?.default?.coupon;
this.price = subscriptions?.default?.price;
this.userService.stateChanged this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => { .subscribe((state) => {
if (state?.user) { if (state?.user) {
this.user = state.user; this.user = state.user;
this.coupon = subscriptions?.[this.user?.subscription?.offer]?.coupon;
this.price = subscriptions?.[this.user?.subscription?.offer]?.price;
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
}); });

2
libs/common/src/lib/interfaces/info-item.interface.ts

@ -15,7 +15,7 @@ export interface InfoItem {
platforms: { id: string; name: string }[]; platforms: { id: string; name: string }[];
statistics: Statistics; statistics: Statistics;
stripePublicKey?: string; stripePublicKey?: string;
subscriptions: Subscription[]; subscriptions: { [offer: string]: Subscription };
systemMessage?: string; systemMessage?: string;
tags: Tag[]; tags: Tag[];
} }

1
libs/common/src/lib/interfaces/user-with-settings.ts

@ -10,6 +10,7 @@ export type UserWithSettings = User & {
Settings: Settings & { settings: UserSettings }; Settings: Settings & { settings: UserSettings };
subscription?: { subscription?: {
expiresAt?: Date; expiresAt?: Date;
offer: 'default' | 'renewal'; // TOOD: Extract type
type: SubscriptionType; type: SubscriptionType;
}; };
}; };

2
libs/common/src/lib/interfaces/user.interface.ts

@ -2,6 +2,7 @@ import { Account, Tag } from '@prisma/client';
import { UserSettings } from './user-settings.interface'; import { UserSettings } from './user-settings.interface';
// TODO: Compare with UserWithSettings
export interface User { export interface User {
access: { access: {
alias?: string; alias?: string;
@ -13,6 +14,7 @@ export interface User {
settings: UserSettings; settings: UserSettings;
subscription: { subscription: {
expiresAt?: Date; expiresAt?: Date;
offer: 'default' | 'renewal';
type: 'Basic' | 'Premium'; type: 'Basic' | 'Premium';
}; };
tags: Tag[]; tags: Tag[];

Loading…
Cancel
Save