Implement Role Management Features and UI Enhancements

- Introduced a new composable `useActiveRole` for managing user roles, including fetching role status and switching roles with server validation.
- Updated `RoleSwitcher.vue` to utilize the new composable, enhancing role selection with improved error handling and UI feedback.
- Added new API endpoints for role management, including fetching user role status and switching active roles.
- Enhanced product visibility logic to filter based on the user's active role, ensuring a tailored experience.
- Updated database schema to support last active role tracking for users, improving session management.
- Refined UI components across the application to reflect role-based changes and improve user experience.
This commit is contained in:
Bastian Masanek
2025-11-05 01:04:26 +01:00
parent 0e450684c6
commit f9125e744b
16 changed files with 1573 additions and 88 deletions

View File

@@ -120,6 +120,48 @@ export async function getVisibleProductIdsForUser(userId: string): Promise<strin
return [...new Set(visibleProducts.map((pv) => pv.productId))]
}
/**
* Get products visible for a SPECIFIC role (used with RoleSwitcher)
* Unlike getVisibleProductIdsForUser(), this only returns products for ONE role
*
* Usage:
* ```typescript
* const activeRole = await getUserActiveRole(event)
* const visibleProductIds = await getVisibleProductIdsForRole(userId, activeRole)
* ```
*
* @param userId - The user's UUID
* @param roleCode - Specific role to filter by ('private', 'educator', 'company')
* @returns Array of product UUIDs visible for this role
*/
export async function getVisibleProductIdsForRole(
userId: string,
roleCode: string
): Promise<string[]> {
const db = useDatabase()
// Verify user has this role (approved)
const hasRole = await db.query.userRoles.findFirst({
where: and(
eq(userRoles.userId, userId),
eq(userRoles.roleCode, roleCode),
eq(userRoles.status, 'approved')
),
})
// User doesn't have this role → return empty array
if (!hasRole) {
return []
}
// Get all products assigned to this specific role
const visibleProducts = await db.query.productRoleVisibility.findMany({
where: eq(productRoleVisibility.roleCode, roleCode),
})
return visibleProducts.map((pv) => pv.productId)
}
/**
* Assign a role to a user (MVP: manual assignment, always approved)
* Phase 2/3: This will be replaced by role request workflow