All files / lib/subscription tiers.ts

97.39% Statements 187/192
92.3% Branches 36/39
100% Functions 8/8
97.39% Lines 187/192

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265                                                      1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x         1x 126x 126x         1x 31x 31x     31x 5x 5x   26x 26x         1x 4x 4x         1x 54x 54x 54x 54x 54x   54x 54x 18x 54x 16x 54x 13x 54x 3x 54x 3x 54x 1x 54x 54x         1x 11x 11x 11x 11x 11x   11x 11x 11x 6x 6x 11x 2x 2x 11x 1x 1x 11x 1x 1x 11x     11x 1x 11x   11x 9x 9x         1x 7x 7x 7x 7x     7x 16x 7x     7x 7x 1x 7x 1x 1x 6x 6x 1x 7x 1x 1x 5x 5x   7x     5x 5x 2x 7x 2x 2x 7x 16x   3x 3x         1x 8x 8x         1x 9x 9x  
/**
 * Subscription Tier Configuration
 *
 * Defines feature limits and capabilities for each subscription tier
 */
 
export type SubscriptionTier = 'free' | 'basic' | 'pro' | 'premium';
 
export interface TierLimits {
  maxSamples: number;
  maxTestimonials: number;
  maxPackages: number;
  maxWorkExperiences: number;
  maxTestimonialRequests: number;
  features: {
    analytics: 'basic' | 'advanced' | 'full';
    customUrl: boolean;
    removeBranding: boolean;
    verifiedBadge: boolean;
    featuredInDirectory: boolean;
    prioritySupport: boolean;
    apiAccess: boolean;
    whatsappButton: boolean;
    seoOptimization: boolean;
  };
}
 
export const TIER_LIMITS: Record<SubscriptionTier, TierLimits> = {
  free: {
    maxSamples: 1,
    maxTestimonials: 0,
    maxPackages: 1,
    maxWorkExperiences: 1,
    maxTestimonialRequests: 0,
    features: {
      analytics: 'basic',
      customUrl: false,
      removeBranding: false,
      verifiedBadge: false,
      featuredInDirectory: false,
      prioritySupport: false,
      apiAccess: false,
      whatsappButton: true,
      seoOptimization: false,
    },
  },
  basic: {
    maxSamples: 5,
    maxTestimonials: 3,
    maxPackages: 3,
    maxWorkExperiences: 3,
    maxTestimonialRequests: 10,
    features: {
      analytics: 'basic',
      customUrl: false,
      removeBranding: false,
      verifiedBadge: false,
      featuredInDirectory: false,
      prioritySupport: false,
      apiAccess: false,
      whatsappButton: true,
      seoOptimization: false,
    },
  },
  pro: {
    maxSamples: 15,
    maxTestimonials: 10,
    maxPackages: 10,
    maxWorkExperiences: 10,
    maxTestimonialRequests: 30,
    features: {
      analytics: 'advanced',
      customUrl: true,
      removeBranding: true,
      verifiedBadge: false,
      featuredInDirectory: false,
      prioritySupport: true,
      apiAccess: false,
      whatsappButton: true,
      seoOptimization: true,
    },
  },
  premium: {
    maxSamples: Infinity,
    maxTestimonials: Infinity,
    maxPackages: Infinity,
    maxWorkExperiences: Infinity,
    maxTestimonialRequests: Infinity,
    features: {
      analytics: 'full',
      customUrl: true,
      removeBranding: true,
      verifiedBadge: true,
      featuredInDirectory: true,
      prioritySupport: true,
      apiAccess: true,
      whatsappButton: true,
      seoOptimization: true,
    },
  },
};
 
export const TIER_NAMES: Record<SubscriptionTier, string> = {
  free: 'Free',
  basic: 'Basic',
  pro: 'Pro',
  premium: 'Premium',
};
 
export const TIER_ORDER: Record<SubscriptionTier, number> = {
  free: 0,
  basic: 1,
  pro: 2,
  premium: 3,
};
 
/**
 * Get the limits for a specific tier
 */
