refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -4,14 +4,13 @@
* Manages user notification preferences.
*/
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
import type { NotificationType, NotificationChannel } from '../../domain/types/NotificationTypes';
// import { NotificationDomainError } from '../../domain/errors/NotificationDomainError';
/**
* Query: GetNotificationPreferencesQuery
@@ -29,24 +28,22 @@ export type GetNotificationPreferencesErrorCode = 'REPOSITORY_ERROR';
export class GetNotificationPreferencesQuery {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly output: UseCaseOutputPort<GetNotificationPreferencesResult>,
private readonly logger: Logger,
) {}
async execute(
input: GetNotificationPreferencesInput,
): Promise<Result<void, ApplicationErrorCode<GetNotificationPreferencesErrorCode, { message: string }>>> {
): Promise<Result<GetNotificationPreferencesResult, ApplicationErrorCode<GetNotificationPreferencesErrorCode, { message: string }>>> {
const { driverId } = input;
this.logger.debug(`Fetching notification preferences for driver: ${driverId}`);
try {
const preferences = await this.preferenceRepository.getOrCreateDefault(driverId);
this.logger.info(`Successfully fetched preferences for driver: ${driverId}`);
this.output.present({ preference: preferences });
return Result.ok(undefined);
return Result.ok<GetNotificationPreferencesResult, ApplicationErrorCode<GetNotificationPreferencesErrorCode, { message: string }>>({ preference: preferences });
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error(`Failed to fetch preferences for driver: ${driverId}`, err);
return Result.err<void, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>({
return Result.err<GetNotificationPreferencesResult, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
@@ -74,13 +71,12 @@ export type UpdateChannelPreferenceErrorCode =
export class UpdateChannelPreferenceUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly output: UseCaseOutputPort<UpdateChannelPreferenceResult>,
private readonly logger: Logger,
) {}
async execute(
command: UpdateChannelPreferenceCommand,
): Promise<Result<void, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>> {
): Promise<Result<UpdateChannelPreferenceResult, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>> {
this.logger.debug(
`Updating channel preference for driver: ${command.driverId}, channel: ${command.channel}, preference: ${JSON.stringify(command.preference)}`,
);
@@ -93,15 +89,17 @@ export class UpdateChannelPreferenceUseCase {
this.logger.info(
`Successfully updated channel preference for driver: ${command.driverId}`,
);
this.output.present({ driverId: command.driverId, channel: command.channel });
return Result.ok(undefined);
return Result.ok<UpdateChannelPreferenceResult, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>({
driverId: command.driverId,
channel: command.channel,
});
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error(
`Failed to update channel preference for driver: ${command.driverId}, channel: ${command.channel}`,
err,
);
return Result.err<void, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>({
return Result.err<UpdateChannelPreferenceResult, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
@@ -128,13 +126,12 @@ export type UpdateTypePreferenceErrorCode = 'REPOSITORY_ERROR';
export class UpdateTypePreferenceUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly output: UseCaseOutputPort<UpdateTypePreferenceResult>,
private readonly logger: Logger,
) {}
async execute(
command: UpdateTypePreferenceCommand,
): Promise<Result<void, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>> {
): Promise<Result<UpdateTypePreferenceResult, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>> {
this.logger.debug(
`Updating type preference for driver: ${command.driverId}, type: ${command.type}, preference: ${JSON.stringify(command.preference)}`,
);
@@ -147,15 +144,17 @@ export class UpdateTypePreferenceUseCase {
this.logger.info(
`Successfully updated type preference for driver: ${command.driverId}`,
);
this.output.present({ driverId: command.driverId, type: command.type });
return Result.ok(undefined);
return Result.ok<UpdateTypePreferenceResult, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>({
driverId: command.driverId,
type: command.type,
});
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error(
`Failed to update type preference for driver: ${command.driverId}, type: ${command.type}`,
err,
);
return Result.err<void, ApplicationErrorCode<GetNotificationPreferencesErrorCode, { message: string }>>({
return Result.err<UpdateTypePreferenceResult, ApplicationErrorCode<GetNotificationPreferencesErrorCode, { message: string }>>({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
@@ -186,13 +185,12 @@ export type UpdateQuietHoursErrorCode =
export class UpdateQuietHoursUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly output: UseCaseOutputPort<UpdateQuietHoursResult>,
private readonly logger: Logger,
) {}
async execute(
command: UpdateQuietHoursCommand,
): Promise<Result<void, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>> {
): Promise<Result<UpdateQuietHoursResult, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>> {
this.logger.debug(
`Updating quiet hours for driver: ${command.driverId}, startHour: ${command.startHour}, endHour: ${command.endHour}`,
);
@@ -202,7 +200,7 @@ export class UpdateQuietHoursUseCase {
this.logger.warn(
`Invalid start hour provided for driver: ${command.driverId}. startHour: ${command.startHour}`,
);
return Result.err<void, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>({
return Result.err<UpdateQuietHoursResult, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>({
code: 'INVALID_START_HOUR',
details: { message: 'Start hour must be between 0 and 23' },
});
@@ -211,7 +209,7 @@ export class UpdateQuietHoursUseCase {
this.logger.warn(
`Invalid end hour provided for driver: ${command.driverId}. endHour: ${command.endHour}`,
);
return Result.err<void, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>({
return Result.err<UpdateQuietHoursResult, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>({
code: 'INVALID_END_HOUR',
details: { message: 'End hour must be between 0 and 23' },
});
@@ -226,16 +224,15 @@ export class UpdateQuietHoursUseCase {
);
await this.preferenceRepository.save(updated);
this.logger.info(`Successfully updated quiet hours for driver: ${command.driverId}`);
this.output.present({
return Result.ok<UpdateQuietHoursResult, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>({
driverId: command.driverId,
startHour: command.startHour,
endHour: command.endHour,
});
return Result.ok(undefined);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
this.logger.error(`Failed to update quiet hours for driver: ${command.driverId}`, err);
return Result.err<void, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>({
return Result.err<UpdateQuietHoursResult, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
@@ -265,14 +262,13 @@ export type SetDigestModeErrorCode =
export class SetDigestModeUseCase {
constructor(
private readonly preferenceRepository: INotificationPreferenceRepository,
private readonly output: UseCaseOutputPort<SetDigestModeResult>,
) {}
async execute(
command: SetDigestModeCommand,
): Promise<Result<void, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>> {
): Promise<Result<SetDigestModeResult, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>> {
if (command.frequencyHours !== undefined && command.frequencyHours < 1) {
return Result.err<void, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>({
return Result.err<SetDigestModeResult, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>({
code: 'INVALID_FREQUENCY',
details: { message: 'Digest frequency must be at least 1 hour' },
});
@@ -292,14 +288,13 @@ export class SetDigestModeUseCase {
enabled: command.enabled,
frequencyHours: command.frequencyHours,
};
this.output.present(result);
return Result.ok(undefined);
return Result.ok<SetDigestModeResult, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>(result);
} catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
return Result.err<void, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>({
return Result.err<SetDigestModeResult, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>({
code: 'REPOSITORY_ERROR',
details: { message: err.message },
});
}
}
}
}