106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import type { Logger } from '@core/shared/domain/Logger';
|
|
import { Result } from '@core/shared/domain/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import { SocialGraphRepository } from '../../domain/repositories/SocialGraphRepository';
|
|
import type { SocialFriendSummary, SocialUserSummary } from '../types/SocialUser';
|
|
|
|
export interface GetCurrentUserSocialParams {
|
|
driverId: string;
|
|
}
|
|
|
|
export type GetCurrentUserSocialInput = GetCurrentUserSocialParams;
|
|
|
|
export interface GetCurrentUserSocialResult {
|
|
currentUser: SocialUserSummary;
|
|
friends: SocialFriendSummary[];
|
|
}
|
|
|
|
export type GetCurrentUserSocialErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export type GetCurrentUserSocialApplicationError = ApplicationErrorCode<
|
|
GetCurrentUserSocialErrorCode,
|
|
{ message: string }
|
|
>;
|
|
|
|
/**
|
|
* Application-level use case to retrieve the current user's social context.
|
|
*
|
|
* Keeps orchestration in the social bounded context while delegating
|
|
* data access to domain repositories.
|
|
* Returns Result directly without calling presenter.
|
|
*/
|
|
export class GetCurrentUserSocialUseCase {
|
|
constructor(
|
|
private readonly socialGraphRepository: SocialGraphRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(
|
|
input: GetCurrentUserSocialInput,
|
|
): Promise<Result<GetCurrentUserSocialResult, GetCurrentUserSocialApplicationError>> {
|
|
this.logger.debug('GetCurrentUserSocialUseCase.execute: Starting execution', { input });
|
|
|
|
try {
|
|
const { driverId } = input;
|
|
|
|
this.logger.debug(
|
|
'GetCurrentUserSocialUseCase.execute: Fetching friends for driverId',
|
|
{ driverId },
|
|
);
|
|
const friendsDomain = await this.socialGraphRepository.getFriends(driverId);
|
|
this.logger.debug(
|
|
'GetCurrentUserSocialUseCase.execute: Successfully fetched friends from social graph repository',
|
|
{ friendsCount: friendsDomain.length },
|
|
);
|
|
if (friendsDomain.length === 0) {
|
|
this.logger.warn(
|
|
`GetCurrentUserSocialUseCase.execute: No friends found for driverId: ${driverId}`,
|
|
);
|
|
}
|
|
|
|
// The social graph context currently only knows about relationships.
|
|
// Profile fields for the current user are expected to be enriched by identity/profile contexts.
|
|
const friends: SocialFriendSummary[] = friendsDomain.map((friend) => ({
|
|
driverId: friend.id,
|
|
displayName: friend.name.toString(),
|
|
avatarUrl: '',
|
|
countryCode: '',
|
|
isOnline: false,
|
|
lastSeen: new Date(),
|
|
}));
|
|
|
|
const currentUser: SocialUserSummary = {
|
|
driverId,
|
|
displayName: '',
|
|
avatarUrl: '',
|
|
countryCode: '',
|
|
};
|
|
|
|
const result: GetCurrentUserSocialResult = {
|
|
currentUser,
|
|
friends,
|
|
};
|
|
|
|
this.logger.info(
|
|
'GetCurrentUserSocialUseCase.execute: Successfully retrieved current user social data',
|
|
);
|
|
|
|
return Result.ok(result);
|
|
} catch (error) {
|
|
const err = error instanceof Error ? error : new Error(String(error));
|
|
|
|
this.logger.error(
|
|
'GetCurrentUserSocialUseCase.execute: Error during execution',
|
|
err,
|
|
{ input },
|
|
);
|
|
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: {
|
|
message: err.message,
|
|
},
|
|
} as GetCurrentUserSocialApplicationError);
|
|
}
|
|
}
|
|
} |