194 lines
6.2 KiB
TypeScript
194 lines
6.2 KiB
TypeScript
/**
|
|
* Domain Entity: NotificationPreference
|
|
*
|
|
* Represents a user's notification preferences for different channels and types.
|
|
*/
|
|
|
|
import type { NotificationType } from '../value-objects/NotificationType';
|
|
import type { NotificationChannel } from '../value-objects/NotificationChannel';
|
|
import { NotificationDomainError } from '../errors/NotificationDomainError';
|
|
import { DEFAULT_ENABLED_CHANNELS } from '../value-objects/NotificationChannel';
|
|
|
|
export interface ChannelPreference {
|
|
/** Whether this channel is enabled */
|
|
enabled: boolean;
|
|
/** Channel-specific settings (e.g., discord webhook URL, email address) */
|
|
settings?: Record<string, string>;
|
|
}
|
|
|
|
export interface TypePreference {
|
|
/** Whether notifications of this type are enabled */
|
|
enabled: boolean;
|
|
/** Which channels to use for this type (overrides global) */
|
|
channels?: NotificationChannel[];
|
|
}
|
|
|
|
export interface NotificationPreferenceProps {
|
|
/** Driver ID this preference belongs to */
|
|
driverId: string;
|
|
/** Global channel preferences */
|
|
channels: Record<NotificationChannel, ChannelPreference>;
|
|
/** Per-type preferences (optional overrides) */
|
|
typePreferences?: Partial<Record<NotificationType, TypePreference>>;
|
|
/** Whether to receive digest emails instead of individual notifications */
|
|
digestMode?: boolean;
|
|
/** Digest frequency in hours (e.g., 24 for daily) */
|
|
digestFrequencyHours?: number;
|
|
/** Quiet hours start (0-23) */
|
|
quietHoursStart?: number;
|
|
/** Quiet hours end (0-23) */
|
|
quietHoursEnd?: number;
|
|
/** Last updated timestamp */
|
|
updatedAt: Date;
|
|
}
|
|
|
|
export class NotificationPreference {
|
|
private constructor(private readonly props: NotificationPreferenceProps) {}
|
|
|
|
static create(props: Omit<NotificationPreferenceProps, 'updatedAt'> & { updatedAt?: Date }): NotificationPreference {
|
|
if (!props.driverId) throw new NotificationDomainError('Driver ID is required');
|
|
if (!props.channels) throw new NotificationDomainError('Channel preferences are required');
|
|
|
|
return new NotificationPreference({
|
|
...props,
|
|
updatedAt: props.updatedAt ?? new Date(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create default preferences for a new user
|
|
*/
|
|
static createDefault(driverId: string): NotificationPreference {
|
|
return new NotificationPreference({
|
|
driverId,
|
|
channels: {
|
|
in_app: { enabled: true },
|
|
email: { enabled: false },
|
|
discord: { enabled: false },
|
|
push: { enabled: false },
|
|
},
|
|
digestMode: false,
|
|
updatedAt: new Date(),
|
|
});
|
|
}
|
|
|
|
get driverId(): string { return this.props.driverId; }
|
|
get channels(): Record<NotificationChannel, ChannelPreference> { return { ...this.props.channels }; }
|
|
get typePreferences(): Partial<Record<NotificationType, TypePreference>> | undefined {
|
|
return this.props.typePreferences ? { ...this.props.typePreferences } : undefined;
|
|
}
|
|
get digestMode(): boolean { return this.props.digestMode ?? false; }
|
|
get digestFrequencyHours(): number { return this.props.digestFrequencyHours ?? 24; }
|
|
get quietHoursStart(): number | undefined { return this.props.quietHoursStart; }
|
|
get quietHoursEnd(): number | undefined { return this.props.quietHoursEnd; }
|
|
get updatedAt(): Date { return this.props.updatedAt; }
|
|
|
|
/**
|
|
* Check if a specific channel is enabled
|
|
*/
|
|
isChannelEnabled(channel: NotificationChannel): boolean {
|
|
return this.props.channels[channel]?.enabled ?? false;
|
|
}
|
|
|
|
/**
|
|
* Check if a specific notification type is enabled
|
|
*/
|
|
isTypeEnabled(type: NotificationType): boolean {
|
|
const typePref = this.props.typePreferences?.[type];
|
|
return typePref?.enabled ?? true; // Default to enabled if not specified
|
|
}
|
|
|
|
/**
|
|
* Get enabled channels for a specific notification type
|
|
*/
|
|
getEnabledChannelsForType(type: NotificationType): NotificationChannel[] {
|
|
// Check type-specific channel overrides
|
|
const typePref = this.props.typePreferences?.[type];
|
|
if (typePref?.channels && typePref.channels.length > 0) {
|
|
return typePref.channels.filter(ch => this.isChannelEnabled(ch));
|
|
}
|
|
|
|
// Fall back to globally enabled channels
|
|
return (Object.keys(this.props.channels) as NotificationChannel[])
|
|
.filter(ch => this.isChannelEnabled(ch));
|
|
}
|
|
|
|
/**
|
|
* Check if current time is in quiet hours
|
|
*/
|
|
isInQuietHours(): boolean {
|
|
if (this.props.quietHoursStart === undefined || this.props.quietHoursEnd === undefined) {
|
|
return false;
|
|
}
|
|
|
|
const now = new Date();
|
|
const currentHour = now.getHours();
|
|
|
|
if (this.props.quietHoursStart < this.props.quietHoursEnd) {
|
|
// Normal range (e.g., 22:00 to 07:00 next day is NOT this case)
|
|
return currentHour >= this.props.quietHoursStart && currentHour < this.props.quietHoursEnd;
|
|
} else {
|
|
// Overnight range (e.g., 22:00 to 07:00)
|
|
return currentHour >= this.props.quietHoursStart || currentHour < this.props.quietHoursEnd;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update channel preference
|
|
*/
|
|
updateChannel(channel: NotificationChannel, preference: ChannelPreference): NotificationPreference {
|
|
return new NotificationPreference({
|
|
...this.props,
|
|
channels: {
|
|
...this.props.channels,
|
|
[channel]: preference,
|
|
},
|
|
updatedAt: new Date(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update type preference
|
|
*/
|
|
updateTypePreference(type: NotificationType, preference: TypePreference): NotificationPreference {
|
|
return new NotificationPreference({
|
|
...this.props,
|
|
typePreferences: {
|
|
...this.props.typePreferences,
|
|
[type]: preference,
|
|
},
|
|
updatedAt: new Date(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update quiet hours
|
|
*/
|
|
updateQuietHours(start: number | undefined, end: number | undefined): NotificationPreference {
|
|
return new NotificationPreference({
|
|
...this.props,
|
|
quietHoursStart: start,
|
|
quietHoursEnd: end,
|
|
updatedAt: new Date(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Toggle digest mode
|
|
*/
|
|
setDigestMode(enabled: boolean, frequencyHours?: number): NotificationPreference {
|
|
return new NotificationPreference({
|
|
...this.props,
|
|
digestMode: enabled,
|
|
digestFrequencyHours: frequencyHours ?? this.props.digestFrequencyHours,
|
|
updatedAt: new Date(),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Convert to plain object for serialization
|
|
*/
|
|
toJSON(): NotificationPreferenceProps {
|
|
return { ...this.props };
|
|
}
|
|
} |