120 lines
3.9 KiB
TypeScript
120 lines
3.9 KiB
TypeScript
/**
|
|
* Application Use Cases: Notification Preferences
|
|
*
|
|
* Manages user notification preferences.
|
|
*/
|
|
|
|
import { NotificationPreference } from '../../domain/entities/NotificationPreference';
|
|
import type { ChannelPreference, TypePreference } from '../../domain/entities/NotificationPreference';
|
|
import type { INotificationPreferenceRepository } from '../../domain/repositories/INotificationPreferenceRepository';
|
|
import type { NotificationType } from '../../domain/value-objects/NotificationType';
|
|
import type { NotificationChannel } from '../../domain/value-objects/NotificationChannel';
|
|
|
|
/**
|
|
* Query: GetNotificationPreferencesQuery
|
|
*/
|
|
export class GetNotificationPreferencesQuery {
|
|
constructor(
|
|
private readonly preferenceRepository: INotificationPreferenceRepository,
|
|
) {}
|
|
|
|
async execute(driverId: string): Promise<NotificationPreference> {
|
|
return this.preferenceRepository.getOrCreateDefault(driverId);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use Case: UpdateChannelPreferenceUseCase
|
|
*/
|
|
export interface UpdateChannelPreferenceCommand {
|
|
driverId: string;
|
|
channel: NotificationChannel;
|
|
preference: ChannelPreference;
|
|
}
|
|
|
|
export class UpdateChannelPreferenceUseCase {
|
|
constructor(
|
|
private readonly preferenceRepository: INotificationPreferenceRepository,
|
|
) {}
|
|
|
|
async execute(command: UpdateChannelPreferenceCommand): Promise<void> {
|
|
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
|
const updated = preferences.updateChannel(command.channel, command.preference);
|
|
await this.preferenceRepository.save(updated);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use Case: UpdateTypePreferenceUseCase
|
|
*/
|
|
export interface UpdateTypePreferenceCommand {
|
|
driverId: string;
|
|
type: NotificationType;
|
|
preference: TypePreference;
|
|
}
|
|
|
|
export class UpdateTypePreferenceUseCase {
|
|
constructor(
|
|
private readonly preferenceRepository: INotificationPreferenceRepository,
|
|
) {}
|
|
|
|
async execute(command: UpdateTypePreferenceCommand): Promise<void> {
|
|
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
|
const updated = preferences.updateTypePreference(command.type, command.preference);
|
|
await this.preferenceRepository.save(updated);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use Case: UpdateQuietHoursUseCase
|
|
*/
|
|
export interface UpdateQuietHoursCommand {
|
|
driverId: string;
|
|
startHour: number | undefined;
|
|
endHour: number | undefined;
|
|
}
|
|
|
|
export class UpdateQuietHoursUseCase {
|
|
constructor(
|
|
private readonly preferenceRepository: INotificationPreferenceRepository,
|
|
) {}
|
|
|
|
async execute(command: UpdateQuietHoursCommand): Promise<void> {
|
|
// Validate hours if provided
|
|
if (command.startHour !== undefined && (command.startHour < 0 || command.startHour > 23)) {
|
|
throw new NotificationDomainError('Start hour must be between 0 and 23');
|
|
}
|
|
if (command.endHour !== undefined && (command.endHour < 0 || command.endHour > 23)) {
|
|
throw new NotificationDomainError('End hour must be between 0 and 23');
|
|
}
|
|
|
|
const preferences = await this.preferenceRepository.getOrCreateDefault(command.driverId);
|
|
const updated = preferences.updateQuietHours(command.startHour, command.endHour);
|
|
await this.preferenceRepository.save(updated);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use Case: SetDigestModeUseCase
|
|
*/
|
|
export interface SetDigestModeCommand {
|
|
driverId: string;
|
|
enabled: boolean;
|
|
frequencyHours?: number;
|
|
}
|
|
|
|
export class SetDigestModeUseCase {
|
|
constructor(
|
|
private readonly preferenceRepository: INotificationPreferenceRepository,
|
|
) {}
|
|
|
|
async execute(command: SetDigestModeCommand): Promise<void> {
|
|
if (command.frequencyHours !== undefined && command.frequencyHours < 1) {
|
|
throw new NotificationDomainError('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);
|
|
}
|
|
} |