Files
orion/app/modules/loyalty/static/merchant/js/loyalty-analytics.js
Samir Boulahtit 1d90bfe044 fix(loyalty): align menu item IDs with URL segments for sidebar highlight
The store and merchant init-alpine.js derive currentPage from the URL's
last segment (e.g., /loyalty/program -> 'program'). Loyalty menu items
used prefixed IDs like 'loyalty-program' which never matched, so sidebar
items never highlighted.

Fixed by renaming all store/merchant menu item IDs and JS currentPage
values to match URL segments: program, cards, analytics, transactions,
pins, settings — consistent with how every other module works.

Also reverted the init-alpine.js guard that broke storeCode extraction,
and added missing loyalty.common.contact_admin_setup translation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 18:32:50 +01:00

110 lines
4.2 KiB
JavaScript

// app/modules/loyalty/static/merchant/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 merchantLoyaltyAnalytics() {
return {
...data(),
currentPage: 'analytics',
program: null,
locations: [],
stats: {
total_cards: 0,
active_cards: 0,
new_this_month: 0,
total_points_issued: 0,
total_points_redeemed: 0,
total_points_balance: 0,
points_issued_30d: 0,
points_redeemed_30d: 0,
transactions_30d: 0,
avg_points_per_member: 0,
estimated_liability_cents: 0,
},
loading: false,
error: 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('=== MERCHANT LOYALTY ANALYTICS PAGE INITIALIZING ===');
if (window._loyaltyAnalyticsInitialized) return;
window._loyaltyAnalyticsInitialized = true;
this.loadMenuConfig();
await this.loadStats();
loyaltyAnalyticsLog.info('=== MERCHANT LOYALTY ANALYTICS PAGE INITIALIZATION COMPLETE ===');
},
async loadStats() {
this.loading = true;
this.error = null;
try {
const response = await apiClient.get('/merchants/loyalty/stats');
if (response) {
this.program = response.program || null;
this.locations = response.locations || [];
const totalBalance = (response.total_points_issued || 0) - (response.total_points_redeemed || 0);
const avgPoints = response.active_cards > 0
? Math.round(totalBalance / response.active_cards * 100) / 100
: 0;
this.stats = {
total_cards: response.total_cards || 0,
active_cards: response.active_cards || 0,
new_this_month: response.new_this_month || 0,
total_points_issued: response.total_points_issued || 0,
total_points_redeemed: response.total_points_redeemed || 0,
total_points_balance: totalBalance,
points_issued_30d: response.points_issued_30d || 0,
points_redeemed_30d: response.points_redeemed_30d || 0,
transactions_30d: response.transactions_30d || 0,
avg_points_per_member: avgPoints,
estimated_liability_cents: response.estimated_liability_cents || 0,
};
loyaltyAnalyticsLog.info('Merchant stats loaded');
}
} catch (error) {
if (error.status === 404) {
loyaltyAnalyticsLog.info('No program found');
this.program = null;
} else {
loyaltyAnalyticsLog.error('Failed to load stats:', error);
this.error = error.message || 'Failed to load analytics';
}
} finally {
this.loading = 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('Merchant loyalty analytics module loaded');