All files / lib/supabase/services profile.ts

63.63% Statements 63/99
61.22% Branches 30/49
83.33% Functions 5/6
63.63% Lines 63/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419                                                                                                                                          5x   5x         5x   4x 1x 1x           1x               3x           3x                           3x     3x           2x   2x           2x 2x   2x 2x                                                                                                         4x   4x     4x   4x 1x             3x                       3x 1x 1x     2x 2x                                 2x   2x     2x   2x 1x               1x     1x 1x         1x         1x     1x             1x                                 1x     1x               1x         1x 1x                             2x   2x     2x   2x 1x               1x             1x               1x               1x         1x 1x                             2x   2x 2x           2x         2x 2x                         16x      
/**
 * Supabase 用户 Profile 服务
 *
 * - 管理 `profiles` 表中的用户资料
 * - 处理头像上传到 Supabase Storage
 * - 与 Auth 用户关联
 */
 
import { decode } from 'base64-arraybuffer';
// 使用 legacy API,因为新 API (File/Directory) 在 SDK 54 中仍需迁移
import * as FileSystem from 'expo-file-system/legacy';
 
import { supabase } from '../client';
import { convertKeysToCamel, logger, wrapResponse, type SupabaseResponse } from '../helpers';
 
// ==================== 类型定义 ====================
 
/**
 * 用户 Profile(数据库 Schema)
 */
export interface ProfileDB {
  id: string; // 与 auth.users.id 关联
  username: string;
  avatar_url: string | null;
  bio: string | null;
  phone: string | null;
  is_admin: boolean;
  created_at: string;
  updated_at: string;
}
 
/**
 * 用户 Profile(前端使用,camelCase)
 */
export interface Profile {
  id: string;
  username: string;
  avatarUrl: string | null;
  bio: string | null;
  phone: string | null;
  isAdmin: boolean;
  createdAt: string;
  updatedAt: string;
}
 
/**
 * 完整用户信息(含宠物)
 */
export interface UserWithPets extends Profile {
  email?: string;
  pets?: any[];
}
 
/**
 * 更新 Profile 参数
 */
export interface UpdateProfileParams {
  username?: string;
  bio?: string;
  phone?: string;
}
 
// ==================== Profile 服务 ====================
 
