feat: Integrate Data Connect and Implement Staff List View Directory

This commit is contained in:
dhinesh-m24
2026-01-31 16:54:59 +05:30
parent 48bb1c457c
commit cb25b33d04
255 changed files with 21425 additions and 109 deletions

View File

@@ -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;
}
};