59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { WeekdaySet } from './WeekdaySet';
|
|
import { MonthlyRecurrencePattern } from './MonthlyRecurrencePattern';
|
|
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
|
|
|
export type WeeklyRecurrenceStrategy = {
|
|
kind: 'weekly';
|
|
weekdays: WeekdaySet;
|
|
};
|
|
|
|
export type EveryNWeeksRecurrenceStrategy = {
|
|
kind: 'everyNWeeks';
|
|
weekdays: WeekdaySet;
|
|
intervalWeeks: number;
|
|
};
|
|
|
|
export type MonthlyNthWeekdayRecurrenceStrategy = {
|
|
kind: 'monthlyNthWeekday';
|
|
monthlyPattern: MonthlyRecurrencePattern;
|
|
};
|
|
|
|
export type RecurrenceStrategy =
|
|
| WeeklyRecurrenceStrategy
|
|
| EveryNWeeksRecurrenceStrategy
|
|
| MonthlyNthWeekdayRecurrenceStrategy;
|
|
|
|
export class RecurrenceStrategyFactory {
|
|
static weekly(weekdays: WeekdaySet): RecurrenceStrategy {
|
|
if (weekdays.getAll().length === 0) {
|
|
throw new RacingDomainValidationError('weekdays are required for weekly recurrence');
|
|
}
|
|
|
|
return {
|
|
kind: 'weekly',
|
|
weekdays,
|
|
};
|
|
}
|
|
|
|
static everyNWeeks(intervalWeeks: number, weekdays: WeekdaySet): RecurrenceStrategy {
|
|
if (!Number.isInteger(intervalWeeks) || intervalWeeks <= 0) {
|
|
throw new RacingDomainValidationError('intervalWeeks must be a positive integer');
|
|
}
|
|
if (weekdays.getAll().length === 0) {
|
|
throw new RacingDomainValidationError('weekdays are required for everyNWeeks recurrence');
|
|
}
|
|
|
|
return {
|
|
kind: 'everyNWeeks',
|
|
weekdays,
|
|
intervalWeeks,
|
|
};
|
|
}
|
|
|
|
static monthlyNthWeekday(pattern: MonthlyRecurrencePattern): RecurrenceStrategy {
|
|
return {
|
|
kind: 'monthlyNthWeekday',
|
|
monthlyPattern: pattern,
|
|
};
|
|
}
|
|
} |