56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { WeekdaySet } from './WeekdaySet';
|
|
import { MonthlyRecurrencePattern } from './MonthlyRecurrencePattern';
|
|
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
|
|
|
export type RecurrenceStrategyKind = 'weekly' | 'everyNWeeks' | 'monthlyNthWeekday';
|
|
|
|
export type WeeklyRecurrence = {
|
|
kind: 'weekly';
|
|
weekdays: WeekdaySet;
|
|
};
|
|
|
|
export type EveryNWeeksRecurrence = {
|
|
kind: 'everyNWeeks';
|
|
intervalWeeks: number;
|
|
weekdays: WeekdaySet;
|
|
};
|
|
|
|
export type MonthlyNthWeekdayRecurrence = {
|
|
kind: 'monthlyNthWeekday';
|
|
monthlyPattern: MonthlyRecurrencePattern;
|
|
};
|
|
|
|
export type RecurrenceStrategy =
|
|
| WeeklyRecurrence
|
|
| EveryNWeeksRecurrence
|
|
| MonthlyNthWeekdayRecurrence;
|
|
|
|
export class RecurrenceStrategyFactory {
|
|
static weekly(weekdays: WeekdaySet): RecurrenceStrategy {
|
|
return {
|
|
kind: 'weekly',
|
|
weekdays,
|
|
};
|
|
}
|
|
|
|
static everyNWeeks(intervalWeeks: number, weekdays: WeekdaySet): RecurrenceStrategy {
|
|
if (!Number.isInteger(intervalWeeks) || intervalWeeks < 1 || intervalWeeks > 12) {
|
|
throw new RacingDomainValidationError(
|
|
'everyNWeeks intervalWeeks must be an integer between 1 and 12',
|
|
);
|
|
}
|
|
|
|
return {
|
|
kind: 'everyNWeeks',
|
|
intervalWeeks,
|
|
weekdays,
|
|
};
|
|
}
|
|
|
|
static monthlyNthWeekday(monthlyPattern: MonthlyRecurrencePattern): RecurrenceStrategy {
|
|
return {
|
|
kind: 'monthlyNthWeekday',
|
|
monthlyPattern,
|
|
};
|
|
}
|
|
} |