phase 1-6 implemented

This commit is contained in:
dimitar committed 2026-03-10 04:14:03 +01:00
1 parent 22c274bb83
commit 2c4927478a
73 files changed
+9702 -2354

No files matched your search

+16 -12
View File
@@ -1,9 +1,10 @@
import { clerkClient } from '@clerk/nextjs/server';
import { clerkClient } from "@clerk/nextjs/server";
import log from "./logger";
/**
* User roles available in the application
*/
export type UserRole = 'admin' | 'trainer' | 'client';
export type UserRole = "admin" | "trainer" | "client";
/**
* Set a user's role in Clerk public metadata
@@ -52,7 +53,10 @@ export async function getUserRole(userId: string): Promise<UserRole | null> {
* @example
* const isAdmin = await hasRole('user_abc123', 'admin');
*/
export async function hasRole(userId: string, role: UserRole): Promise<boolean> {
export async function hasRole(
userId: string,
role: UserRole,
): Promise<boolean> {
const userRole = await getUserRole(userId);
return userRole === role;
}
@@ -67,7 +71,7 @@ export async function hasRole(userId: string, role: UserRole): Promise<boolean>
* const isAdmin = await isAdmin('user_abc123');
*/
export async function isAdmin(userId: string): Promise<boolean> {
return hasRole(userId, 'admin');
return hasRole(userId, "admin");
}
/**
@@ -80,7 +84,7 @@ export async function isAdmin(userId: string): Promise<boolean> {
* const isTrainer = await isTrainer('user_abc123');
*/
export async function isTrainer(userId: string): Promise<boolean> {
return hasRole(userId, 'trainer');
return hasRole(userId, "trainer");
}
/**
@@ -93,7 +97,7 @@ export async function isTrainer(userId: string): Promise<boolean> {
* const isClient = await isClient('user_abc123');
*/
export async function isClient(userId: string): Promise<boolean> {
return hasRole(userId, 'client');
return hasRole(userId, "client");
}
/**
@@ -109,7 +113,7 @@ export async function isClient(userId: string): Promise<boolean> {
* ]);
*/
export async function bulkSetUserRoles(
userRoles: Array<{ userId: string; role: UserRole }>
userRoles: Array<{ userId: string; role: UserRole }>,
): Promise<void> {
const client = await clerkClient();
@@ -117,8 +121,8 @@ export async function bulkSetUserRoles(
userRoles.map(({ userId, role }) =>
client.users.updateUser(userId, {
publicMetadata: { role },
})
)
}),
),
);
}
@@ -139,7 +143,7 @@ export async function getUsersByRole(role: UserRole) {
const { data: users } = await client.users.getUserList();
return users.filter(
(user) => (user.publicMetadata?.role as UserRole) === role
(user) => (user.publicMetadata?.role as UserRole) === role,
);
}
@@ -163,7 +167,7 @@ export async function getUserCountByRole(): Promise<Record<UserRole, number>> {
};
users.forEach((user) => {
const role = (user.publicMetadata?.role as UserRole) || 'client';
const role = (user.publicMetadata?.role as UserRole) || "client";
counts[role]++;
});
@@ -192,7 +196,7 @@ export async function syncUserRole(userId: string): Promise<boolean> {
return true;
} catch (error) {
console.error('Error syncing user role:', error);
log.error("Failed to sync user role", error, { userId });
return false;
}
}