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;
}
private async getSubscriptions(): Promise<Subscription[]> {
private async getSubscriptions(): Promise<{ [offer: string]: Subscription }> {
if (!this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
return undefined;
}
let subscriptions: Subscription[] = [];
let subscriptions: { [offer: string]: Subscription } = {};
const stripeConfig = (await this.prismaService.property.findUnique({
where: { key: PROPERTY_STRIPE_CONFIG }
})) ?? { value: '{}' };
subscriptions = [JSON.parse(stripeConfig.value)];
subscriptions = JSON.parse(stripeConfig.value);
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) {
const latestSubscription = aSubscriptions.reduce((a, b) => {
return new Date(a.expiresAt) > new Date(b.expiresAt) ? a : b;
@ -131,12 +133,14 @@ export class SubscriptionService {
return {
expiresAt: latestSubscription.expiresAt,
offer: 'renewal',
type: isBefore(new Date(), latestSubscription.expiresAt)
? SubscriptionType.Premium
: SubscriptionType.Basic
};
} else {
return {
offer: 'default',
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.baseCurrency = baseCurrency;
this.coupon = subscriptions?.[0]?.coupon;
this.couponId = subscriptions?.[0]?.couponId;
this.currencies = currencies;
this.hasPermissionForSubscription = hasPermission(
@ -105,9 +103,6 @@ export class AccountPageComponent implements OnDestroy, OnInit {
permissions.deleteAccess
);
this.price = subscriptions?.[0]?.price;
this.priceId = subscriptions?.[0]?.priceId;
this.userService.stateChanged
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => {
@ -141,6 +136,12 @@ export class AccountPageComponent implements OnDestroy, OnInit {
this.locales.push(this.user.settings.locale);
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();
}
});

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 dataService: DataService,
private userService: UserService
) {
const { baseCurrency, subscriptions } = this.dataService.fetchInfo();
) {}
public ngOnInit() {
const { baseCurrency, subscriptions } = this.dataService.fetchInfo();
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
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe((state) => {
if (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();
}
});

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

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

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

@ -10,6 +10,7 @@ export type UserWithSettings = User & {
Settings: Settings & { settings: UserSettings };
subscription?: {
expiresAt?: Date;
offer: 'default' | 'renewal'; // TOOD: Extract type
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';
// TODO: Compare with UserWithSettings
export interface User {
access: {
alias?: string;
@ -13,6 +14,7 @@ export interface User {
settings: UserSettings;
subscription: {
expiresAt?: Date;
offer: 'default' | 'renewal';
type: 'Basic' | 'Premium';
};
tags: Tag[];

Loading…
Cancel
Save