refactor
This commit is contained in:
@@ -1,38 +1,45 @@
|
||||
import type { IValueObject } from '@core/shared/domain';
|
||||
import { WeekdaySet } from './WeekdaySet';
|
||||
import { MonthlyRecurrencePattern } from './MonthlyRecurrencePattern';
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
|
||||
export type WeeklyRecurrenceStrategy = {
|
||||
export type WeeklyRecurrenceStrategyProps = {
|
||||
kind: 'weekly';
|
||||
weekdays: WeekdaySet;
|
||||
};
|
||||
|
||||
export type EveryNWeeksRecurrenceStrategy = {
|
||||
export type EveryNWeeksRecurrenceStrategyProps = {
|
||||
kind: 'everyNWeeks';
|
||||
weekdays: WeekdaySet;
|
||||
intervalWeeks: number;
|
||||
};
|
||||
|
||||
export type MonthlyNthWeekdayRecurrenceStrategy = {
|
||||
export type MonthlyNthWeekdayRecurrenceStrategyProps = {
|
||||
kind: 'monthlyNthWeekday';
|
||||
monthlyPattern: MonthlyRecurrencePattern;
|
||||
};
|
||||
|
||||
export type RecurrenceStrategy =
|
||||
| WeeklyRecurrenceStrategy
|
||||
| EveryNWeeksRecurrenceStrategy
|
||||
| MonthlyNthWeekdayRecurrenceStrategy;
|
||||
export type RecurrenceStrategyProps =
|
||||
| WeeklyRecurrenceStrategyProps
|
||||
| EveryNWeeksRecurrenceStrategyProps
|
||||
| MonthlyNthWeekdayRecurrenceStrategyProps;
|
||||
|
||||
export class RecurrenceStrategy implements IValueObject<RecurrenceStrategyProps> {
|
||||
private constructor(private readonly strategy: RecurrenceStrategyProps) {}
|
||||
|
||||
get props(): RecurrenceStrategyProps {
|
||||
return this.strategy;
|
||||
}
|
||||
|
||||
export class RecurrenceStrategyFactory {
|
||||
static weekly(weekdays: WeekdaySet): RecurrenceStrategy {
|
||||
if (weekdays.getAll().length === 0) {
|
||||
throw new RacingDomainValidationError('weekdays are required for weekly recurrence');
|
||||
}
|
||||
|
||||
return {
|
||||
return new RecurrenceStrategy({
|
||||
kind: 'weekly',
|
||||
weekdays,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
static everyNWeeks(intervalWeeks: number, weekdays: WeekdaySet): RecurrenceStrategy {
|
||||
@@ -43,17 +50,36 @@ export class RecurrenceStrategyFactory {
|
||||
throw new RacingDomainValidationError('weekdays are required for everyNWeeks recurrence');
|
||||
}
|
||||
|
||||
return {
|
||||
return new RecurrenceStrategy({
|
||||
kind: 'everyNWeeks',
|
||||
weekdays,
|
||||
intervalWeeks,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
static monthlyNthWeekday(pattern: MonthlyRecurrencePattern): RecurrenceStrategy {
|
||||
return {
|
||||
return new RecurrenceStrategy({
|
||||
kind: 'monthlyNthWeekday',
|
||||
monthlyPattern: pattern,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
equals(other: IValueObject<RecurrenceStrategyProps>): boolean {
|
||||
const otherProps = other.props;
|
||||
if (this.strategy.kind !== otherProps.kind) {
|
||||
return false;
|
||||
}
|
||||
switch (this.strategy.kind) {
|
||||
case 'weekly':
|
||||
return this.strategy.weekdays.equals((otherProps as WeeklyRecurrenceStrategyProps).weekdays);
|
||||
case 'everyNWeeks':
|
||||
const everyN = otherProps as EveryNWeeksRecurrenceStrategyProps;
|
||||
return this.strategy.intervalWeeks === everyN.intervalWeeks && this.strategy.weekdays.equals(everyN.weekdays);
|
||||
case 'monthlyNthWeekday':
|
||||
const monthly = otherProps as MonthlyNthWeekdayRecurrenceStrategyProps;
|
||||
return this.strategy.monthlyPattern.equals(monthly.monthlyPattern);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user