Files
orion/app/modules/loyalty/static/admin/js/loyalty-analytics.js
Samir Boulahtit 8c8975239a feat(loyalty): fix Google Wallet integration and improve enrollment flow
- Fix Google Wallet class creation: add required issuerName field (merchant name),
  programLogo with default logo fallback, hexBackgroundColor default
- Add default loyalty logo assets (200px + 512px) for programs without custom logos
- Smart retry: skip retries on 400/401/403/404 client errors (not transient)
- Fix enrollment success page: use sessionStorage for wallet URLs instead of
  authenticated API call (self-enrolled customers have no session)
- Hide wallet section on success page when no wallet URLs available
- Wire up T&C modal on enrollment page with program.terms_text
- Add startup validation for Google/Apple Wallet configs in lifespan
- Add admin wallet status dashboard endpoint and UI (moved to service layer)
- Fix Apple Wallet push notifications with real APNs HTTP/2 implementation
- Fix docs: correct enrollment URLs (port, path segments, /v1 prefix)
- Fix test assertion: !loyalty-enroll! → !enrollment!

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 17:32:55 +01:00

200 lines
7.9 KiB
JavaScript

// app/modules/loyalty/static/admin/js/loyalty-analytics.js
// noqa: js-006 - async init pattern is safe, loadData has try/catch
const loyaltyAnalyticsLog = window.LogConfig.loggers.loyaltyAnalytics || window.LogConfig.createLogger('loyaltyAnalytics');
function adminLoyaltyAnalytics() {
return {
...data(),
currentPage: 'loyalty-analytics',
stats: {
total_programs: 0,
active_programs: 0,
total_cards: 0,
active_cards: 0,
transactions_30d: 0,
points_issued_30d: 0,
points_redeemed_30d: 0,
merchants_with_programs: 0,
total_points_issued: 0,
total_points_redeemed: 0,
total_points_balance: 0,
new_this_month: 0,
estimated_liability_cents: 0,
},
locations: [],
// Merchant filter state
selectedMerchant: null,
merchantSearch: '',
merchantResults: [],
showMerchantDropdown: false,
searchingMerchants: false,
// Wallet integration status
walletStatus: null,
walletStatusLoading: false,
loading: false,
error: null,
_searchTimeout: null,
get redemptionRate() {
if (this.stats.points_issued_30d === 0) return 0;
return Math.round((this.stats.points_redeemed_30d / this.stats.points_issued_30d) * 100);
},
get issuedPercentage() {
const total = this.stats.points_issued_30d + this.stats.points_redeemed_30d;
if (total === 0) return 50;
return Math.round((this.stats.points_issued_30d / total) * 100);
},
get redeemedPercentage() {
return 100 - this.issuedPercentage;
},
async init() {
loyaltyAnalyticsLog.info('=== LOYALTY ANALYTICS PAGE INITIALIZING ===');
if (window._loyaltyAnalyticsInitialized) return;
window._loyaltyAnalyticsInitialized = true;
await this.loadStats();
await this.loadWalletStatus();
loyaltyAnalyticsLog.info('=== LOYALTY ANALYTICS PAGE INITIALIZATION COMPLETE ===');
},
async loadStats() {
this.loading = true;
this.error = null;
try {
const response = await apiClient.get('/admin/loyalty/stats');
if (response) {
this.stats = {
total_programs: response.total_programs || 0,
active_programs: response.active_programs || 0,
total_cards: response.total_cards || 0,
active_cards: response.active_cards || 0,
transactions_30d: response.transactions_30d || 0,
points_issued_30d: response.points_issued_30d || 0,
points_redeemed_30d: response.points_redeemed_30d || 0,
merchants_with_programs: response.merchants_with_programs || 0,
total_points_issued: response.total_points_issued || 0,
total_points_redeemed: response.total_points_redeemed || 0,
total_points_balance: response.total_points_balance || 0,
new_this_month: response.new_this_month || 0,
estimated_liability_cents: response.estimated_liability_cents || 0,
};
this.locations = [];
loyaltyAnalyticsLog.info('Platform stats loaded');
}
} catch (error) {
loyaltyAnalyticsLog.error('Failed to load analytics:', error);
this.error = error.message || 'Failed to load analytics';
} finally {
this.loading = false;
}
},
searchMerchants() {
clearTimeout(this._searchTimeout);
this._searchTimeout = setTimeout(async () => {
if (this.merchantSearch.length < 2) {
this.merchantResults = [];
return;
}
this.searchingMerchants = true;
try {
const response = await apiClient.get(`/admin/loyalty/programs?search=${encodeURIComponent(this.merchantSearch)}&limit=10`);
if (response && response.programs) {
this.merchantResults = response.programs.map(p => ({
id: p.merchant_id,
merchant_name: p.merchant_name || p.display_name || `Program #${p.id}`,
loyalty_type: p.loyalty_type,
}));
}
} catch (error) {
loyaltyAnalyticsLog.error('Merchant search failed:', error);
this.merchantResults = [];
} finally {
this.searchingMerchants = false;
}
}, 300);
},
async selectMerchant(item) {
this.selectedMerchant = item;
this.merchantSearch = '';
this.merchantResults = [];
this.showMerchantDropdown = false;
this.loading = true;
this.error = null;
try {
const response = await apiClient.get(`/admin/loyalty/merchants/${item.id}/stats`);
if (response) {
this.stats = {
total_programs: 1,
active_programs: response.program?.is_active ? 1 : 0,
total_cards: response.total_cards || 0,
active_cards: response.active_cards || 0,
transactions_30d: response.transactions_30d || 0,
points_issued_30d: response.points_issued_30d || 0,
points_redeemed_30d: response.points_redeemed_30d || 0,
merchants_with_programs: 1,
total_points_issued: response.total_points_issued || 0,
total_points_redeemed: response.total_points_redeemed || 0,
total_points_balance: (response.total_points_issued || 0) - (response.total_points_redeemed || 0),
new_this_month: response.new_this_month || 0,
estimated_liability_cents: response.estimated_liability_cents || 0,
};
this.locations = response.locations || [];
loyaltyAnalyticsLog.info('Merchant stats loaded for:', item.merchant_name);
}
} catch (error) {
loyaltyAnalyticsLog.error('Failed to load merchant stats:', error);
this.error = error.message || 'Failed to load merchant stats';
} finally {
this.loading = false;
}
},
async clearMerchantFilter() {
this.selectedMerchant = null;
this.merchantSearch = '';
this.merchantResults = [];
await this.loadStats();
},
async loadWalletStatus() {
this.walletStatusLoading = true;
try {
const response = await apiClient.get('/admin/loyalty/wallet-status');
if (response) {
this.walletStatus = response;
loyaltyAnalyticsLog.info('Wallet status loaded');
}
} catch (error) {
loyaltyAnalyticsLog.error('Failed to load wallet status:', error);
} finally {
this.walletStatusLoading = false;
}
},
formatNumber(num) {
if (num === null || num === undefined) return '0';
return new Intl.NumberFormat('en-US').format(num);
}
};
}
if (!window.LogConfig.loggers.loyaltyAnalytics) {
window.LogConfig.loggers.loyaltyAnalytics = window.LogConfig.createLogger('loyaltyAnalytics');
}
loyaltyAnalyticsLog.info('Loyalty analytics module loaded');