class SupabaseProfileService {
  /**
   * 获取当前用户 Profile
   */
  async getCurrentProfile(): Promise<SupabaseResponse<UserWithPets>> {
    logger.query('profiles', 'getCurrentProfile');
 
    try {
      // 获取当前认证用户
      const {
        data: { user },
        error: authError,
      } = await supabase.auth.getUser();
 
      if (authError || !user) {
        logger.error('profiles', 'getCurrentProfile', authError || '未登录');
        const errorObj = authError || {
          message: '未登录',
          code: 'NOT_AUTHENTICATED',
          details: '',
          hint: '',
        };
        return {
          data: null,
          error: errorObj as any,
          success: false,
        };
      }
 
      // 查询 profile(暂时不关联 pets,避免关系错误)
      const { data: profile, error: profileError } = await supabase
        .from('profiles')
        .select('*')
        .eq('id', user.id)
        .single();
 
      Iif (profileError) {
        // 如果 profile 不存在,自动创建
        if (profileError.code === 'PGRST116') {
          const newProfile = await this.createProfile(user.id, {
            username: user.user_metadata?.username || user.email?.split('@')[0] || 'user',
          });
          return newProfile;
        }
 
        logger.error('profiles', 'getCurrentProfile', profileError);
        return wrapResponse<UserWithPets>(null, profileError);
      }
 
      // 转换为 camelCase 并添加 email
      const camelProfile = convertKeysToCamel(profile);
 
      // 单独查询用户的宠物列表(保持 snake_case,与 Pet schema 一致)
      const { data: petsData } = await supabase
        .from('pets')
        .select('*')
        .eq('user_id', user.id)
        .order('created_at', { ascending: false });
 
      const pets = petsData || [];
 
      const result = {
        ...(camelProfile as object),
        email: user.email,
        pets,
      } as unknown as UserWithPets;
 
      logger.success('profiles', 'getCurrentProfile');
      return { data: result, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'getCurrentProfile', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
 
  /**
   * 创建用户 Profile
   */
  async createProfile(
    userId: string,
    params: { username: string }
  ): Promise<SupabaseResponse<UserWithPets>> {
    logger.query('profiles', 'createProfile', { userId });
 
    try {
      const { data, error } = await supabase
        .from('profiles')
        .insert({
          id: userId,
          username: params.username,
        })
        .select()
        .single();
 
      if (error) {
        logger.error('profiles', 'createProfile', error);
        return wrapResponse<UserWithPets>(null, error);
      }
 
      const result = {
        ...(convertKeysToCamel(data) as object),
        pets: [],
      } as unknown as UserWithPets;
 
      logger.success('profiles', 'createProfile');
      return { data: result, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'createProfile', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
 
  /**
   * 更新用户 Profile
   */
  async updateProfile(params: UpdateProfileParams): Promise<SupabaseResponse<Profile>> {
    logger.query('profiles', 'updateProfile', params);
 
    try {
      const {
        data: { user },
      } = await supabase.auth.getUser();
 
      if (!user) {
        return {
          data: null,
          error: { message: '未登录', code: 'NOT_AUTHENTICATED', details: '', hint: '' } as any,
          success: false,
        };
      }
 
      const { data, error } = await supabase
        .from('profiles')
        .update({
          ...(params.username && { username: params.username }),
          ...(params.bio !== undefined && { bio: params.bio }),
          ...(params.phone !== undefined && { phone: params.phone }),
          updated_at: new Date().toISOString(),
        })
        .eq('id', user.id)
        .select()
        .single();
 
      if (error) {
        logger.error('profiles', 'updateProfile', error);
        return wrapResponse<Profile>(null, error);
      }
 
      logger.success('profiles', 'updateProfile');
      return { data: convertKeysToCamel(data) as Profile, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'updateProfile', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
 
  /**
   * 上传头像
   *
   * @param imageUri - 本地图片 URI(file://...)
   */
  async uploadAvatar(imageUri: string): Promise<SupabaseResponse<{ avatarUrl: string }>> {
    logger.query('profiles', 'uploadAvatar');
 
    try {
      const {
        data: { user },
      } = await supabase.auth.getUser();
 
      if (!user) {
        return {
          data: null,
          error: { message: '未登录', code: 'NOT_AUTHENTICATED', details: '', hint: '' } as any,
          success: false,
        };
      }
 
      // 准备文件路径
      const fileName = `${user.id}/avatar_${Date.now()}.jpg`;
 
      // 处理 file:// URI
      let fileUri = imageUri;
      Iif (!fileUri.startsWith('file://') && !fileUri.startsWith('content://')) {
        fileUri = `file://${fileUri}`;
      }
 
      // 使用 FileSystem 读取文件为 base64
      const base64Data = await FileSystem.readAsStringAsync(fileUri, {
        encoding: 'base64',
      });
 
      // 将 base64 转换为 ArrayBuffer
      const arrayBuffer = decode(base64Data);
 
      // 上传到 Storage
      const { error: uploadError } = await supabase.storage
        .from('avatars')
        .upload(fileName, arrayBuffer, {
          contentType: 'image/jpeg',
          upsert: true,
        });
 
      Iif (uploadError) {
        logger.error('profiles', 'uploadAvatar', uploadError);
        return {
          data: null,
          error: {
            message: uploadError.message,
            code: 'UPLOAD_ERROR',
            details: '',
            hint: '',
          } as any,
          success: false,
        };
      }
 
      // 获取公开 URL
      const {
        data: { publicUrl },
      } = supabase.storage.from('avatars').getPublicUrl(fileName);
 
      // 更新 profile
      const { error: updateError } = await supabase
        .from('profiles')
        .update({
          avatar_url: publicUrl,
          updated_at: new Date().toISOString(),
        })
        .eq('id', user.id);
 
      Iif (updateError) {
        logger.error('profiles', 'uploadAvatar (update profile)', updateError);
        return wrapResponse<{ avatarUrl: string }>(null, updateError);
      }
 
      logger.success('profiles', 'uploadAvatar');
      return { data: { avatarUrl: publicUrl }, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'uploadAvatar', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
 
  /**
   * 删除头像
   */
  async deleteAvatar(): Promise<SupabaseResponse<void>> {
    logger.query('profiles', 'deleteAvatar');
 
    try {
      const {
        data: { user },
      } = await supabase.auth.getUser();
 
      if (!user) {
        return {
          data: null,
          error: { message: '未登录', code: 'NOT_AUTHENTICATED', details: '', hint: '' } as any,
          success: false,
        };
      }
 
      // 获取当前头像 URL
      const { data: profile } = await supabase
        .from('profiles')
        .select('avatar_url')
        .eq('id', user.id)
        .single();
 
      // 如果有头像,从 Storage 删除
      Iif (profile?.avatar_url) {
        const path = profile.avatar_url.split('/avatars/')[1];
        if (path) {
          await supabase.storage.from('avatars').remove([path]);
        }
      }
 
      // 清空 profile 中的头像 URL
      const { error } = await supabase
        .from('profiles')
        .update({
          avatar_url: null,
          updated_at: new Date().toISOString(),
        })
        .eq('id', user.id);
 
      Iif (error) {
        logger.error('profiles', 'deleteAvatar', error);
        return wrapResponse<void>(null, error);
      }
 
      logger.success('profiles', 'deleteAvatar');
      return { data: undefined, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'deleteAvatar', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
 
  /**
   * 获取指定用户的 Profile(公开信息)
   */
  async getProfileById(userId: string): Promise<SupabaseResponse<Profile>> {
    logger.query('profiles', 'getProfileById', { userId });
 
    try {
      const { data, error } = await supabase
        .from('profiles')
        .select('id, username, avatar_url, bio, is_admin, created_at')
        .eq('id', userId)
        .single();
 
      Iif (error) {
        logger.error('profiles', 'getProfileById', error);
        return wrapResponse<Profile>(null, error);
      }
 
      logger.success('profiles', 'getProfileById');
      return { data: convertKeysToCamel(data) as Profile, error: null, success: true };
    } catch (err) {
      logger.error('profiles', 'getProfileById', err);
      return {
        data: null,
        error: { message: String(err), code: 'UNKNOWN', details: '', hint: '' } as any,
        success: false,
      };
    }
  }
}
 
// 导出单例
export const supabaseProfileService = new SupabaseProfileService();
 
export default supabaseProfileService;