This repository has no description
2.9 kB
103 lines
1import { UseCase } from 'src/shared/core/UseCase';
2import { IProfileService } from '../../../domain/services/IProfileService';
3import { err, ok, Result } from 'src/shared/core/Result';
4import { DIDOrHandle } from 'src/modules/atproto/domain/DIDOrHandle';
5import { IIdentityResolutionService } from 'src/modules/atproto/domain/services/IIdentityResolutionService';
6
7export interface GetMyProfileQuery {
8 userId: string;
9 callerDid?: string;
10}
11
12export interface GetMyProfileResult {
13 id: string;
14 name: string;
15 handle: string;
16 description?: string;
17 avatarUrl?: string;
18}
19
20export class ValidationError extends Error {
21 constructor(message: string) {
22 super(message);
23 this.name = 'ValidationError';
24 }
25}
26
27export class GetProfileUseCase
28 implements UseCase<GetMyProfileQuery, Result<GetMyProfileResult>>
29{
30 constructor(
31 private profileService: IProfileService,
32 private identityResolver: IIdentityResolutionService,
33 ) {}
34
35 async execute(query: GetMyProfileQuery): Promise<Result<GetMyProfileResult>> {
36 // Validate user ID
37 if (!query.userId || query.userId.trim().length === 0) {
38 return err(new ValidationError('User ID is required'));
39 }
40
41 // Parse and validate user identifier
42 const identifierResult = DIDOrHandle.create(query.userId);
43 if (identifierResult.isErr()) {
44 return err(new ValidationError('Invalid user identifier'));
45 }
46
47 // Resolve to DID
48 const didResult = await this.identityResolver.resolveToDID(
49 identifierResult.value,
50 );
51 if (didResult.isErr()) {
52 return err(
53 new ValidationError(
54 `Could not resolve user identifier: ${didResult.error.message}`,
55 ),
56 );
57 }
58 let callerDid = undefined;
59 if (query.callerDid) {
60 const callerDidResult = DIDOrHandle.create(query.callerDid);
61 if (callerDidResult.isErr()) {
62 return err(
63 new ValidationError(
64 `Invalid caller DID: ${callerDidResult.error.message}`,
65 ),
66 );
67 }
68 callerDid = callerDidResult.value.value;
69 }
70
71 try {
72 // Fetch user profile using the resolved DID
73 const profileResult = await this.profileService.getProfile(
74 didResult.value.value,
75 callerDid,
76 );
77
78 if (profileResult.isErr()) {
79 return err(
80 new Error(
81 `Failed to fetch user profile: ${profileResult.error instanceof Error ? profileResult.error.message : 'Unknown error'}`,
82 ),
83 );
84 }
85
86 const profile = profileResult.value;
87
88 return ok({
89 id: profile.id,
90 name: profile.name,
91 handle: profile.handle,
92 description: profile.bio,
93 avatarUrl: profile.avatarUrl,
94 });
95 } catch (error) {
96 return err(
97 new Error(
98 `Failed to get user profile: ${error instanceof Error ? error.message : 'Unknown error'}`,
99 ),
100 );
101 }
102 }
103}