feat: add platform assignment to user and vendor creation forms

User create page:
- When role=admin, show super admin toggle
- If not super admin, show platform multi-select
- Admin users created via /api/v1/admin/admin-users endpoint
- Vendor users created via existing /admin/users endpoint

Vendor create page:
- Add platform selection section
- Vendors can be assigned to multiple platforms on creation
- Update VendorCreate schema to accept platform_ids
- Update AdminService.create_vendor() to create VendorPlatform records

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-24 19:49:40 +01:00
parent 300f49c5a1
commit 3cbe7e2979
6 changed files with 241 additions and 13 deletions

View File

@@ -17,8 +17,11 @@ function adminUserCreate() {
password: '',
first_name: '',
last_name: '',
role: 'vendor'
role: 'vendor',
is_super_admin: false,
platform_ids: []
},
platforms: [],
errors: {},
saving: false,
@@ -33,30 +36,114 @@ function adminUserCreate() {
}
window._userCreateInitialized = true;
// Load platforms for admin assignment
await this.loadPlatforms();
userCreateLog.info('=== USER CREATE PAGE INITIALIZATION COMPLETE ===');
},
// Load available platforms
async loadPlatforms() {
try {
userCreateLog.debug('Loading platforms...');
const response = await apiClient.get('/admin/platforms');
this.platforms = response.platforms || response.items || [];
userCreateLog.debug(`Loaded ${this.platforms.length} platforms`);
} catch (error) {
userCreateLog.error('Failed to load platforms:', error);
this.platforms = [];
}
},
// Handle role change
onRoleChange() {
userCreateLog.debug('Role changed to:', this.formData.role);
if (this.formData.role !== 'admin') {
// Reset admin-specific fields when switching away from admin
this.formData.is_super_admin = false;
this.formData.platform_ids = [];
}
},
// Validate form
validateForm() {
this.errors = {};
if (!this.formData.username.trim()) {
this.errors.username = 'Username is required';
}
if (!this.formData.email.trim()) {
this.errors.email = 'Email is required';
}
if (!this.formData.password || this.formData.password.length < 6) {
this.errors.password = 'Password must be at least 6 characters';
}
// Admin-specific validation
if (this.formData.role === 'admin' && !this.formData.is_super_admin) {
if (!this.formData.platform_ids || this.formData.platform_ids.length === 0) {
this.errors.platform_ids = 'Platform admins must be assigned to at least one platform';
}
}
return Object.keys(this.errors).length === 0;
},
// Submit form
async handleSubmit() {
userCreateLog.info('=== CREATING USER ===');
userCreateLog.debug('Form data:', { ...this.formData, password: '[REDACTED]' });
this.errors = {};
if (!this.validateForm()) {
userCreateLog.warn('Validation failed:', this.errors);
Utils.showToast('Please fix the errors before submitting', 'error');
return;
}
this.saving = true;
try {
const url = `/admin/users`;
window.LogConfig.logApiCall('POST', url, { ...this.formData, password: '[REDACTED]' }, 'request');
let url, payload, response;
if (this.formData.role === 'admin') {
// Use admin-users endpoint for creating admin users
url = '/api/v1/admin/admin-users';
payload = {
email: this.formData.email,
username: this.formData.username,
password: this.formData.password,
first_name: this.formData.first_name || null,
last_name: this.formData.last_name || null,
is_super_admin: this.formData.is_super_admin,
platform_ids: this.formData.is_super_admin ? [] : this.formData.platform_ids.map(id => parseInt(id))
};
} else {
// Use regular users endpoint for vendor users
url = '/admin/users';
payload = {
email: this.formData.email,
username: this.formData.username,
password: this.formData.password,
first_name: this.formData.first_name || null,
last_name: this.formData.last_name || null,
role: this.formData.role
};
}
window.LogConfig.logApiCall('POST', url, { ...payload, password: '[REDACTED]' }, 'request');
const startTime = performance.now();
const response = await apiClient.post(url, this.formData);
response = await apiClient.post(url, payload);
const duration = performance.now() - startTime;
window.LogConfig.logApiCall('POST', url, response, 'response');
window.LogConfig.logPerformance('Create User', duration);
Utils.showToast('User created successfully', 'success');
userCreateLog.info(`User created successfully in ${duration}ms`, response);
const userType = this.formData.role === 'admin'
? (this.formData.is_super_admin ? 'Super admin' : 'Platform admin')
: 'User';
Utils.showToast(`${userType} created successfully`, 'success');
userCreateLog.info(`${userType} created successfully in ${duration}ms`, response);
// Redirect to the new user's detail page
setTimeout(() => {
@@ -79,9 +166,9 @@ function adminUserCreate() {
// Handle specific errors
if (error.message) {
if (error.message.includes('Email already registered')) {
if (error.message.includes('Email already')) {
this.errors.email = 'This email is already registered';
} else if (error.message.includes('Username already taken')) {
} else if (error.message.includes('Username already')) {
this.errors.username = 'This username is already taken';
}
}

View File

@@ -23,6 +23,9 @@ function adminVendorCreate() {
companies: [],
loadingCompanies: true,
// Platforms list for selection
platforms: [],
// Form data matching VendorCreate schema
formData: {
company_id: '',
@@ -32,7 +35,8 @@ function adminVendorCreate() {
description: '',
letzshop_csv_url_fr: '',
letzshop_csv_url_en: '',
letzshop_csv_url_de: ''
letzshop_csv_url_de: '',
platform_ids: []
},
// UI state
@@ -49,7 +53,10 @@ function adminVendorCreate() {
try {
vendorCreateLog.info('Initializing vendor create page');
await this.loadCompanies();
await Promise.all([
this.loadCompanies(),
this.loadPlatforms()
]);
} catch (error) {
vendorCreateLog.error('Failed to initialize vendor create:', error);
}
@@ -70,6 +77,18 @@ function adminVendorCreate() {
}
},
// Load platforms for selection
async loadPlatforms() {
try {
const response = await apiClient.get('/admin/platforms');
this.platforms = response.platforms || response.items || [];
vendorCreateLog.debug('Loaded platforms:', this.platforms.length);
} catch (error) {
vendorCreateLog.error('Failed to load platforms:', error);
this.platforms = [];
}
},
// Auto-generate subdomain from vendor name
autoGenerateSubdomain() {
if (!this.formData.name) {
@@ -124,6 +143,11 @@ function adminVendorCreate() {
payload.letzshop_csv_url_de = this.formData.letzshop_csv_url_de;
}
// Add platform assignments
if (this.formData.platform_ids && this.formData.platform_ids.length > 0) {
payload.platform_ids = this.formData.platform_ids.map(id => parseInt(id));
}
const response = await apiClient.post('/admin/vendors', payload);
vendorCreateLog.info('Vendor created successfully:', response.vendor_code);
@@ -147,7 +171,8 @@ function adminVendorCreate() {
description: '',
letzshop_csv_url_fr: '',
letzshop_csv_url_en: '',
letzshop_csv_url_de: ''
letzshop_csv_url_de: '',
platform_ids: []
};
// Scroll to top to show success message