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:
173
app/modules/loyalty/static/admin/js/loyalty-company-settings.js
Normal file
173
app/modules/loyalty/static/admin/js/loyalty-company-settings.js
Normal file
@@ -0,0 +1,173 @@
|
||||
// app/modules/loyalty/static/admin/js/loyalty-company-settings.js
|
||||
// noqa: js-006 - async init pattern is safe, loadData has try/catch
|
||||
|
||||
// Use centralized logger
|
||||
const loyaltyCompanySettingsLog = window.LogConfig.loggers.loyaltyCompanySettings || window.LogConfig.createLogger('loyaltyCompanySettings');
|
||||
|
||||
// ============================================
|
||||
// LOYALTY COMPANY SETTINGS FUNCTION
|
||||
// ============================================
|
||||
function adminLoyaltyCompanySettings() {
|
||||
return {
|
||||
// Inherit base layout functionality
|
||||
...data(),
|
||||
|
||||
// Page identifier for sidebar active state
|
||||
currentPage: 'loyalty-programs',
|
||||
|
||||
// Company ID from URL
|
||||
companyId: null,
|
||||
|
||||
// Company data
|
||||
company: null,
|
||||
|
||||
// Settings form data
|
||||
settings: {
|
||||
staff_pin_policy: 'optional',
|
||||
staff_pin_lockout_attempts: 5,
|
||||
staff_pin_lockout_minutes: 30,
|
||||
allow_self_enrollment: true,
|
||||
allow_void_transactions: true,
|
||||
allow_cross_location_redemption: true
|
||||
},
|
||||
|
||||
// State
|
||||
loading: false,
|
||||
saving: false,
|
||||
error: null,
|
||||
|
||||
// Back URL
|
||||
get backUrl() {
|
||||
return `/admin/loyalty/companies/${this.companyId}`;
|
||||
},
|
||||
|
||||
// Initialize
|
||||
async init() {
|
||||
loyaltyCompanySettingsLog.info('=== LOYALTY COMPANY SETTINGS PAGE INITIALIZING ===');
|
||||
|
||||
// Prevent multiple initializations
|
||||
if (window._loyaltyCompanySettingsInitialized) {
|
||||
loyaltyCompanySettingsLog.warn('Loyalty company settings page already initialized, skipping...');
|
||||
return;
|
||||
}
|
||||
window._loyaltyCompanySettingsInitialized = true;
|
||||
|
||||
// Extract company ID from URL
|
||||
const pathParts = window.location.pathname.split('/');
|
||||
const companiesIndex = pathParts.indexOf('companies');
|
||||
if (companiesIndex !== -1 && pathParts[companiesIndex + 1]) {
|
||||
this.companyId = parseInt(pathParts[companiesIndex + 1]);
|
||||
}
|
||||
|
||||
if (!this.companyId) {
|
||||
this.error = 'Invalid company ID';
|
||||
loyaltyCompanySettingsLog.error('Could not extract company ID from URL');
|
||||
return;
|
||||
}
|
||||
|
||||
loyaltyCompanySettingsLog.info('Company ID:', this.companyId);
|
||||
|
||||
loyaltyCompanySettingsLog.group('Loading company settings data');
|
||||
await this.loadData();
|
||||
loyaltyCompanySettingsLog.groupEnd();
|
||||
|
||||
loyaltyCompanySettingsLog.info('=== LOYALTY COMPANY SETTINGS PAGE INITIALIZATION COMPLETE ===');
|
||||
},
|
||||
|
||||
// Load all data
|
||||
async loadData() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
// Load company info and settings in parallel
|
||||
await Promise.all([
|
||||
this.loadCompany(),
|
||||
this.loadSettings()
|
||||
]);
|
||||
} catch (error) {
|
||||
loyaltyCompanySettingsLog.error('Failed to load data:', error);
|
||||
this.error = error.message || 'Failed to load settings';
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
// Load company basic info
|
||||
async loadCompany() {
|
||||
try {
|
||||
loyaltyCompanySettingsLog.info('Fetching company info...');
|
||||
|
||||
const response = await apiClient.get(`/admin/companies/${this.companyId}`);
|
||||
|
||||
if (response) {
|
||||
this.company = response;
|
||||
loyaltyCompanySettingsLog.info('Company loaded:', this.company.name);
|
||||
}
|
||||
} catch (error) {
|
||||
loyaltyCompanySettingsLog.error('Failed to load company:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// Load settings
|
||||
async loadSettings() {
|
||||
try {
|
||||
loyaltyCompanySettingsLog.info('Fetching company loyalty settings...');
|
||||
|
||||
const response = await apiClient.get(`/admin/loyalty/companies/${this.companyId}/settings`);
|
||||
|
||||
if (response) {
|
||||
// Merge with defaults to ensure all fields exist
|
||||
this.settings = {
|
||||
staff_pin_policy: response.staff_pin_policy || 'optional',
|
||||
staff_pin_lockout_attempts: response.staff_pin_lockout_attempts || 5,
|
||||
staff_pin_lockout_minutes: response.staff_pin_lockout_minutes || 30,
|
||||
allow_self_enrollment: response.allow_self_enrollment !== false,
|
||||
allow_void_transactions: response.allow_void_transactions !== false,
|
||||
allow_cross_location_redemption: response.allow_cross_location_redemption !== false
|
||||
};
|
||||
|
||||
loyaltyCompanySettingsLog.info('Settings loaded:', this.settings);
|
||||
}
|
||||
} catch (error) {
|
||||
loyaltyCompanySettingsLog.warn('Failed to load settings, using defaults:', error.message);
|
||||
// Keep default settings
|
||||
}
|
||||
},
|
||||
|
||||
// Save settings
|
||||
async saveSettings() {
|
||||
this.saving = true;
|
||||
|
||||
try {
|
||||
loyaltyCompanySettingsLog.info('Saving company loyalty settings...');
|
||||
|
||||
const response = await apiClient.patch(
|
||||
`/admin/loyalty/companies/${this.companyId}/settings`,
|
||||
this.settings
|
||||
);
|
||||
|
||||
if (response) {
|
||||
loyaltyCompanySettingsLog.info('Settings saved successfully');
|
||||
Utils.showToast('Settings saved successfully', 'success');
|
||||
|
||||
// Navigate back to company detail
|
||||
window.location.href = this.backUrl;
|
||||
}
|
||||
} catch (error) {
|
||||
loyaltyCompanySettingsLog.error('Failed to save settings:', error);
|
||||
Utils.showToast(`Failed to save settings: ${error.message}`, 'error');
|
||||
} finally {
|
||||
this.saving = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Register logger for configuration
|
||||
if (!window.LogConfig.loggers.loyaltyCompanySettings) {
|
||||
window.LogConfig.loggers.loyaltyCompanySettings = window.LogConfig.createLogger('loyaltyCompanySettings');
|
||||
}
|
||||
|
||||
loyaltyCompanySettingsLog.info('Loyalty company settings module loaded');
|
||||
Reference in New Issue
Block a user