This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / modules / cards / application / useCases / queries / GetLibrariesForCardUseCase.ts
2.4 kB 85 lines
1import { UseCase } from 'src/shared/core/UseCase'; 2import { ICardQueryRepository } from '../../../domain/ICardQueryRepository'; 3import { IProfileService } from '../../../domain/services/IProfileService'; 4import { err, ok, Result } from 'src/shared/core/Result'; 5import { UserProfileDTO } from '@semble/types'; 6import { ProfileEnricher } from '../../services/ProfileEnricher'; 7 8export interface GetLibrariesForCardQuery { 9 cardId: string; 10} 11 12export interface GetLibrariesForCardResult { 13 cardId: string; 14 users: UserProfileDTO[]; 15 totalCount: number; 16} 17 18export class ValidationError extends Error { 19 constructor(message: string) { 20 super(message); 21 this.name = 'ValidationError'; 22 } 23} 24 25export class GetLibrariesForCardUseCase 26 implements 27 UseCase<GetLibrariesForCardQuery, Result<GetLibrariesForCardResult>> 28{ 29 constructor( 30 private cardQueryRepo: ICardQueryRepository, 31 private profileService: IProfileService, 32 ) {} 33 34 async execute( 35 query: GetLibrariesForCardQuery, 36 ): Promise<Result<GetLibrariesForCardResult>> { 37 // Validate card ID 38 if (!query.cardId || query.cardId.trim().length === 0) { 39 return err(new ValidationError('Card ID is required')); 40 } 41 42 try { 43 // Get user IDs who have this card in their library 44 const userIds = await this.cardQueryRepo.getLibrariesForCard( 45 query.cardId, 46 ); 47 48 // Fetch profiles for all users using ProfileEnricher 49 const profileEnricher = new ProfileEnricher(this.profileService); 50 const profileMapResult = await profileEnricher.buildProfileMap( 51 userIds, 52 undefined, // No calling user 53 { 54 skipFailures: true, // Skip failed profiles 55 mapToUser: false, // Use inline profile (without isFollowing) 56 }, 57 ); 58 59 if (profileMapResult.isErr()) { 60 return err( 61 new Error( 62 `Failed to fetch user profiles: ${profileMapResult.error.message}`, 63 ), 64 ); 65 } 66 67 const profileMap = profileMapResult.value; 68 69 // Convert map to array 70 const users: UserProfileDTO[] = Array.from(profileMap.values()); 71 72 return ok({ 73 cardId: query.cardId, 74 users, 75 totalCount: users.length, 76 }); 77 } catch (error) { 78 return err( 79 new Error( 80 `Failed to get libraries for card: ${error instanceof Error ? error.message : 'Unknown error'}`, 81 ), 82 ); 83 } 84 } 85}