All files / lib/subscription downgrade.ts

100% Statements 96/96
96.42% Branches 27/28
100% Functions 6/6
100% Lines 96/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641x                                                                         1x 13x 13x 13x 13x 13x         1x 4x 4x 4x 4x 4x         1x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x         1x 5x 5x 3x 2x   5x         1x 9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 9x 9x 9x 9x 9x   9x 81x 81x 44x 44x 81x     9x 9x 8x 8x   9x 9x         1x 3x 3x         3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
 * Subscription Downgrade Logic
 *
 * Handles plan downgrade operations including impact analysis and content limiting
 */
 
import { SubscriptionTier, TIER_LIMITS, TIER_ORDER, TIER_NAMES } from './tiers';
 
export interface DowngradeImpact {
  samples: {
    current: number;
    limit: number;
    over_limit: number;
  };
  testimonials: {
    current: number;
    limit: number;
    over_limit: number;
  };
  packages: {
    current: number;
    limit: number;
    over_limit: number;
  };
}
 
export interface DowngradeResult {
  success: boolean;
  error?: string;
  effective_date?: string;
  from_tier?: SubscriptionTier;
  to_tier?: SubscriptionTier;
}
 
/**
 * Check if a user can downgrade from one tier to another
 */
export function canDowngradeTo(
  currentTier: SubscriptionTier,
  targetTier: SubscriptionTier
): boolean {
  return TIER_ORDER[currentTier] > TIER_ORDER[targetTier];
}
 
/**
 * Get all valid downgrade options for a tier
 */
export function getDowngradeOptions(currentTier: SubscriptionTier): SubscriptionTier[] {
  const currentOrder = TIER_ORDER[currentTier];
  return (Object.keys(TIER_ORDER) as SubscriptionTier[]).filter(
    (tier) => TIER_ORDER[tier] < currentOrder
  );
}
 
/**
 * Calculate the impact of downgrading to a specific tier
 */
export function calculateDowngradeImpact(
  targetTier: SubscriptionTier,
  currentCounts: { samples: number; testimonials: number; packages: number }
): DowngradeImpact {
  const limits = TIER_LIMITS[targetTier];
 
  return {
    samples: {
      current: currentCounts.samples,
      limit: limits.maxSamples === Infinity ? 999999 : limits.maxSamples,
      over_limit: Math.max(
        0,
        currentCounts.samples - (limits.maxSamples === Infinity ? 999999 : limits.maxSamples)
      ),
    },
    testimonials: {
      current: currentCounts.testimonials,
      limit: limits.maxTestimonials === Infinity ? 999999 : limits.maxTestimonials,
      over_limit: Math.max(
        0,
        currentCounts.testimonials -
          (limits.maxTestimonials === Infinity ? 999999 : limits.maxTestimonials)
      ),
    },
    packages: {
      current: currentCounts.packages,
      limit: limits.maxPackages === Infinity ? 999999 : limits.maxPackages,
      over_limit: Math.max(
        0,
        currentCounts.packages - (limits.maxPackages === Infinity ? 999999 : limits.maxPackages)
      ),
    },
  };
}
 
/**
 * Check if downgrade will affect any content
 */
export function hasDowngradeImpact(impact: DowngradeImpact): boolean {
  return (
    impact.samples.over_limit > 0 ||
    impact.testimonials.over_limit > 0 ||
    impact.packages.over_limit > 0
  );
}
 
/**
 * Get features lost when downgrading
 */
export function getLostFeatures(
  currentTier: SubscriptionTier,
  targetTier: SubscriptionTier
): string[] {
  const currentFeatures = TIER_LIMITS[currentTier].features;
  const targetFeatures = TIER_LIMITS[targetTier].features;
  const lost: string[] = [];
 
  const featureNames: Record<string, string> = {
    customUrl: 'Custom URL',
    removeBranding: 'Remove Branding',
    verifiedBadge: 'Verified Badge',
    featuredInDirectory: 'Featured in Directory',
    prioritySupport: 'Priority Support',
    apiAccess: 'API Access',
    seoOptimization: 'SEO Optimization',
  };
 
  for (const [key, value] of Object.entries(currentFeatures)) {
    if (key === 'analytics') continue;
    if (value === true && targetFeatures[key as keyof typeof targetFeatures] !== true) {
      lost.push(featureNames[key] || key);
    }
  }
 
  // Check analytics downgrade
  const analyticsLevels = { basic: 1, advanced: 2, full: 3 };
  if (analyticsLevels[currentFeatures.analytics] > analyticsLevels[targetFeatures.analytics]) {
    lost.push(`Advanced Analytics (downgraded to ${targetFeatures.analytics})`);
  }
 
  return lost;
}
 
/**
 * Format tier comparison for display
 */
export function formatTierComparison(
  currentTier: SubscriptionTier,
  targetTier: SubscriptionTier
): {
  current: { name: string; limits: typeof TIER_LIMITS.free };
  target: { name: string; limits: typeof TIER_LIMITS.free };
  lostFeatures: string[];
} {
  return {
    current: {
      name: TIER_NAMES[currentTier],
      limits: TIER_LIMITS[currentTier],
    },
    target: {
      name: TIER_NAMES[targetTier],
      limits: TIER_LIMITS[targetTier],
    },
    lostFeatures: getLostFeatures(currentTier, targetTier),
  };
}