refactor use cases
This commit is contained in:
@@ -1,43 +1,65 @@
|
||||
import type { AsyncUseCase , Logger } from '@core/shared/application';
|
||||
import type { Logger , UseCaseOutputPort } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { ISocialGraphRepository } from '../../domain/repositories/ISocialGraphRepository';
|
||||
import type { CurrentUserSocialDTO } from '../dto/CurrentUserSocialDTO';
|
||||
import type { FriendDTO } from '../dto/FriendDTO';
|
||||
import type {
|
||||
CurrentUserSocialViewModel,
|
||||
ICurrentUserSocialPresenter,
|
||||
} from '../presenters/ISocialPresenters';
|
||||
|
||||
export interface GetCurrentUserSocialParams {
|
||||
driverId: string;
|
||||
}
|
||||
|
||||
export type GetCurrentUserSocialInput = GetCurrentUserSocialParams;
|
||||
|
||||
export interface GetCurrentUserSocialResult {
|
||||
currentUser: CurrentUserSocialDTO;
|
||||
friends: FriendDTO[];
|
||||
}
|
||||
|
||||
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 and presenting via a presenter.
|
||||
* data access to domain repositories and presenting via an output port.
|
||||
*/
|
||||
export class GetCurrentUserSocialUseCase
|
||||
implements AsyncUseCase<GetCurrentUserSocialParams, void> {
|
||||
export class GetCurrentUserSocialUseCase {
|
||||
constructor(
|
||||
private readonly socialGraphRepository: ISocialGraphRepository,
|
||||
public readonly presenter: ICurrentUserSocialPresenter,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetCurrentUserSocialResult>,
|
||||
) {}
|
||||
|
||||
async execute(params: GetCurrentUserSocialParams): Promise<void> {
|
||||
this.logger.debug('GetCurrentUserSocialUseCase: Starting execution', { params });
|
||||
try {
|
||||
const { driverId } = params;
|
||||
async execute(
|
||||
input: GetCurrentUserSocialInput,
|
||||
): Promise<Result<void, GetCurrentUserSocialApplicationError>> {
|
||||
this.logger.debug('GetCurrentUserSocialUseCase.execute: Starting execution', { input });
|
||||
|
||||
this.logger.debug(`GetCurrentUserSocialUseCase: Fetching friends for driverId: ${driverId}`);
|
||||
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: Successfully fetched friends from social graph repository', { friendsCount: friendsDomain.length });
|
||||
this.logger.debug(
|
||||
'GetCurrentUserSocialUseCase.execute: Successfully fetched friends from social graph repository',
|
||||
{ friendsCount: friendsDomain.length },
|
||||
);
|
||||
if (friendsDomain.length === 0) {
|
||||
this.logger.warn(`GetCurrentUserSocialUseCase: No friends found for driverId: ${driverId}`);
|
||||
this.logger.warn(
|
||||
`GetCurrentUserSocialUseCase.execute: No friends found for driverId: ${driverId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const friends: FriendDTO[] = friendsDomain.map((friend) => ({
|
||||
const friends: FriendDTO[] = friendsDomain.map(friend => ({
|
||||
driverId: friend.id,
|
||||
displayName: friend.name,
|
||||
avatarUrl: '',
|
||||
@@ -52,16 +74,32 @@ export class GetCurrentUserSocialUseCase
|
||||
countryCode: '',
|
||||
};
|
||||
|
||||
const viewModel: CurrentUserSocialViewModel = {
|
||||
const result: GetCurrentUserSocialResult = {
|
||||
currentUser,
|
||||
friends,
|
||||
};
|
||||
|
||||
this.presenter.present(viewModel);
|
||||
this.logger.info('GetCurrentUserSocialUseCase: Successfully presented current user social data');
|
||||
this.output.present(result);
|
||||
this.logger.info(
|
||||
'GetCurrentUserSocialUseCase.execute: Successfully presented current user social data',
|
||||
);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('GetCurrentUserSocialUseCase: Error during execution', { error });
|
||||
throw 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user