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:
@@ -0,0 +1,87 @@
|
||||
// app/modules/loyalty/static/storefront/js/loyalty-dashboard.js
|
||||
// Customer loyalty dashboard
|
||||
|
||||
function customerLoyaltyDashboard() {
|
||||
return {
|
||||
...data(),
|
||||
|
||||
// Data
|
||||
card: null,
|
||||
program: null,
|
||||
rewards: [],
|
||||
transactions: [],
|
||||
locations: [],
|
||||
|
||||
// UI state
|
||||
loading: false,
|
||||
showBarcode: false,
|
||||
|
||||
async init() {
|
||||
console.log('Customer loyalty dashboard initializing...');
|
||||
await this.loadData();
|
||||
},
|
||||
|
||||
async loadData() {
|
||||
this.loading = true;
|
||||
try {
|
||||
await Promise.all([
|
||||
this.loadCard(),
|
||||
this.loadTransactions()
|
||||
]);
|
||||
} catch (error) {
|
||||
console.error('Failed to load loyalty data:', error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async loadCard() {
|
||||
try {
|
||||
const response = await apiClient.get('/storefront/loyalty/card');
|
||||
if (response) {
|
||||
this.card = response.card;
|
||||
this.program = response.program;
|
||||
this.rewards = response.program?.points_rewards || [];
|
||||
this.locations = response.locations || [];
|
||||
console.log('Loyalty card loaded:', this.card?.card_number);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
console.log('No loyalty card found');
|
||||
this.card = null;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async loadTransactions() {
|
||||
try {
|
||||
const response = await apiClient.get('/storefront/loyalty/transactions?limit=10');
|
||||
if (response && response.transactions) {
|
||||
this.transactions = response.transactions;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to load transactions:', error.message);
|
||||
}
|
||||
},
|
||||
|
||||
formatNumber(num) {
|
||||
if (num == null) return '0';
|
||||
return new Intl.NumberFormat('en-US').format(num);
|
||||
},
|
||||
|
||||
formatDate(dateString) {
|
||||
if (!dateString) return '-';
|
||||
try {
|
||||
return new Date(dateString).toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric'
|
||||
});
|
||||
} catch (e) {
|
||||
return dateString;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
94
app/modules/loyalty/static/storefront/js/loyalty-enroll.js
Normal file
94
app/modules/loyalty/static/storefront/js/loyalty-enroll.js
Normal file
@@ -0,0 +1,94 @@
|
||||
// app/modules/loyalty/static/storefront/js/loyalty-enroll.js
|
||||
// Self-service loyalty enrollment
|
||||
|
||||
function customerLoyaltyEnroll() {
|
||||
return {
|
||||
...data(),
|
||||
|
||||
// Program info
|
||||
program: null,
|
||||
|
||||
// Form data
|
||||
form: {
|
||||
email: '',
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
phone: '',
|
||||
birthday: '',
|
||||
terms_accepted: false,
|
||||
marketing_consent: false
|
||||
},
|
||||
|
||||
// State
|
||||
loading: false,
|
||||
enrolling: false,
|
||||
enrolled: false,
|
||||
enrolledCard: null,
|
||||
error: null,
|
||||
|
||||
async init() {
|
||||
console.log('Customer loyalty enroll initializing...');
|
||||
await this.loadProgram();
|
||||
},
|
||||
|
||||
async loadProgram() {
|
||||
this.loading = true;
|
||||
try {
|
||||
const response = await apiClient.get('/storefront/loyalty/program');
|
||||
if (response) {
|
||||
this.program = response;
|
||||
console.log('Program loaded:', this.program.display_name);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
console.log('No loyalty program available');
|
||||
this.program = null;
|
||||
} else {
|
||||
console.error('Failed to load program:', error);
|
||||
this.error = 'Failed to load program information';
|
||||
}
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async submitEnrollment() {
|
||||
if (!this.form.email || !this.form.first_name || !this.form.terms_accepted) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.enrolling = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await apiClient.post('/storefront/loyalty/enroll', {
|
||||
customer_email: this.form.email,
|
||||
customer_name: [this.form.first_name, this.form.last_name].filter(Boolean).join(' '),
|
||||
customer_phone: this.form.phone || null,
|
||||
customer_birthday: this.form.birthday || null,
|
||||
marketing_email_consent: this.form.marketing_consent,
|
||||
marketing_sms_consent: this.form.marketing_consent
|
||||
});
|
||||
|
||||
if (response) {
|
||||
console.log('Enrollment successful:', response.card_number);
|
||||
// Redirect to success page - extract base path from current URL
|
||||
// Current page is at /storefront/loyalty/join, redirect to /storefront/loyalty/join/success
|
||||
const currentPath = window.location.pathname;
|
||||
const successUrl = currentPath.replace(/\/join\/?$/, '/join/success') +
|
||||
'?card=' + encodeURIComponent(response.card_number);
|
||||
window.location.href = successUrl;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Enrollment failed:', error);
|
||||
if (error.message?.includes('already')) {
|
||||
this.error = 'This email is already registered in our loyalty program.';
|
||||
} else {
|
||||
this.error = error.message || 'Enrollment failed. Please try again.';
|
||||
}
|
||||
} finally {
|
||||
this.enrolling = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
119
app/modules/loyalty/static/storefront/js/loyalty-history.js
Normal file
119
app/modules/loyalty/static/storefront/js/loyalty-history.js
Normal file
@@ -0,0 +1,119 @@
|
||||
// app/modules/loyalty/static/storefront/js/loyalty-history.js
|
||||
// Customer loyalty transaction history
|
||||
|
||||
function customerLoyaltyHistory() {
|
||||
return {
|
||||
...data(),
|
||||
|
||||
// Data
|
||||
card: null,
|
||||
transactions: [],
|
||||
|
||||
// Pagination
|
||||
pagination: {
|
||||
page: 1,
|
||||
per_page: 20,
|
||||
total: 0,
|
||||
pages: 0
|
||||
},
|
||||
|
||||
// State
|
||||
loading: false,
|
||||
|
||||
async init() {
|
||||
console.log('Customer loyalty history initializing...');
|
||||
await this.loadData();
|
||||
},
|
||||
|
||||
async loadData() {
|
||||
this.loading = true;
|
||||
try {
|
||||
await Promise.all([
|
||||
this.loadCard(),
|
||||
this.loadTransactions()
|
||||
]);
|
||||
} catch (error) {
|
||||
console.error('Failed to load history:', error);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async loadCard() {
|
||||
try {
|
||||
const response = await apiClient.get('/storefront/loyalty/card');
|
||||
if (response) {
|
||||
this.card = response.card;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to load card:', error.message);
|
||||
}
|
||||
},
|
||||
|
||||
async loadTransactions() {
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
params.append('skip', (this.pagination.page - 1) * this.pagination.per_page);
|
||||
params.append('limit', this.pagination.per_page);
|
||||
|
||||
const response = await apiClient.get(`/storefront/loyalty/transactions?${params}`);
|
||||
if (response) {
|
||||
this.transactions = response.transactions || [];
|
||||
this.pagination.total = response.total || 0;
|
||||
this.pagination.pages = Math.ceil(this.pagination.total / this.pagination.per_page);
|
||||
console.log(`Loaded ${this.transactions.length} transactions`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load transactions:', error);
|
||||
}
|
||||
},
|
||||
|
||||
previousPage() {
|
||||
if (this.pagination.page > 1) {
|
||||
this.pagination.page--;
|
||||
this.loadTransactions();
|
||||
}
|
||||
},
|
||||
|
||||
nextPage() {
|
||||
if (this.pagination.page < this.pagination.pages) {
|
||||
this.pagination.page++;
|
||||
this.loadTransactions();
|
||||
}
|
||||
},
|
||||
|
||||
getTransactionLabel(tx) {
|
||||
const type = tx.transaction_type || '';
|
||||
const labels = {
|
||||
'points_earned': 'Points Earned',
|
||||
'points_redeemed': 'Reward Redeemed',
|
||||
'points_voided': 'Points Voided',
|
||||
'welcome_bonus': 'Welcome Bonus',
|
||||
'points_expired': 'Points Expired',
|
||||
'stamp_earned': 'Stamp Earned',
|
||||
'stamp_redeemed': 'Stamp Redeemed'
|
||||
};
|
||||
return labels[type] || type.replace(/_/g, ' ');
|
||||
},
|
||||
|
||||
formatNumber(num) {
|
||||
if (num == null) return '0';
|
||||
return new Intl.NumberFormat('en-US').format(num);
|
||||
},
|
||||
|
||||
formatDateTime(dateString) {
|
||||
if (!dateString) return '-';
|
||||
try {
|
||||
return new Date(dateString).toLocaleString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
} catch (e) {
|
||||
return dateString;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user