Files
orion/app/modules/loyalty/static/store/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

108 lines
3.6 KiB
JavaScript

// app/modules/loyalty/static/store/js/loyalty-enroll.js
// noqa: js-006 - async init pattern is safe, loadData has try/catch
const loyaltyEnrollLog = window.LogConfig.loggers.loyaltyEnroll || window.LogConfig.createLogger('loyaltyEnroll');
function storeLoyaltyEnroll() {
return {
...data(),
currentPage: 'loyalty-enroll',
program: null,
form: {
first_name: '',
last_name: '',
email: '',
phone: '',
birthday: '',
marketing_email: false,
marketing_sms: false
},
enrolling: false,
enrolledCard: null,
loading: false,
error: null,
async init() {
loyaltyEnrollLog.info('=== LOYALTY ENROLL PAGE INITIALIZING ===');
if (window._loyaltyEnrollInitialized) return;
window._loyaltyEnrollInitialized = true;
// IMPORTANT: Call parent init first to set storeCode from URL
const parentInit = data().init;
if (parentInit) {
await parentInit.call(this);
}
await this.loadProgram();
loyaltyEnrollLog.info('=== LOYALTY ENROLL PAGE INITIALIZATION COMPLETE ===');
},
async loadProgram() {
this.loading = true;
try {
const response = await apiClient.get('/store/loyalty/program');
if (response) {
this.program = response;
loyaltyEnrollLog.info('Program loaded:', this.program.display_name);
}
} catch (error) {
if (error.status === 404) {
loyaltyEnrollLog.warn('No program configured');
} else {
this.error = error.message;
}
} finally {
this.loading = false;
}
},
async enrollCustomer() {
if (!this.form.first_name || !this.form.email) return;
this.enrolling = true;
try {
loyaltyEnrollLog.info('Enrolling customer:', this.form.email);
const response = await apiClient.post('/store/loyalty/cards/enroll', {
email: this.form.email,
customer_phone: this.form.phone || null,
customer_name: [this.form.first_name, this.form.last_name].filter(Boolean).join(' '),
customer_birthday: this.form.birthday || null,
marketing_email_consent: this.form.marketing_email,
marketing_sms_consent: this.form.marketing_sms
});
if (response) {
this.enrolledCard = response;
loyaltyEnrollLog.info('Customer enrolled successfully:', response.card_number);
}
} catch (error) {
Utils.showToast(`Enrollment failed: ${error.message}`, 'error');
loyaltyEnrollLog.error('Enrollment failed:', error);
} finally {
this.enrolling = false;
}
},
resetForm() {
this.form = {
first_name: '',
last_name: '',
email: '',
phone: '',
birthday: '',
marketing_email: false,
marketing_sms: false
};
}
};
}
if (!window.LogConfig.loggers.loyaltyEnroll) {
window.LogConfig.loggers.loyaltyEnroll = window.LogConfig.createLogger('loyaltyEnroll');
}
loyaltyEnrollLog.info('Loyalty enroll module loaded');