From fa693a2813fbd97f55fd6938f19edab505d5c419 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Thu, 8 Jun 2023 19:02:41 -0400 Subject: [PATCH] Avoids overwriting a trial in progress --- src/plus/subscription/subscriptionService.ts | 14 ++++++++++---- src/subscription.ts | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/plus/subscription/subscriptionService.ts b/src/plus/subscription/subscriptionService.ts index bc122e5..3f7bc68 100644 --- a/src/plus/subscription/subscriptionService.ts +++ b/src/plus/subscription/subscriptionService.ts @@ -36,6 +36,7 @@ import { getSubscriptionTimeRemaining, getTimeRemaining, isSubscriptionExpired, + isSubscriptionInProTrial, isSubscriptionPaid, isSubscriptionTrial, SubscriptionPlanId, @@ -467,6 +468,9 @@ export class SubscriptionService implements Disposable { return; } + // Don't overwrite a trial that is already in progress + if (isSubscriptionInProTrial(this._subscription)) return; + const startedOn = new Date(); let days: number; @@ -658,7 +662,11 @@ export class SubscriptionService implements Disposable { SubscriptionPlanId.FreePlus, false, undefined, - data.user.firstGitLensCheckIn != null ? new Date(data.user.firstGitLensCheckIn) : undefined, + data.user.firstGitLensCheckIn != null + ? new Date(data.user.firstGitLensCheckIn) + : data.user.createdDate != null + ? new Date(data.user.createdDate) + : undefined, ); } @@ -685,9 +693,7 @@ export class SubscriptionService implements Disposable { ); } - if (effective == null) { - effective = { ...actual }; - } else if (getSubscriptionPlanPriority(actual.id) >= getSubscriptionPlanPriority(effective.id)) { + if (effective == null || getSubscriptionPlanPriority(actual.id) >= getSubscriptionPlanPriority(effective.id)) { effective = { ...actual }; } diff --git a/src/subscription.ts b/src/subscription.ts index 7c083df..90f1065 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -206,6 +206,19 @@ export function isSubscriptionTrial(subscription: Optional): boolean { + if ( + subscription.account == null || + !isSubscriptionTrial(subscription) || + isSubscriptionPreviewTrialExpired(subscription) === false + ) { + return false; + } + + const remaining = getSubscriptionTimeRemaining(subscription); + return remaining != null ? remaining <= 0 : true; +} + export function isSubscriptionPreviewTrialExpired(subscription: Optional): boolean | undefined { const remaining = getTimeRemaining(subscription.previewTrial?.expiresOn); return remaining != null ? remaining <= 0 : undefined;