Files
bakery-ia/frontend/src/api/services/subscription.ts

388 lines
11 KiB
TypeScript
Raw Normal View History

import { apiClient } from '../client';
2025-09-20 08:59:12 +02:00
import {
// New types
SubscriptionTier,
SUBSCRIPTION_TIERS,
BillingCycle,
PlanMetadata,
2025-09-20 08:59:12 +02:00
AvailablePlans,
UsageSummary,
FeatureCheckResponse,
QuotaCheckResponse,
2025-09-20 08:59:12 +02:00
PlanUpgradeValidation,
2025-09-23 22:11:34 +02:00
PlanUpgradeResult,
doesPlanMeetMinimum,
getPlanColor,
getYearlyDiscountPercentage,
PLAN_HIERARCHY,
// Analytics levels
2025-09-23 22:11:34 +02:00
ANALYTICS_LEVELS,
AnalyticsLevel,
ANALYTICS_HIERARCHY
} from '../types/subscription';
// Map plan tiers to analytics levels based on backend data
const TIER_TO_ANALYTICS_LEVEL: Record<SubscriptionTier, AnalyticsLevel> = {
[SUBSCRIPTION_TIERS.STARTER]: ANALYTICS_LEVELS.BASIC,
[SUBSCRIPTION_TIERS.PROFESSIONAL]: ANALYTICS_LEVELS.ADVANCED,
[SUBSCRIPTION_TIERS.ENTERPRISE]: ANALYTICS_LEVELS.PREDICTIVE
2025-09-23 22:11:34 +02:00
};
// Cache for available plans
let cachedPlans: AvailablePlans | null = null;
let lastFetchTime: number | null = null;
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes
export class SubscriptionService {
2025-10-06 15:27:01 +02:00
private readonly baseUrl = '/tenants';
private readonly plansUrl = '/plans';
// ============================================================================
// NEW METHODS - Centralized Plans API
// ============================================================================
2025-10-30 21:08:07 +01:00
/**
* Invalidate cached plan data
* Call this when subscription changes to ensure fresh data on next fetch
*/
invalidateCache(): void {
cachedPlans = null;
lastFetchTime = null;
}
/**
* Fetch available subscription plans with complete metadata
* Uses cached data if available and fresh (5 min cache)
*/
async fetchAvailablePlans(): Promise<AvailablePlans> {
const now = Date.now();
// Return cached data if it's still valid
if (cachedPlans && lastFetchTime && (now - lastFetchTime) < CACHE_DURATION) {
return cachedPlans;
}
try {
const plans = await apiClient.get<AvailablePlans>(this.plansUrl);
cachedPlans = plans;
lastFetchTime = now;
return plans;
} catch (error) {
console.error('Failed to fetch subscription plans:', error);
throw error;
}
}
/**
* Get metadata for a specific plan tier
*/
async getPlanMetadata(tier: SubscriptionTier): Promise<PlanMetadata | null> {
try {
const plans = await this.fetchAvailablePlans();
return plans.plans[tier] || null;
} catch (error) {
console.error('Failed to get plan metadata:', error);
return null;
}
}
/**
* Get all available features for a tier
*/
async getPlanFeatures(tier: SubscriptionTier): Promise<string[]> {
try {
const metadata = await this.getPlanMetadata(tier);
return metadata?.features || [];
} catch (error) {
console.error('Failed to get plan features:', error);
return [];
}
}
/**
* Check if a feature is available in a tier
*/
async hasFeatureInTier(tier: SubscriptionTier, featureName: string): Promise<boolean> {
try {
const features = await this.getPlanFeatures(tier);
return features.includes(featureName);
} catch (error) {
console.error('Failed to check feature availability:', error);
return false;
}
}
/**
* Get plan comparison data for pricing page
*/
async getPlanComparison(): Promise<{
tiers: SubscriptionTier[];
metadata: Record<SubscriptionTier, PlanMetadata>;
}> {
try {
const plans = await this.fetchAvailablePlans();
return {
tiers: [
SUBSCRIPTION_TIERS.STARTER,
SUBSCRIPTION_TIERS.PROFESSIONAL,
SUBSCRIPTION_TIERS.ENTERPRISE
],
metadata: plans.plans
};
} catch (error) {
console.error('Failed to get plan comparison:', error);
throw error;
}
}
/**
* Calculate savings for yearly billing
*/
calculateYearlySavings(monthlyPrice: number, yearlyPrice: number): {
savingsAmount: number;
savingsPercentage: number;
monthsFree: number;
} {
const yearlyAnnual = monthlyPrice * 12;
const savingsAmount = yearlyAnnual - yearlyPrice;
const savingsPercentage = getYearlyDiscountPercentage(monthlyPrice, yearlyPrice);
const monthsFree = Math.round(savingsAmount / monthlyPrice);
return {
savingsAmount,
savingsPercentage,
monthsFree
};
}
/**
* Check if user's plan meets minimum requirement
*/
checkPlanMeetsMinimum(userPlan: SubscriptionTier, requiredPlan: SubscriptionTier): boolean {
return doesPlanMeetMinimum(userPlan, requiredPlan);
}
/**
* Get plan display color
*/
getPlanDisplayColor(tier: SubscriptionTier): string {
return getPlanColor(tier);
}
// ============================================================================
// TENANT SUBSCRIPTION STATUS & USAGE
// ============================================================================
/**
* Get current usage summary for a tenant
*/
async getUsageSummary(tenantId: string): Promise<UsageSummary> {
return apiClient.get<UsageSummary>(`${this.baseUrl}/subscriptions/${tenantId}/usage`);
}
/**
* Check if tenant has access to a specific feature
*/
async checkFeatureAccess(
2025-10-06 15:27:01 +02:00
tenantId: string,
featureName: string
): Promise<FeatureCheckResponse> {
return apiClient.get<FeatureCheckResponse>(
2025-10-06 15:27:01 +02:00
`${this.baseUrl}/subscriptions/${tenantId}/features/${featureName}/check`
);
}
/**
* Check if tenant can perform an action within quota limits
*/
2025-10-27 16:33:26 +01:00
async checkQuotaLimit(
2025-10-06 15:27:01 +02:00
tenantId: string,
quotaType: string,
requestedAmount?: number
): Promise<QuotaCheckResponse> {
2025-10-27 16:33:26 +01:00
// Map quotaType to the existing endpoints in tenant_operations.py
let endpoint: string;
switch (quotaType) {
case 'inventory_items':
endpoint = 'can-add-product';
break;
case 'users':
endpoint = 'can-add-user';
break;
case 'locations':
endpoint = 'can-add-location';
break;
default:
throw new Error(`Unsupported quota type: ${quotaType}`);
}
2025-10-27 16:33:26 +01:00
const url = `${this.baseUrl}/subscriptions/${tenantId}/${endpoint}`;
// Get the response from the endpoint (returns different format than expected)
const response = await apiClient.get<{
can_add: boolean;
current_count?: number;
max_allowed?: number;
reason?: string;
message?: string;
}>(url);
// Map the response to QuotaCheckResponse format
return {
allowed: response.can_add,
current: response.current_count || 0,
limit: response.max_allowed || null,
remaining: response.max_allowed !== undefined && response.current_count !== undefined
? response.max_allowed - response.current_count
: null,
message: response.reason || response.message || ''
};
2025-09-20 08:59:12 +02:00
}
async validatePlanUpgrade(tenantId: string, planKey: string): Promise<PlanUpgradeValidation> {
2025-10-07 07:15:07 +02:00
return apiClient.get<PlanUpgradeValidation>(`${this.baseUrl}/subscriptions/${tenantId}/validate-upgrade/${planKey}`);
2025-09-20 08:59:12 +02:00
}
async upgradePlan(tenantId: string, planKey: string): Promise<PlanUpgradeResult> {
2025-10-07 07:15:07 +02:00
return apiClient.post<PlanUpgradeResult>(`${this.baseUrl}/subscriptions/${tenantId}/upgrade?new_plan=${planKey}`, {});
2025-09-21 13:27:50 +02:00
}
async canAddLocation(tenantId: string): Promise<{ can_add: boolean; reason?: string; current_count?: number; max_allowed?: number }> {
2025-10-07 07:15:07 +02:00
return apiClient.get(`${this.baseUrl}/subscriptions/${tenantId}/can-add-location`);
2025-09-21 13:27:50 +02:00
}
async canAddProduct(tenantId: string): Promise<{ can_add: boolean; reason?: string; current_count?: number; max_allowed?: number }> {
2025-10-07 07:15:07 +02:00
return apiClient.get(`${this.baseUrl}/subscriptions/${tenantId}/can-add-product`);
2025-09-21 13:27:50 +02:00
}
async canAddUser(tenantId: string): Promise<{ can_add: boolean; reason?: string; current_count?: number; max_allowed?: number }> {
2025-10-07 07:15:07 +02:00
return apiClient.get(`${this.baseUrl}/subscriptions/${tenantId}/can-add-user`);
2025-09-21 13:27:50 +02:00
}
async hasFeature(tenantId: string, featureName: string): Promise<{ has_feature: boolean; feature_value?: any; plan?: string; reason?: string }> {
2025-10-07 07:15:07 +02:00
return apiClient.get(`${this.baseUrl}/subscriptions/${tenantId}/features/${featureName}`);
2025-09-20 08:59:12 +02:00
}
formatPrice(amount: number): string {
return new Intl.NumberFormat('es-ES', {
style: 'currency',
currency: 'EUR',
minimumFractionDigits: 0,
maximumFractionDigits: 2
}).format(amount);
}
2025-09-23 22:11:34 +02:00
/**
* Get plan display information
*/
async getPlanDisplayInfo(planKey: string) {
try {
const plans = await this.fetchAvailablePlans();
const plan = plans.plans[planKey as SubscriptionTier];
2025-09-23 22:11:34 +02:00
if (plan) {
return {
name: plan.name,
color: this.getPlanColor(planKey as SubscriptionTier),
2025-09-23 22:11:34 +02:00
description: plan.description,
monthlyPrice: plan.monthly_price
};
}
2025-09-23 22:11:34 +02:00
return { name: 'Desconocido', color: 'gray', description: '', monthlyPrice: 0 };
} catch (error) {
console.error('Failed to get plan display info:', error);
return { name: 'Desconocido', color: 'gray', description: '', monthlyPrice: 0 };
}
}
/**
* Get plan color based on plan key
*/
getPlanColor(planKey: string): string {
switch (planKey) {
case SUBSCRIPTION_TIERS.STARTER:
2025-09-23 22:11:34 +02:00
return 'blue';
case SUBSCRIPTION_TIERS.PROFESSIONAL:
2025-09-23 22:11:34 +02:00
return 'purple';
case SUBSCRIPTION_TIERS.ENTERPRISE:
2025-09-23 22:11:34 +02:00
return 'amber';
default:
return 'gray';
}
}
/**
* Get analytics level for a plan tier
2025-09-23 22:11:34 +02:00
*/
getAnalyticsLevelForTier(tier: SubscriptionTier): AnalyticsLevel {
return TIER_TO_ANALYTICS_LEVEL[tier] || ANALYTICS_LEVELS.NONE;
2025-09-23 22:11:34 +02:00
}
/**
* Get analytics level for a plan (alias for getAnalyticsLevelForTier)
* @deprecated Use getAnalyticsLevelForTier instead
2025-09-23 22:11:34 +02:00
*/
getAnalyticsLevelForPlan(tier: SubscriptionTier): AnalyticsLevel {
return this.getAnalyticsLevelForTier(tier);
2025-09-23 22:11:34 +02:00
}
/**
* Check if analytics level meets minimum requirements
*/
doesAnalyticsLevelMeetMinimum(level: AnalyticsLevel, minimumRequired: AnalyticsLevel): boolean {
return ANALYTICS_HIERARCHY[level] >= ANALYTICS_HIERARCHY[minimumRequired];
}
2025-10-16 07:28:04 +02:00
/**
* Cancel subscription - Downgrade to read-only mode
*/
async cancelSubscription(tenantId: string, reason?: string): Promise<{
success: boolean;
message: string;
status: string;
cancellation_effective_date: string;
days_remaining: number;
read_only_mode_starts: string;
}> {
return apiClient.post('/subscriptions/cancel', {
tenant_id: tenantId,
reason: reason || ''
});
}
/**
* Reactivate a cancelled or inactive subscription
*/
async reactivateSubscription(tenantId: string, plan: string = 'starter'): Promise<{
success: boolean;
message: string;
status: string;
plan: string;
next_billing_date: string | null;
}> {
return apiClient.post('/subscriptions/reactivate', {
tenant_id: tenantId,
plan
});
}
/**
* Get subscription status including read-only mode info
*/
async getSubscriptionStatus(tenantId: string): Promise<{
tenant_id: string;
status: string;
plan: string;
is_read_only: boolean;
cancellation_effective_date: string | null;
days_until_inactive: number | null;
}> {
return apiClient.get(`/subscriptions/${tenantId}/status`);
}
}
2025-10-27 16:33:26 +01:00
export const subscriptionService = new SubscriptionService();