Some checks failed
- Add admin SQL query tool with saved queries, schema explorer presets, and collapsible category sections (dev_tools module) - Add platform debug tool for admin diagnostics - Add loyalty settings page with owner-only access control - Fix loyalty settings owner check (use currentUser instead of window.__userData) - Replace HTTPException with AuthorizationException in loyalty routes - Expand loyalty module with PIN service, Apple Wallet, program management - Improve store login with platform detection and multi-platform support - Update billing feature gates and subscription services - Add store platform sync improvements and remove is_primary column - Add unit tests for loyalty (PIN, points, stamps, program services) - Update i18n translations across dev_tools locales Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
183 lines
6.7 KiB
JavaScript
183 lines
6.7 KiB
JavaScript
// app/modules/loyalty/static/store/js/loyalty-settings.js
|
|
// noqa: js-006 - async init pattern is safe, loadData has try/catch
|
|
|
|
const loyaltySettingsLog = window.LogConfig.loggers.loyaltySettings || window.LogConfig.createLogger('loyaltySettings');
|
|
|
|
// ============================================
|
|
// STORE LOYALTY SETTINGS FUNCTION
|
|
// ============================================
|
|
function loyaltySettings() {
|
|
return {
|
|
// Inherit base layout functionality
|
|
...data(),
|
|
|
|
// Page identifier
|
|
currentPage: 'loyalty-settings',
|
|
|
|
// State
|
|
program: null,
|
|
loading: false,
|
|
saving: false,
|
|
error: null,
|
|
isOwner: false,
|
|
|
|
// Form data
|
|
form: {
|
|
loyalty_type: 'points',
|
|
stamps_target: 10,
|
|
stamps_reward_description: 'Free item',
|
|
stamps_reward_value_cents: null,
|
|
points_per_euro: 10,
|
|
welcome_bonus_points: 0,
|
|
minimum_redemption_points: 100,
|
|
minimum_purchase_cents: 0,
|
|
points_expiration_days: null,
|
|
points_rewards: [],
|
|
cooldown_minutes: 15,
|
|
max_daily_stamps: 5,
|
|
require_staff_pin: true,
|
|
card_name: '',
|
|
card_color: '#4F46E5',
|
|
logo_url: '',
|
|
terms_text: '',
|
|
},
|
|
|
|
// Initialize
|
|
async init() {
|
|
loyaltySettingsLog.info('=== LOYALTY SETTINGS INITIALIZING ===');
|
|
|
|
if (window._loyaltySettingsInitialized) {
|
|
loyaltySettingsLog.warn('Already initialized, skipping...');
|
|
return;
|
|
}
|
|
window._loyaltySettingsInitialized = true;
|
|
|
|
const parentInit = data().init;
|
|
if (parentInit) {
|
|
await parentInit.call(this);
|
|
}
|
|
|
|
// Check if user is merchant_owner
|
|
this.isOwner = this.currentUser?.role === 'merchant_owner';
|
|
|
|
await this.loadData();
|
|
|
|
loyaltySettingsLog.info('=== LOYALTY SETTINGS INITIALIZATION COMPLETE ===');
|
|
},
|
|
|
|
async loadData() {
|
|
this.loading = true;
|
|
this.error = null;
|
|
|
|
try {
|
|
await this.loadProgram();
|
|
} catch (error) {
|
|
loyaltySettingsLog.error('Failed to load data:', error);
|
|
this.error = error.message || 'Failed to load settings';
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
async loadProgram() {
|
|
try {
|
|
loyaltySettingsLog.info('Loading program...');
|
|
const response = await apiClient.get('/store/loyalty/program');
|
|
|
|
if (response) {
|
|
this.program = response;
|
|
this.populateForm(response);
|
|
loyaltySettingsLog.info('Program loaded:', response.display_name);
|
|
}
|
|
} catch (error) {
|
|
if (error.status === 404) {
|
|
loyaltySettingsLog.info('No program configured — showing create form');
|
|
this.program = null;
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
},
|
|
|
|
populateForm(program) {
|
|
this.form.loyalty_type = program.loyalty_type || 'points';
|
|
this.form.stamps_target = program.stamps_target || 10;
|
|
this.form.stamps_reward_description = program.stamps_reward_description || 'Free item';
|
|
this.form.stamps_reward_value_cents = program.stamps_reward_value_cents || null;
|
|
this.form.points_per_euro = program.points_per_euro || 10;
|
|
this.form.welcome_bonus_points = program.welcome_bonus_points || 0;
|
|
this.form.minimum_redemption_points = program.minimum_redemption_points || 100;
|
|
this.form.minimum_purchase_cents = program.minimum_purchase_cents || 0;
|
|
this.form.points_expiration_days = program.points_expiration_days || null;
|
|
this.form.points_rewards = (program.points_rewards || []).map(r => ({
|
|
id: r.id,
|
|
name: r.name,
|
|
points_required: r.points_required,
|
|
description: r.description || '',
|
|
is_active: r.is_active !== false,
|
|
}));
|
|
this.form.cooldown_minutes = program.cooldown_minutes ?? 15;
|
|
this.form.max_daily_stamps = program.max_daily_stamps || 5;
|
|
this.form.require_staff_pin = program.require_staff_pin !== false;
|
|
this.form.card_name = program.card_name || '';
|
|
this.form.card_color = program.card_color || '#4F46E5';
|
|
this.form.logo_url = program.logo_url || '';
|
|
this.form.terms_text = program.terms_text || '';
|
|
},
|
|
|
|
addReward() {
|
|
const id = 'reward_' + Date.now();
|
|
this.form.points_rewards.push({
|
|
id: id,
|
|
name: '',
|
|
points_required: 100,
|
|
description: '',
|
|
is_active: true,
|
|
});
|
|
},
|
|
|
|
async saveProgram() {
|
|
this.saving = true;
|
|
|
|
try {
|
|
const payload = { ...this.form };
|
|
|
|
// Clean up empty optional fields
|
|
if (!payload.stamps_reward_value_cents) payload.stamps_reward_value_cents = null;
|
|
if (!payload.points_expiration_days) payload.points_expiration_days = null;
|
|
if (!payload.card_name) payload.card_name = null;
|
|
if (!payload.logo_url) payload.logo_url = null;
|
|
if (!payload.terms_text) payload.terms_text = null;
|
|
|
|
let response;
|
|
if (this.program) {
|
|
// Update existing
|
|
response = await apiClient.put('/store/loyalty/program', payload);
|
|
Utils.showToast('Program updated successfully', 'success');
|
|
} else {
|
|
// Create new
|
|
response = await apiClient.post('/store/loyalty/program', payload);
|
|
Utils.showToast('Program created successfully', 'success');
|
|
}
|
|
|
|
this.program = response;
|
|
this.populateForm(response);
|
|
|
|
loyaltySettingsLog.info('Program saved:', response.display_name);
|
|
} catch (error) {
|
|
Utils.showToast(`Failed to save: ${error.message}`, 'error');
|
|
loyaltySettingsLog.error('Save failed:', error);
|
|
} finally {
|
|
this.saving = false;
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
// Register logger
|
|
if (!window.LogConfig.loggers.loyaltySettings) {
|
|
window.LogConfig.loggers.loyaltySettings = window.LogConfig.createLogger('loyaltySettings');
|
|
}
|
|
|
|
loyaltySettingsLog.info('Loyalty settings module loaded');
|