fx-1 / src /services /profileService.ts
felix-fx's picture
Initial commit from GitHub fx-1
6711fd5 verified
Raw History Blame Contribute Delete
2.49 kB
import { supabase } from "@/integrations/supabase/client";
const FUNCTION_NAME = "profile-manager";
interface ProfileError extends Error {
code?: string;
status?: number;
}
async function callProfileManager(payload: Record<string, any>) {
try {
const { data, error } = await supabase.functions.invoke(FUNCTION_NAME, {
body: payload,
});
if (error) {
console.error("Profile Manager Error:", error);
const profileError: ProfileError = new Error(error.message || "فشل الاتصال بالخادم");
profileError.code = error.code;
profileError.status = error.status;
throw profileError;
}
if (data?.error) {
const profileError: ProfileError = new Error(data.error);
profileError.code = data.code;
throw profileError;
}
return data;
} catch (err: any) {
console.error("Profile service error:", err);
throw err;
}
}
export const profileService = {
async register(profile: {
id: string;
user_id: string;
display_name: string;
username: string;
is_verified: boolean;
bio: string;
password_hash: string;
avatar_url: string | null;
}) {
try {
return await callProfileManager({ action: "register", ...profile });
} catch (err: any) {
if (err.code === "USERNAME_EXISTS") {
throw new Error("هذا المعرف مستخدم بالفعل");
}
throw err;
}
},
async login(username: string) {
try {
return await callProfileManager({ action: "login", username });
} catch (err: any) {
if (err.code === "USER_NOT_FOUND") {
throw new Error("المعرف غير موجود في النظام");
}
if (err.code === "DB_LOOKUP_ERROR") {
throw new Error("خطأ في الاتصال بقاعدة البيانات");
}
throw err;
}
},
async update(username: string, updates: Record<string, any>) {
try {
return await callProfileManager({ action: "update", username, updates });
} catch (err: any) {
if (err.code === "USER_NOT_FOUND") {
throw new Error("الحساب غير موجود");
}
throw err;
}
},
async checkUsername(username: string): Promise<boolean> {
try {
const result = await callProfileManager({ action: "check_username", username });
return result.exists;
} catch (err: any) {
console.error("Username check failed:", err);
return false;
}
},
};