Files
orion/app/modules/loyalty/static/storefront/js/loyalty-enroll.js
Samir Boulahtit 53dfe018c2
Some checks failed
CI / ruff (push) Successful in 10s
CI / pytest (push) Failing after 46m41s
CI / validate (push) Successful in 23s
CI / dependency-scanning (push) Successful in 30s
CI / docs (push) Has been skipped
CI / deploy (push) Has been skipped
fix: loyalty storefront and store card detail — enrollment, context, and Alpine.js
- Fix storefront enabled_modules always empty (page_context overwrote computed
  set with empty default via extra_context)
- Fix storefront loyalty JS using store's data() instead of shopLayoutData()
- Remove defer from storefront loyalty scripts to prevent Alpine race condition
- Fix enrollment field name mismatch (customer_email → email) in both store
  and storefront JS
- Add self-enrollment customer creation (resolve_customer_id with
  create_if_missing) including hashed_password and customer_number
- Fix card list showing "Unknown" — add customer_name/email to CardResponse
- Add GET /cards/{card_id} detail endpoint for store card detail page
- Fix enroll-success.html using data() instead of shopLayoutData()
- Fix enrollment redirect reading response.card_number instead of
  response.card.card_number

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 14:28:37 +01:00

96 lines
3.4 KiB
JavaScript

// app/modules/loyalty/static/storefront/js/loyalty-enroll.js
// Self-service loyalty enrollment
function customerLoyaltyEnroll() {
return {
...shopLayoutData(),
// 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', {
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;
console.log('Enrollment successful:', cardNumber);
// 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(cardNumber);
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;
}
}
};
}