phase 1-6 implemented
This commit is contained in:
1 parent
22c274bb83
commit
2c4927478a
73 files changed
+9702
-2354
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { currentUser } from "@clerk/nextjs/server";
|
||||
import type { IDatabase, User } from "./database/types";
|
||||
import log from "./logger";
|
||||
|
||||
export async function ensureUserSynced(
|
||||
userId: string,
|
||||
@@ -8,7 +9,7 @@ export async function ensureUserSynced(
|
||||
const existingUser = await db.getUserById(userId);
|
||||
if (existingUser) return existingUser;
|
||||
|
||||
console.log("User not found in DB by ID, checking Clerk:", userId);
|
||||
log.debug("User not found in DB by ID, checking Clerk", { userId });
|
||||
const clerkUser = await currentUser();
|
||||
|
||||
if (!clerkUser || clerkUser.id !== userId) {
|
||||
@@ -24,7 +25,7 @@ export async function ensureUserSynced(
|
||||
// Check if user exists by email (e.g. seeded user)
|
||||
const existingByEmail = await db.getUserByEmail(email);
|
||||
if (existingByEmail) {
|
||||
console.log("User found by email but ID mismatch. Migrating ID...", {
|
||||
log.debug("User found by email but ID mismatch, migrating ID", {
|
||||
oldId: existingByEmail.id,
|
||||
newId: userId,
|
||||
});
|
||||
@@ -50,7 +51,7 @@ export async function ensureUserSynced(
|
||||
return db.getUserById(userId);
|
||||
}
|
||||
|
||||
console.log("Creating new user from Clerk data:", userId);
|
||||
log.debug("Creating new user from Clerk data", { userId, email });
|
||||
const user = await db.createUser({
|
||||
id: userId,
|
||||
email,
|
||||
@@ -59,11 +60,11 @@ export async function ensureUserSynced(
|
||||
password: "", // Managed by Clerk
|
||||
phone: clerkUser.phoneNumbers[0]?.phoneNumber || undefined,
|
||||
gymId: (clerkUser.publicMetadata.gymId as string | undefined) || undefined,
|
||||
role: ((): any => {
|
||||
role: (() => {
|
||||
const r = clerkUser.publicMetadata.role as string | undefined;
|
||||
return r &&
|
||||
["superAdmin", "admin", "trainer", "client", "generalUser"].includes(r)
|
||||
? r
|
||||
const validRoles = ["superAdmin", "admin", "trainer", "client"] as const;
|
||||
return r && validRoles.includes(r as (typeof validRoles)[number])
|
||||
? (r as (typeof validRoles)[number])
|
||||
: "client";
|
||||
})(),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user