Files
Samir Boulahtit f95db7c0b1
Some checks failed
CI / ruff (push) Successful in 9s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has started running
feat(roles): add admin store roles page, permission i18n, and menu integration
- Add admin store roles page with merchant→store cascading for superadmin
  and store-only selection for platform admin
- Add permission catalog API with translated labels/descriptions (en/fr/de/lb)
- Add permission translations to all 15 module locale files (60 files total)
- Add info icon tooltips for permission descriptions in role editor
- Add store roles menu item and admin menu item in module definition
- Fix store-selector.js URL construction bug when apiEndpoint has query params
- Add admin store roles API (CRUD + platform scoping)
- Add integration tests for admin store roles and permission catalog

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 23:31:27 +01:00

156 lines
5.1 KiB
JavaScript

/**
* Store Roles Management — Alpine.js component
*
* Provides CRUD for custom roles with a permission matrix UI.
* Loaded on /store/{store_code}/team/roles.
*/
const storeRolesLog = (window.LogConfig && window.LogConfig.createLogger)
? window.LogConfig.createLogger('storeRoles', false)
: console;
function storeRoles() {
storeRolesLog.info('storeRoles() called');
return {
// Inherit base layout state
...data(),
// Set page identifier
currentPage: 'team',
// Component state
roles: [],
loading: true,
error: false,
saving: false,
showRoleModal: false,
editingRole: null,
roleForm: { name: '', permissions: [] },
permissionCategories: [],
presetRoles: ['manager', 'staff', 'support', 'viewer', 'marketing'],
async init() {
// Guard against multiple initialization
if (window._storeRolesInitialized) {
storeRolesLog.warn('Already initialized, skipping');
return;
}
window._storeRolesInitialized = true;
// Call parent init to set storeCode from URL
const parentInit = data().init;
if (parentInit) {
await parentInit.call(this);
}
await this.loadPermissions();
await this.loadRoles();
},
async loadPermissions() {
try {
const response = await apiClient.get('/store/team/permissions/catalog');
this.permissionCategories = response.categories || [];
storeRolesLog.info('Loaded permission catalog:', this.permissionCategories.length, 'categories');
} catch (e) {
storeRolesLog.warn('Could not load permission catalog:', e);
}
},
async loadRoles() {
this.loading = true;
this.error = false;
try {
const response = await apiClient.get('/store/team/roles');
this.roles = response.roles || [];
} catch (e) {
this.error = true;
storeRolesLog.error('Error loading roles:', e);
} finally {
this.loading = false;
}
},
isPresetRole(name) {
return this.presetRoles.includes(name.toLowerCase());
},
openCreateModal() {
this.editingRole = null;
this.roleForm = { name: '', permissions: [] };
this.showRoleModal = true;
},
openEditModal(role) {
this.editingRole = role;
this.roleForm = {
name: role.name,
permissions: [...(role.permissions || [])],
};
this.showRoleModal = true;
},
togglePermission(permId) {
const idx = this.roleForm.permissions.indexOf(permId);
if (idx >= 0) {
this.roleForm.permissions.splice(idx, 1);
} else {
this.roleForm.permissions.push(permId);
}
},
toggleCategory(category) {
const perms = category.permissions || [];
const permIds = perms.map(p => p.id);
const allSelected = permIds.every(id => this.roleForm.permissions.includes(id));
if (allSelected) {
this.roleForm.permissions = this.roleForm.permissions.filter(id => !permIds.includes(id));
} else {
for (const id of permIds) {
if (!this.roleForm.permissions.includes(id)) {
this.roleForm.permissions.push(id);
}
}
}
},
isCategoryFullySelected(category) {
const perms = category.permissions || [];
return perms.length > 0 && perms.every(p => this.roleForm.permissions.includes(p.id));
},
async saveRole() {
this.saving = true;
try {
if (this.editingRole) {
await apiClient.put(`/store/team/roles/${this.editingRole.id}`, this.roleForm);
} else {
await apiClient.post('/store/team/roles', this.roleForm);
}
this.showRoleModal = false;
Utils.showToast('Role saved successfully', 'success');
await this.loadRoles();
} catch (e) {
storeRolesLog.error('Error saving role:', e);
Utils.showToast(e.message || 'Failed to save role', 'error');
} finally {
this.saving = false;
}
},
async confirmDelete(role) {
if (!confirm(`Delete role "${role.name}"? This cannot be undone.`)) return;
try {
await apiClient.delete(`/store/team/roles/${role.id}`);
Utils.showToast('Role deleted successfully', 'success');
await this.loadRoles();
} catch (e) {
storeRolesLog.error('Error deleting role:', e);
Utils.showToast(e.message || 'Failed to delete role', 'error');
}
},
};
}