wip
This commit is contained in:
59
packages/racing/domain/value-objects/RecurrenceStrategy.ts
Normal file
59
packages/racing/domain/value-objects/RecurrenceStrategy.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user