Files
orion/app/modules/customers/static/admin/js/customers.js
Samir Boulahtit d7a0ff8818 refactor: complete module-driven architecture migration
This commit completes the migration to a fully module-driven architecture:

## Models Migration
- Moved all domain models from models/database/ to their respective modules:
  - tenancy: User, Admin, Vendor, Company, Platform, VendorDomain, etc.
  - cms: MediaFile, VendorTheme
  - messaging: Email, VendorEmailSettings, VendorEmailTemplate
  - core: AdminMenuConfig
- models/database/ now only contains Base and TimestampMixin (infrastructure)

## Schemas Migration
- Moved all domain schemas from models/schema/ to their respective modules:
  - tenancy: company, vendor, admin, team, vendor_domain
  - cms: media, image, vendor_theme
  - messaging: email
- models/schema/ now only contains base.py and auth.py (infrastructure)

## Routes Migration
- Moved admin routes from app/api/v1/admin/ to modules:
  - menu_config.py -> core module
  - modules.py -> tenancy module
  - module_config.py -> tenancy module
- app/api/v1/admin/ now only aggregates auto-discovered module routes

## Menu System
- Implemented module-driven menu system with MenuDiscoveryService
- Extended FrontendType enum: PLATFORM, ADMIN, VENDOR, STOREFRONT
- Added MenuItemDefinition and MenuSectionDefinition dataclasses
- Each module now defines its own menu items in definition.py
- MenuService integrates with MenuDiscoveryService for template rendering

## Documentation
- Updated docs/architecture/models-structure.md
- Updated docs/architecture/menu-management.md
- Updated architecture validation rules for new exceptions

## Architecture Validation
- Updated MOD-019 rule to allow base.py in models/schema/
- Created core module exceptions.py and schemas/ directory
- All validation errors resolved (only warnings remain)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:02:56 +01:00

408 lines
13 KiB
JavaScript

