feat: add SQL query tool, platform debug, loyalty settings, and multi-module improvements
Some checks failed
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>
This commit is contained in:
@@ -59,11 +59,10 @@ function storeLoyaltyCards() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
try {
|
||||
await Promise.all([
|
||||
this.loadProgram(),
|
||||
this.loadCards(),
|
||||
this.loadStats()
|
||||
]);
|
||||
await this.loadProgram();
|
||||
if (this.program) {
|
||||
await Promise.all([this.loadCards(), this.loadStats()]);
|
||||
}
|
||||
} catch (error) {
|
||||
loyaltyCardsLog.error('Failed to load data:', error);
|
||||
this.error = error.message;
|
||||
|
||||
182
app/modules/loyalty/static/store/js/loyalty-settings.js
Normal file
182
app/modules/loyalty/static/store/js/loyalty-settings.js
Normal file
@@ -0,0 +1,182 @@
|
||||
// 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');
|
||||
@@ -8,6 +8,8 @@ function storeLoyaltyStats() {
|
||||
...data(),
|
||||
currentPage: 'loyalty-stats',
|
||||
|
||||
program: null,
|
||||
|
||||
stats: {
|
||||
total_cards: 0,
|
||||
active_cards: 0,
|
||||
@@ -35,10 +37,22 @@ function storeLoyaltyStats() {
|
||||
await parentInit.call(this);
|
||||
}
|
||||
|
||||
await this.loadStats();
|
||||
await this.loadProgram();
|
||||
if (this.program) {
|
||||
await this.loadStats();
|
||||
}
|
||||
loyaltyStatsLog.info('=== LOYALTY STATS PAGE INITIALIZATION COMPLETE ===');
|
||||
},
|
||||
|
||||
async loadProgram() {
|
||||
try {
|
||||
const response = await apiClient.get('/store/loyalty/program');
|
||||
if (response) this.program = response;
|
||||
} catch (error) {
|
||||
if (error.status !== 404) throw error;
|
||||
}
|
||||
},
|
||||
|
||||
async loadStats() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
@@ -191,7 +191,11 @@ function storeLoyaltyTerminal() {
|
||||
this.processing = true;
|
||||
|
||||
try {
|
||||
if (this.pendingAction === 'earn') {
|
||||
if (this.pendingAction === 'stamp') {
|
||||
await this.addStamp();
|
||||
} else if (this.pendingAction === 'redeemStamps') {
|
||||
await this.redeemStamps();
|
||||
} else if (this.pendingAction === 'earn') {
|
||||
await this.earnPoints();
|
||||
} else if (this.pendingAction === 'redeem') {
|
||||
await this.redeemReward();
|
||||
@@ -216,6 +220,30 @@ function storeLoyaltyTerminal() {
|
||||
}
|
||||
},
|
||||
|
||||
// Add stamp
|
||||
async addStamp() {
|
||||
loyaltyTerminalLog.info('Adding stamp...');
|
||||
|
||||
await apiClient.post('/store/loyalty/stamp', {
|
||||
card_id: this.selectedCard.id,
|
||||
staff_pin: this.pinDigits
|
||||
});
|
||||
|
||||
Utils.showToast('Stamp added!', 'success');
|
||||
},
|
||||
|
||||
// Redeem stamps
|
||||
async redeemStamps() {
|
||||
loyaltyTerminalLog.info('Redeeming stamps...');
|
||||
|
||||
await apiClient.post('/store/loyalty/stamp/redeem', {
|
||||
card_id: this.selectedCard.id,
|
||||
staff_pin: this.pinDigits
|
||||
});
|
||||
|
||||
Utils.showToast('Stamps redeemed! Reward earned.', 'success');
|
||||
},
|
||||
|
||||
// Earn points
|
||||
async earnPoints() {
|
||||
loyaltyTerminalLog.info('Earning points...', { amount: this.earnAmount });
|
||||
|
||||
Reference in New Issue
Block a user