Files
orion/app/modules/loyalty/static/storefront/js/loyalty-enroll.js
Samir Boulahtit 4a1f71a312
Some checks failed
CI / ruff (push) Successful in 11s
CI / validate (push) Successful in 26s
CI / dependency-scanning (push) Successful in 32s
CI / pytest (push) Failing after 3h8m55s
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
fix(loyalty): resolve critical production readiness issues
- Add pessimistic locking (SELECT FOR UPDATE) on card write operations
  to prevent race conditions in stamp_service and points_service
- Replace 16 console.log/error/warn calls with LogConfig.createLogger()
  in 3 storefront JS files (dashboard, history, enroll)
- Delete all stale lu.json locale files across 8 modules (lb is the
  correct ISO 639-1 code for Luxembourgish)
- Update architecture rules and docs to reference lb.json not lu.json
- Add production-readiness.md report for loyalty module

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 23:18:18 +01:00

103 lines
3.7 KiB
JavaScript

// app/modules/loyalty/static/storefront/js/loyalty-enroll.js
// Self-service loyalty enrollment
const loyaltyEnrollLog = window.LogConfig.loggers.loyaltyEnroll || window.LogConfig.createLogger('loyaltyEnroll');
function customerLoyaltyEnroll() {
return {
...storefrontLayoutData(),
// 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,
showTerms: false,
async init() {
loyaltyEnrollLog.info('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;
loyaltyEnrollLog.info('Program loaded:', this.program.display_name);
}
} catch (error) {
if (error.status === 404) {
loyaltyEnrollLog.info('No loyalty program available');
this.program = null;
} else {
loyaltyEnrollLog.error('Failed to load program:', error);
this.error = I18n.t('loyalty.enrollment.errors.load_failed');
}
} 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', {
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) {
const cardNumber = response.card?.card_number || response.card_number;
loyaltyEnrollLog.info('Enrollment successful:', cardNumber);
// Store wallet URLs for the success page (no auth needed)
if (response.wallet_urls) {
sessionStorage.setItem('loyalty_wallet_urls', JSON.stringify(response.wallet_urls));
}
// Redirect to success page
const currentPath = window.location.pathname;
const successUrl = currentPath.replace(/\/join\/?$/, '/join/success') +
'?card=' + encodeURIComponent(cardNumber);
window.location.href = successUrl;
}
} catch (error) {
loyaltyEnrollLog.error('Enrollment failed:', error);
if (error.message?.includes('already')) {
this.error = I18n.t('loyalty.enrollment.errors.email_exists');
} else {
this.error = error.message || I18n.t('loyalty.enrollment.errors.failed');
}
} finally {
this.enrolling = false;
}
}
};
}