feat(loyalty): implement Phase 2 - company-wide points system
Complete implementation of loyalty module Phase 2 features: Database & Models: - Add company_id to LoyaltyProgram for chain-wide loyalty - Add company_id to LoyaltyCard for multi-location support - Add CompanyLoyaltySettings model for admin-controlled settings - Add points expiration, welcome bonus, and minimum redemption fields - Add POINTS_EXPIRED, WELCOME_BONUS transaction types Services: - Update program_service for company-based queries - Update card_service with enrollment and welcome bonus - Update points_service with void_points for returns - Update stamp_service for company context - Update pin_service for company-wide operations API Endpoints: - Admin: Program listing with stats, company detail views - Vendor: Terminal operations, card management, settings - Storefront: Customer card/transactions, self-enrollment UI Templates: - Admin: Programs dashboard, company detail, settings - Vendor: Terminal, cards list, card detail, settings, stats, enrollment - Storefront: Dashboard, history, enrollment, success pages Background Tasks: - Point expiration task (daily, based on inactivity) - Wallet sync task (hourly) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
118
app/modules/loyalty/static/vendor/js/loyalty-settings.js
vendored
Normal file
118
app/modules/loyalty/static/vendor/js/loyalty-settings.js
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
// app/modules/loyalty/static/vendor/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');
|
||||
|
||||
function vendorLoyaltySettings() {
|
||||
return {
|
||||
...data(),
|
||||
currentPage: 'loyalty-settings',
|
||||
|
||||
settings: {
|
||||
loyalty_type: 'points',
|
||||
points_per_euro: 1,
|
||||
welcome_bonus_points: 0,
|
||||
minimum_redemption_points: 100,
|
||||
points_expiration_days: null,
|
||||
points_rewards: [],
|
||||
card_name: '',
|
||||
card_color: '#4F46E5',
|
||||
is_active: true
|
||||
},
|
||||
|
||||
loading: false,
|
||||
saving: false,
|
||||
error: null,
|
||||
isNewProgram: false,
|
||||
|
||||
async init() {
|
||||
loyaltySettingsLog.info('=== LOYALTY SETTINGS PAGE INITIALIZING ===');
|
||||
if (window._loyaltySettingsInitialized) return;
|
||||
window._loyaltySettingsInitialized = true;
|
||||
|
||||
await this.loadSettings();
|
||||
loyaltySettingsLog.info('=== LOYALTY SETTINGS PAGE INITIALIZATION COMPLETE ===');
|
||||
},
|
||||
|
||||
async loadSettings() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await apiClient.get('/vendor/loyalty/program');
|
||||
if (response) {
|
||||
this.settings = {
|
||||
loyalty_type: response.loyalty_type || 'points',
|
||||
points_per_euro: response.points_per_euro || 1,
|
||||
welcome_bonus_points: response.welcome_bonus_points || 0,
|
||||
minimum_redemption_points: response.minimum_redemption_points || 100,
|
||||
points_expiration_days: response.points_expiration_days || null,
|
||||
points_rewards: response.points_rewards || [],
|
||||
card_name: response.card_name || '',
|
||||
card_color: response.card_color || '#4F46E5',
|
||||
is_active: response.is_active !== false
|
||||
};
|
||||
this.isNewProgram = false;
|
||||
loyaltySettingsLog.info('Settings loaded');
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
loyaltySettingsLog.info('No program found, creating new');
|
||||
this.isNewProgram = true;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async saveSettings() {
|
||||
this.saving = true;
|
||||
|
||||
try {
|
||||
// Ensure rewards have IDs
|
||||
this.settings.points_rewards = this.settings.points_rewards.map((r, i) => ({
|
||||
...r,
|
||||
id: r.id || `reward_${i + 1}`,
|
||||
is_active: r.is_active !== false
|
||||
}));
|
||||
|
||||
let response;
|
||||
if (this.isNewProgram) {
|
||||
response = await apiClient.post('/vendor/loyalty/program', this.settings);
|
||||
this.isNewProgram = false;
|
||||
} else {
|
||||
response = await apiClient.patch('/vendor/loyalty/program', this.settings);
|
||||
}
|
||||
|
||||
Utils.showToast('Settings saved successfully', 'success');
|
||||
loyaltySettingsLog.info('Settings saved');
|
||||
} catch (error) {
|
||||
Utils.showToast(`Failed to save: ${error.message}`, 'error');
|
||||
loyaltySettingsLog.error('Save failed:', error);
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
},
|
||||
|
||||
addReward() {
|
||||
this.settings.points_rewards.push({
|
||||
id: `reward_${Date.now()}`,
|
||||
name: '',
|
||||
points_required: 100,
|
||||
description: '',
|
||||
is_active: true
|
||||
});
|
||||
},
|
||||
|
||||
removeReward(index) {
|
||||
this.settings.points_rewards.splice(index, 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (!window.LogConfig.loggers.loyaltySettings) {
|
||||
window.LogConfig.loggers.loyaltySettings = window.LogConfig.createLogger('loyaltySettings');
|
||||
}
|
||||
loyaltySettingsLog.info('Loyalty settings module loaded');
|
||||
Reference in New Issue
Block a user