feat: Integrate Data Connect and Implement Staff List View Directory
This commit is contained in:
@@ -1,23 +1,46 @@
|
||||
import { getFirestore, doc, getDoc } from "firebase/firestore";
|
||||
import { app } from "../features/auth/firebase";
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore - generated dataconnect types may not be resolvable in this context
|
||||
import { getUserById } from "@/dataconnect-generated";
|
||||
|
||||
export interface UserData {
|
||||
id: string;
|
||||
email: string;
|
||||
fullName?: string;
|
||||
userRole: "admin" | "client" | "vendor";
|
||||
// role may come back uppercase or lowercase from the backend; treat as optional
|
||||
userRole?: "admin" | "client" | "vendor" | "ADMIN" | "CLIENT" | "VENDOR";
|
||||
photoURL?: string;
|
||||
}
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
/**
|
||||
* Fetch user data from Firestore including their role
|
||||
* Fetch user data from DataConnect (fallback to Firestore if needed)
|
||||
* @param uid - Firebase User UID
|
||||
* @returns UserData object with role information
|
||||
*/
|
||||
export const fetchUserData = async (uid: string): Promise<UserData | null> => {
|
||||
try {
|
||||
// Prefer backend dataconnect query for authoritative user role
|
||||
const { data } = await getUserById({ id: uid });
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const dataAny = data as any;
|
||||
|
||||
if (dataAny && dataAny.user) {
|
||||
const user = dataAny.user;
|
||||
|
||||
return {
|
||||
id: uid,
|
||||
email: user.email || "",
|
||||
fullName: user.fullName,
|
||||
userRole: user.userRole,
|
||||
photoURL: user.photoUrl || user.photoURL,
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback: attempt Firestore lookup if dataconnect didn't return a user
|
||||
const userDocRef = doc(db, "users", uid);
|
||||
const userDocSnap = await getDoc(userDocRef);
|
||||
|
||||
@@ -27,14 +50,14 @@ export const fetchUserData = async (uid: string): Promise<UserData | null> => {
|
||||
id: uid,
|
||||
email: data.email || "",
|
||||
fullName: data.fullName,
|
||||
userRole: data.userRole || "client",
|
||||
userRole: data.userRole, // no frontend defaulting
|
||||
photoURL: data.photoURL,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Error fetching user data from Firestore:", error);
|
||||
console.error("Error fetching user data from DataConnect/Firestore:", error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user