refactor use cases
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
* Manages user notification preferences.
|
||||
*/
|
||||
|
||||
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 { NotificationPreference } from '../../domain/entities/NotificationPreference';
|
||||
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
|
||||
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
|
||||
@@ -14,21 +16,40 @@ import { NotificationDomainError } from '../../domain/errors/NotificationDomainE
|
||||
/**
|
||||
* Query: GetNotificationPreferencesQuery
|
||||
*/
|
||||
export class GetNotificationPreferencesQuery implements AsyncUseCase<string, NotificationPreference> {
|
||||
export interface GetNotificationPreferencesInput {
|
||||
driverId: string;
|
||||
}
|
||||
|
||||
export interface GetNotificationPreferencesResult {
|
||||
preference: NotificationPreference;
|
||||
}
|
||||
|
||||
export type GetNotificationPreferencesErrorCode = 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetNotificationPreferencesQuery {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly output: UseCaseOutputPort<GetNotificationPreferencesResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(driverId: string): Promise<NotificationPreference> {
|
||||
async execute(
|
||||
input: GetNotificationPreferencesInput,
|
||||
): Promise<Result<void, 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}`);
|
||||
return preferences;
|
||||
this.output.present({ preference: preferences });
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to fetch preferences for driver: ${driverId}`, error);
|
||||
throw 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({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,22 +63,48 @@ export interface UpdateChannelPreferenceCommand {
|
||||
preference: ChannelPreference;
|
||||
}
|
||||
|
||||
export class UpdateChannelPreferenceUseCase implements AsyncUseCase<UpdateChannelPreferenceCommand, void> {
|
||||
export interface UpdateChannelPreferenceResult {
|
||||
driverId: string;
|
||||
channel: NotificationChannel;
|
||||
}
|
||||
|
||||
export type UpdateChannelPreferenceErrorCode =
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export class UpdateChannelPreferenceUseCase {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly output: UseCaseOutputPort<UpdateChannelPreferenceResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateChannelPreferenceCommand): Promise<void> {
|
||||
this.logger.debug(`Updating channel preference for driver: ${command.driverId}, channel: ${command.channel}, preference: ${command.preference}`);
|
||||
async execute(
|
||||
command: UpdateChannelPreferenceCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<UpdateChannelPreferenceErrorCode, { message: string }>>> {
|
||||
this.logger.debug(
|
||||
`Updating channel preference for driver: ${command.driverId}, channel: ${command.channel}, preference: ${JSON.stringify(command.preference)}`,
|
||||
);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(
|
||||
command.driverId,
|
||||
);
|
||||
const updated = preferences.updateChannel(command.channel, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated channel preference for driver: ${command.driverId}`);
|
||||
this.logger.info(
|
||||
`Successfully updated channel preference for driver: ${command.driverId}`,
|
||||
);
|
||||
this.output.present({ driverId: command.driverId, channel: command.channel });
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update channel preference for driver: ${command.driverId}, channel: ${command.channel}`, error);
|
||||
throw 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({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,22 +118,47 @@ export interface UpdateTypePreferenceCommand {
|
||||
preference: TypePreference;
|
||||
}
|
||||
|
||||
export class UpdateTypePreferenceUseCase implements AsyncUseCase<UpdateTypePreferenceCommand, void> {
|
||||
export interface UpdateTypePreferenceResult {
|
||||
driverId: string;
|
||||
type: NotificationType;
|
||||
}
|
||||
|
||||
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<void> {
|
||||
this.logger.debug(`Updating type preference for driver: ${command.driverId}, type: ${command.type}, preference: ${command.preference}`);
|
||||
async execute(
|
||||
command: UpdateTypePreferenceCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<UpdateTypePreferenceErrorCode, { message: string }>>> {
|
||||
this.logger.debug(
|
||||
`Updating type preference for driver: ${command.driverId}, type: ${command.type}, preference: ${JSON.stringify(command.preference)}`,
|
||||
);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(
|
||||
command.driverId,
|
||||
);
|
||||
const updated = preferences.updateTypePreference(command.type, command.preference);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated type preference for driver: ${command.driverId}`);
|
||||
this.logger.info(
|
||||
`Successfully updated type preference for driver: ${command.driverId}`,
|
||||
);
|
||||
this.output.present({ driverId: command.driverId, type: command.type });
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update type preference for driver: ${command.driverId}, type: ${command.type}`, error);
|
||||
throw 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({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,32 +172,73 @@ export interface UpdateQuietHoursCommand {
|
||||
endHour: number | undefined;
|
||||
}
|
||||
|
||||
export class UpdateQuietHoursUseCase implements AsyncUseCase<UpdateQuietHoursCommand, void> {
|
||||
export interface UpdateQuietHoursResult {
|
||||
driverId: string;
|
||||
startHour: number | undefined;
|
||||
endHour: number | undefined;
|
||||
}
|
||||
|
||||
export type UpdateQuietHoursErrorCode =
|
||||
| 'INVALID_START_HOUR'
|
||||
| 'INVALID_END_HOUR'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export class UpdateQuietHoursUseCase {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly output: UseCaseOutputPort<UpdateQuietHoursResult>,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateQuietHoursCommand): Promise<void> {
|
||||
this.logger.debug(`Updating quiet hours for driver: ${command.driverId}, startHour: ${command.startHour}, endHour: ${command.endHour}`);
|
||||
async execute(
|
||||
command: UpdateQuietHoursCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<UpdateQuietHoursErrorCode, { message: string }>>> {
|
||||
this.logger.debug(
|
||||
`Updating quiet hours for driver: ${command.driverId}, startHour: ${command.startHour}, endHour: ${command.endHour}`,
|
||||
);
|
||||
try {
|
||||
// Validate hours if provided
|
||||
if (command.startHour !== undefined && (command.startHour < 0 || command.startHour > 23)) {
|
||||
this.logger.warn(`Invalid start hour provided for driver: ${command.driverId}. startHour: ${command.startHour}`);
|
||||
throw new NotificationDomainError('Start hour must be between 0 and 23');
|
||||
this.logger.warn(
|
||||
`Invalid start hour provided for driver: ${command.driverId}. startHour: ${command.startHour}`,
|
||||
);
|
||||
return Result.err({
|
||||
code: 'INVALID_START_HOUR',
|
||||
details: { message: 'Start hour must be between 0 and 23' },
|
||||
});
|
||||
}
|
||||
if (command.endHour !== undefined && (command.endHour < 0 || command.endHour > 23)) {
|
||||
this.logger.warn(`Invalid end hour provided for driver: ${command.driverId}. endHour: ${command.endHour}`);
|
||||
throw new NotificationDomainError('End hour must be between 0 and 23');
|
||||
this.logger.warn(
|
||||
`Invalid end hour provided for driver: ${command.driverId}. endHour: ${command.endHour}`,
|
||||
);
|
||||
return Result.err({
|
||||
code: 'INVALID_END_HOUR',
|
||||
details: { message: 'End hour must be between 0 and 23' },
|
||||
});
|
||||
}
|
||||
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.updateQuietHours(command.startHour, command.endHour);
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(
|
||||
command.driverId,
|
||||
);
|
||||
const updated = preferences.updateQuietHours(
|
||||
command.startHour,
|
||||
command.endHour,
|
||||
);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.logger.info(`Successfully updated quiet hours for driver: ${command.driverId}`);
|
||||
this.output.present({
|
||||
driverId: command.driverId,
|
||||
startHour: command.startHour,
|
||||
endHour: command.endHour,
|
||||
});
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to update quiet hours for driver: ${command.driverId}`, error);
|
||||
throw 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({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,18 +252,53 @@ export interface SetDigestModeCommand {
|
||||
frequencyHours?: number;
|
||||
}
|
||||
|
||||
export class SetDigestModeUseCase implements AsyncUseCase<SetDigestModeCommand, void> {
|
||||
export interface SetDigestModeResult {
|
||||
driverId: string;
|
||||
enabled: boolean;
|
||||
frequencyHours?: number;
|
||||
}
|
||||
|
||||
export type SetDigestModeErrorCode =
|
||||
| 'INVALID_FREQUENCY'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export class SetDigestModeUseCase {
|
||||
constructor(
|
||||
private readonly preferenceRepository: INotificationPreferenceRepository,
|
||||
private readonly output: UseCaseOutputPort<SetDigestModeResult>,
|
||||
) {}
|
||||
|
||||
async execute(command: SetDigestModeCommand): Promise<void> {
|
||||
async execute(
|
||||
command: SetDigestModeCommand,
|
||||
): Promise<Result<void, ApplicationErrorCode<SetDigestModeErrorCode, { message: string }>>> {
|
||||
if (command.frequencyHours !== undefined && command.frequencyHours < 1) {
|
||||
throw new NotificationDomainError('Digest frequency must be at least 1 hour');
|
||||
return Result.err({
|
||||
code: 'INVALID_FREQUENCY',
|
||||
details: { message: 'Digest frequency must be at least 1 hour' },
|
||||
});
|
||||
}
|
||||
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
||||
const updated = preferences.setDigestMode(command.enabled, command.frequencyHours);
|
||||
await this.preferenceRepository.save(updated);
|
||||
try {
|
||||
const preferences = await this.preferenceRepository.getOrCreateDefault(
|
||||
command.driverId,
|
||||
);
|
||||
const updated = preferences.setDigestMode(
|
||||
command.enabled,
|
||||
command.frequencyHours,
|
||||
);
|
||||
await this.preferenceRepository.save(updated);
|
||||
this.output.present({
|
||||
driverId: command.driverId,
|
||||
enabled: command.enabled,
|
||||
frequencyHours: command.frequencyHours,
|
||||
});
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message: err.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user