export function getTierLimits(tier: SubscriptionTier): TierLimits {
  return TIER_LIMITS[tier] || TIER_LIMITS.free;
}
 
/**
 * Check if a tier has access to a specific feature
 */
export function hasFeature(tier: SubscriptionTier, feature: keyof TierLimits['features']): boolean {
  const limits = getTierLimits(tier);
  const value = limits.features[feature];
 
  // Handle analytics specially
  if (feature === 'analytics') {
    return value !== 'basic';
  }
 
  return !!value;
}
 
/**
 * Get the analytics level for a tier
 */
export function getAnalyticsLevel(tier: SubscriptionTier): 'basic' | 'advanced' | 'full' {
  return getTierLimits(tier).features.analytics;
}
 
/**
 * Check if user can add more items of a type
 */
export function canAddMore(
  tier: SubscriptionTier,
  itemType: 'samples' | 'testimonials' | 'packages' | 'workExperiences' | 'testimonialRequests',
  currentCount: number
): boolean {
  const limits = getTierLimits(tier);
 
  switch (itemType) {
    case 'samples':
      return currentCount < limits.maxSamples;
    case 'testimonials':
      return currentCount < limits.maxTestimonials;
    case 'packages':
      return currentCount < limits.maxPackages;
    case 'workExperiences':
      return currentCount < limits.maxWorkExperiences;
    case 'testimonialRequests':
      return currentCount < limits.maxTestimonialRequests;
    default:
      return false;
  }
}
 
/**
 * Get the remaining count for an item type
 */
export function getRemainingCount(
  tier: SubscriptionTier,
  itemType: 'samples' | 'testimonials' | 'packages' | 'workExperiences' | 'testimonialRequests',
  currentCount: number
): number {
  const limits = getTierLimits(tier);
 
  let max: number;
  switch (itemType) {
    case 'samples':
      max = limits.maxSamples;
      break;
    case 'testimonials':
      max = limits.maxTestimonials;
      break;
    case 'packages':
      max = limits.maxPackages;
      break;
    case 'workExperiences':
      max = limits.maxWorkExperiences;
      break;
    case 'testimonialRequests':
      max = limits.maxTestimonialRequests;
      break;
    default:
      max = 0;
  }
 
  if (max === Infinity) return Infinity;
  return Math.max(0, max - currentCount);
}
 
/**
 * Get upgrade suggestion based on what the user is trying to do
 */
export function getUpgradeSuggestion(
  currentTier: SubscriptionTier,
  neededFeature: string
): SubscriptionTier | null {
  const order = TIER_ORDER[currentTier];
 
  // Find the lowest tier that has this feature
  for (const tier of ['basic', 'pro', 'premium'] as SubscriptionTier[]) {
    if (TIER_ORDER[tier] > order) {
      const limits = getTierLimits(tier);
 
      // Check if this tier has the needed feature
      if (
        neededFeature === 'moreSamples' &&
        limits.maxSamples > getTierLimits(currentTier).maxSamples
      ) {
        return tier;
      }
      if (
        neededFeature === 'moreTestimonials' &&
        limits.maxTestimonials > getTierLimits(currentTier).maxTestimonials
      ) {
        return tier;
      }
      if (
        neededFeature === 'morePackages' &&
        limits.maxPackages > getTierLimits(currentTier).maxPackages
      ) {
        return tier;
      }
      if (
        neededFeature in limits.features &&
        limits.features[neededFeature as keyof TierLimits['features']]
      ) {
        return tier;
      }
    }
  }
 
  return null;
}
 
/**
 * Compare two tiers
 */
export function compareTiers(tier1: SubscriptionTier, tier2: SubscriptionTier): number {
  return TIER_ORDER[tier1] - TIER_ORDER[tier2];
}
 
/**
 * Check if tier1 is higher than or equal to tier2
 */
export function isAtLeast(tier: SubscriptionTier, requiredTier: SubscriptionTier): boolean {
  return TIER_ORDER[tier] >= TIER_ORDER[requiredTier];
}