// noqa: js-006 - async init pattern is safe, loadData has try/catch
// static/admin/js/customers.js
/**
* Admin customer management page logic
*/
// Create logger for this module
const customersLog = window.LogConfig?.createLogger('CUSTOMERS') || console;
function adminCustomers() {
return {
// Inherit base layout state
...data(),
// Page identifier
currentPage: 'customers',
// Loading states
loading: true,
loadingCustomers: false,
// Error state
error: '',
// Data
customers: [],
stats: {
total: 0,
active: 0,
inactive: 0,
with_orders: 0,
total_spent: 0,
total_orders: 0,
avg_order_value: 0
},
// Pagination (standard structure matching pagination macro)
pagination: {
page: 1,
per_page: 20,
total: 0,
pages: 0
},
// Filters
filters: {
search: '',
is_active: '',
vendor_id: ''
},
// Selected vendor (for prominent display and filtering)
selectedVendor: null,
// Tom Select instance
vendorSelectInstance: null,
// Computed: total pages
get totalPages() {
return this.pagination.pages;
},
// Computed: Start index for pagination display
get startIndex() {
if (this.pagination.total === 0) return 0;
return (this.pagination.page - 1) * this.pagination.per_page + 1;
},
// Computed: End index for pagination display
get endIndex() {
const end = this.pagination.page * this.pagination.per_page;
return end > this.pagination.total ? this.pagination.total : end;
},
// Computed: Page numbers for pagination
get pageNumbers() {
const pages = [];
const totalPages = this.totalPages;
const current = this.pagination.page;
if (totalPages <= 7) {
for (let i = 1; i <= totalPages; i++) {
pages.push(i);
}
} else {
pages.push(1);
if (current > 3) {
pages.push('...');
}
const start = Math.max(2, current - 1);
const end = Math.min(totalPages - 1, current + 1);
for (let i = start; i <= end; i++) {
pages.push(i);
}
if (current < totalPages - 2) {
pages.push('...');
}
pages.push(totalPages);
}
return pages;
},
async init() {
// Load i18n translations
await I18n.loadModule('customers');
customersLog.debug('Customers page initialized');
// Load platform settings for rows per page
if (window.PlatformSettings) {
this.pagination.per_page = await window.PlatformSettings.getRowsPerPage();
}
// Initialize Tom Select for vendor filter
this.initVendorSelect();
// Check localStorage for saved vendor
const savedVendorId = localStorage.getItem('customers_selected_vendor_id');
if (savedVendorId) {
customersLog.debug('Restoring saved vendor:', savedVendorId);
// Restore vendor after a short delay to ensure TomSelect is ready
setTimeout(async () => {
await this.restoreSavedVendor(parseInt(savedVendorId));
}, 200);
// Load stats but not customers (restoreSavedVendor will do that)
await this.loadStats();
} else {
// No saved vendor - load all data
await Promise.all([
this.loadCustomers(),
this.loadStats()
]);
}
this.loading = false;
},
/**
* Restore saved vendor from localStorage
*/
async restoreSavedVendor(vendorId) {
try {
const vendor = await apiClient.get(`/admin/vendors/${vendorId}`);
if (this.vendorSelectInstance && vendor) {
// Add the vendor as an option and select it
this.vendorSelectInstance.addOption({
id: vendor.id,
name: vendor.name,
vendor_code: vendor.vendor_code
});
this.vendorSelectInstance.setValue(vendor.id, true);
// Set the filter state
this.selectedVendor = vendor;
this.filters.vendor_id = vendor.id;
customersLog.debug('Restored vendor:', vendor.name);
// Load customers with the vendor filter applied
await this.loadCustomers();
}
} catch (error) {
customersLog.error('Failed to restore saved vendor, clearing localStorage:', error);
localStorage.removeItem('customers_selected_vendor_id');
// Load unfiltered customers as fallback
await this.loadCustomers();
}
},
/**
* Initialize Tom Select for vendor autocomplete
*/
initVendorSelect() {
const selectEl = this.$refs.vendorSelect;
if (!selectEl) {
customersLog.warn('Vendor select element not found');
return;
}
// Wait for Tom Select to be available
if (typeof TomSelect === 'undefined') {
customersLog.warn('TomSelect not loaded, retrying in 100ms');
setTimeout(() => this.initVendorSelect(), 100);
return;
}
this.vendorSelectInstance = new TomSelect(selectEl, {
valueField: 'id',
labelField: 'name',
searchField: ['name', 'vendor_code'],
placeholder: 'Filter by vendor...',
allowEmptyOption: true,
load: async (query, callback) => {
try {
const response = await apiClient.get('/admin/vendors', {
search: query,
limit: 50
});
callback(response.vendors || []);
} catch (error) {
customersLog.error('Failed to search vendors:', error);
callback([]);
}
},
render: {
option: (data, escape) => {
return `<div class="flex items-center justify-between py-1">
<span>${escape(data.name)}</span>
<span class="text-xs text-gray-400 font-mono">${escape(data.vendor_code || '')}</span>
</div>`;
},
item: (data, escape) => {
return `<div>${escape(data.name)}</div>`;
}
},
onChange: (value) => {
if (value) {
const vendor = this.vendorSelectInstance.options[value];
this.selectedVendor = vendor;
this.filters.vendor_id = value;
// Save to localStorage
localStorage.setItem('customers_selected_vendor_id', value.toString());
} else {
this.selectedVendor = null;
this.filters.vendor_id = '';
// Clear from localStorage
localStorage.removeItem('customers_selected_vendor_id');
}
this.pagination.page = 1;
this.loadCustomers();
this.loadStats();
}
});
customersLog.debug('Vendor select initialized');
},
/**
* Clear vendor filter
*/
clearVendorFilter() {
if (this.vendorSelectInstance) {
this.vendorSelectInstance.clear();
}
this.selectedVendor = null;
this.filters.vendor_id = '';
// Clear from localStorage
localStorage.removeItem('customers_selected_vendor_id');
this.pagination.page = 1;
this.loadCustomers();
this.loadStats();
},
/**
* Load customers with current filters
*/
async loadCustomers() {
this.loadingCustomers = true;
this.error = '';
try {
const params = new URLSearchParams({
skip: ((this.pagination.page - 1) * this.pagination.per_page).toString(),
limit: this.pagination.per_page.toString()
});
if (this.filters.search) {
params.append('search', this.filters.search);
}
if (this.filters.is_active !== '') {
params.append('is_active', this.filters.is_active);
}
if (this.filters.vendor_id) {
params.append('vendor_id', this.filters.vendor_id);
}
const response = await apiClient.get(`/admin/customers?${params}`);
this.customers = response.customers || [];
this.pagination.total = response.total || 0;
this.pagination.pages = Math.ceil(this.pagination.total / this.pagination.per_page);
} catch (error) {
customersLog.error('Failed to load customers:', error);
this.error = error.message || 'Failed to load customers';
this.customers = [];
} finally {
this.loadingCustomers = false;
}
},
/**
* Load customer statistics
*/
async loadStats() {
try {
const params = new URLSearchParams();
if (this.filters.vendor_id) {
params.append('vendor_id', this.filters.vendor_id);
}
const response = await apiClient.get(`/admin/customers/stats?${params}`);
this.stats = response;
} catch (error) {
customersLog.error('Failed to load stats:', error);
}
},
/**
* Reset pagination and reload
*/
async resetAndLoad() {
this.pagination.page = 1;
await Promise.all([
this.loadCustomers(),
this.loadStats()
]);
},
/**
* Go to previous page
*/
previousPage() {
if (this.pagination.page > 1) {
this.pagination.page--;
this.loadCustomers();
}
},
/**
* Go to next page
*/
nextPage() {
if (this.pagination.page < this.totalPages) {
this.pagination.page++;
this.loadCustomers();
}
},
/**
* Go to specific page
*/
goToPage(pageNum) {
if (typeof pageNum === 'number' && pageNum !== this.pagination.page) {
this.pagination.page = pageNum;
this.loadCustomers();
}
},
/**
* Toggle customer active status
*/
async toggleStatus(customer) {
const action = customer.is_active ? 'deactivate' : 'activate';
if (!confirm(`Are you sure you want to ${action} this customer?`)) {
return;
}
try {
const response = await apiClient.patch(`/admin/customers/${customer.id}/toggle-status`);
customer.is_active = response.is_active;
// Update stats
if (response.is_active) {
this.stats.active++;
this.stats.inactive--;
} else {
this.stats.active--;
this.stats.inactive++;
}
customersLog.info(response.message);
} catch (error) {
customersLog.error('Failed to toggle status:', error);
Utils.showToast(error.message || I18n.t('customers.messages.failed_to_toggle_customer_status'), 'error');
}
},
/**
* Format currency for display
*/
formatCurrency(amount) {
if (amount == null) return '-';
return new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(amount);
},
/**
* Format date for display
*/
formatDate(dateString) {
if (!dateString) return '-';
try {
const date = new Date(dateString);
return date.toLocaleDateString('en-GB', {
year: 'numeric',
month: 'short',
day: 'numeric'
});
} catch {
return dateString;
}
}
};
}