feat(subscriptions): migrate subscription management to merchant level and seed tiers

Move subscription create/edit from store detail (broken endpoint) to merchant
detail page with proper modal UI. Seed 4 subscription tiers (Essential,
Professional, Business, Enterprise) in init_production.py. Also includes
cross-module dependency declarations, store domain platform_id migration,
platform context middleware, CMS route fixes, and migration backups.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-09 21:04:04 +01:00
parent 7feacd5af8
commit 68493dc6cb
97 changed files with 13286 additions and 77 deletions

View File

@@ -16,6 +16,16 @@ function adminMerchantDetail() {
error: null,
merchantId: null,
// Subscription state
subscription: null,
subscriptionTier: null,
usageMetrics: [],
tiers: [],
platformId: null,
showCreateSubscriptionModal: false,
createForm: { tier_code: 'essential', status: 'trial', trial_days: 14, is_annual: false },
creatingSubscription: false,
// Initialize
async init() {
// Load i18n translations
@@ -38,6 +48,10 @@ function adminMerchantDetail() {
this.merchantId = match[1];
merchantDetailLog.info('Viewing merchant:', this.merchantId);
await this.loadMerchant();
await this.loadPlatforms();
if (this.platformId) {
await this.loadSubscription();
}
} else {
merchantDetailLog.error('No merchant ID in URL');
this.error = 'Invalid merchant URL';
@@ -84,6 +98,128 @@ function adminMerchantDetail() {
}
},
// Load platforms and find OMS platform ID
async loadPlatforms() {
try {
const response = await apiClient.get('/admin/platforms');
const platforms = response.items || response;
const oms = platforms.find(p => p.code === 'oms');
if (oms) {
this.platformId = oms.id;
merchantDetailLog.info('OMS platform resolved:', this.platformId);
} else {
merchantDetailLog.warn('OMS platform not found');
}
} catch (error) {
merchantDetailLog.warn('Failed to load platforms:', error.message);
}
},
// Load subscription for this merchant
async loadSubscription() {
if (!this.merchantId || !this.platformId) return;
merchantDetailLog.info('Loading subscription for merchant:', this.merchantId);
try {
const url = `/admin/subscriptions/merchants/${this.merchantId}/platforms/${this.platformId}`;
window.LogConfig.logApiCall('GET', url, null, 'request');
const response = await apiClient.get(url);
window.LogConfig.logApiCall('GET', url, response, 'response');
this.subscription = response.subscription || response;
this.subscriptionTier = response.tier || null;
this.usageMetrics = response.features || [];
merchantDetailLog.info('Subscription loaded:', {
tier: this.subscription?.tier,
status: this.subscription?.status,
features_count: this.usageMetrics.length
});
} catch (error) {
if (error.status === 404) {
merchantDetailLog.info('No subscription found for merchant');
this.subscription = null;
this.usageMetrics = [];
} else {
merchantDetailLog.warn('Failed to load subscription:', error.message);
}
}
},
// Load available subscription tiers
async loadTiers() {
if (this.tiers.length > 0) return;
try {
const response = await apiClient.get('/admin/subscriptions/tiers');
this.tiers = response.items || response;
merchantDetailLog.info('Loaded tiers:', this.tiers.length);
} catch (error) {
merchantDetailLog.warn('Failed to load tiers:', error.message);
}
},
// Open create subscription modal
async openCreateSubscriptionModal() {
await this.loadTiers();
this.createForm = { tier_code: 'essential', status: 'trial', trial_days: 14, is_annual: false };
this.showCreateSubscriptionModal = true;
},
// Create subscription for this merchant
async createSubscription() {
if (!this.merchantId || !this.platformId) return;
this.creatingSubscription = true;
merchantDetailLog.info('Creating subscription for merchant:', this.merchantId);
try {
const url = `/admin/subscriptions/merchants/${this.merchantId}/platforms/${this.platformId}`;
const payload = {
merchant_id: parseInt(this.merchantId),
platform_id: this.platformId,
tier_code: this.createForm.tier_code,
status: this.createForm.status,
trial_days: this.createForm.status === 'trial' ? parseInt(this.createForm.trial_days) : 0,
is_annual: this.createForm.is_annual
};
window.LogConfig.logApiCall('POST', url, payload, 'request');
const response = await apiClient.post(url, payload);
window.LogConfig.logApiCall('POST', url, response, 'response');
this.showCreateSubscriptionModal = false;
Utils.showToast('Subscription created successfully', 'success');
merchantDetailLog.info('Subscription created');
await this.loadSubscription();
} catch (error) {
window.LogConfig.logError(error, 'Create Subscription');
Utils.showToast(error.message || 'Failed to create subscription', 'error');
} finally {
this.creatingSubscription = false;
}
},
// Get usage bar color based on percentage
getUsageBarColor(current, limit) {
if (!limit || limit === 0) return 'bg-blue-500';
const percent = (current / limit) * 100;
if (percent >= 90) return 'bg-red-500';
if (percent >= 75) return 'bg-yellow-500';
return 'bg-green-500';
},
// Format tier price for display
formatTierPrice(tier) {
if (!tier.price_monthly_cents) return 'Custom';
return `${(tier.price_monthly_cents / 100).toFixed(2)}/mo`;
},
// Format date (matches dashboard pattern)
formatDate(dateString) {
if (!dateString) {
@@ -140,6 +276,9 @@ function adminMerchantDetail() {
async refresh() {
merchantDetailLog.info('=== MERCHANT REFRESH TRIGGERED ===');
await this.loadMerchant();
if (this.platformId) {
await this.loadSubscription();
}
Utils.showToast(I18n.t('tenancy.messages.merchant_details_refreshed'), 'success');
merchantDetailLog.info('=== MERCHANT REFRESH COMPLETE ===');
}

View File

@@ -19,7 +19,6 @@ function adminStoreDetail() {
loading: false,
error: null,
storeCode: null,
showSubscriptionModal: false,
// Initialize
async init() {
@@ -150,39 +149,6 @@ function adminStoreDetail() {
return 'bg-green-500';
},
// Create a new subscription for this store
async createSubscription() {
if (!this.store?.id) {
Utils.showToast(I18n.t('tenancy.messages.no_store_loaded'), 'error');
return;
}
detailLog.info('Creating subscription for store:', this.store.id);
try {
// Create a trial subscription with default tier
const url = `/admin/subscriptions/${this.store.id}`;
const data = {
tier: 'essential',
status: 'trial',
trial_days: 14,
is_annual: false
};
window.LogConfig.logApiCall('POST', url, data, 'request');
const response = await apiClient.post(url, data);
window.LogConfig.logApiCall('POST', url, response, 'response');
this.subscription = response;
Utils.showToast(I18n.t('tenancy.messages.subscription_created_successfully'), 'success');
detailLog.info('Subscription created:', this.subscription);
} catch (error) {
window.LogConfig.logError(error, 'Create Subscription');
Utils.showToast(error.message || 'Failed to create subscription', 'error');
}
},
// Delete store
async deleteStore() {
detailLog.info('Delete store requested:', this.storeCode);