|
| 1 | +import { getOrdinalNumber } from '@shared/utils'; |
| 2 | + |
| 3 | +import { CRON_VALUE_DEFAULT, DAYS_OF_WEEK } from './constants'; |
| 4 | +import { CronSegmentKey, CronSegmentValue } from './types'; |
| 5 | + |
| 6 | +/** Class for convenient handling cron settings */ |
| 7 | +export class CronService { |
| 8 | + private initialValueLength = 5; |
| 9 | + |
| 10 | + private value: Map<CronSegmentKey, CronSegmentValue>; |
| 11 | + |
| 12 | + constructor(initialValue?: string) { |
| 13 | + this.value = this.transformInitialValueToMap(initialValue); |
| 14 | + } |
| 15 | + |
| 16 | + private transformInitialValueToMap(initialValue?: string) { |
| 17 | + const splittedValue = initialValue?.split(' '); |
| 18 | + if (splittedValue?.length !== this.initialValueLength) { |
| 19 | + return CRON_VALUE_DEFAULT; |
| 20 | + } |
| 21 | + |
| 22 | + const cronValue = splittedValue.map((segment) => { |
| 23 | + const parsedValue = parseInt(segment); |
| 24 | + if (Number.isInteger(parsedValue)) { |
| 25 | + return parsedValue; |
| 26 | + } |
| 27 | + return null; |
| 28 | + }); |
| 29 | + |
| 30 | + return new Map<CronSegmentKey, CronSegmentValue>([ |
| 31 | + ['minute', cronValue[0]], |
| 32 | + ['hour', cronValue[1]], |
| 33 | + ['date', cronValue[2]], |
| 34 | + ['day', cronValue[4]], |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + getMinute(): number { |
| 39 | + return this.value.get('minute')!; |
| 40 | + } |
| 41 | + |
| 42 | + getHour(): number { |
| 43 | + return this.value.get('hour')!; |
| 44 | + } |
| 45 | + |
| 46 | + getMonthDay(): CronSegmentValue { |
| 47 | + return this.value.get('date') ?? null; |
| 48 | + } |
| 49 | + |
| 50 | + getWeekDay(): CronSegmentValue { |
| 51 | + return this.value.get('day') ?? null; |
| 52 | + } |
| 53 | + |
| 54 | + getTime() { |
| 55 | + return `${this.getHour()}:${this.getMinute()}`; |
| 56 | + } |
| 57 | + |
| 58 | + setMinute(value: number) { |
| 59 | + if (value < 0 || value > 59) { |
| 60 | + this.value.set('minute', new Date().getMinutes()); |
| 61 | + } else { |
| 62 | + this.value.set('minute', value); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + setHour(value: number) { |
| 67 | + if (value < 0 || value > 23) { |
| 68 | + this.value.set('hour', new Date().getHours()); |
| 69 | + } else { |
| 70 | + this.value.set('hour', value); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + setMonthDay(value: CronSegmentValue) { |
| 75 | + if (value === null) { |
| 76 | + this.value.set('date', null); |
| 77 | + return; |
| 78 | + } |
| 79 | + if (value < 1 || value > 31) { |
| 80 | + this.value.set('date', new Date().getDate()); |
| 81 | + } else { |
| 82 | + this.value.set('date', value); |
| 83 | + } |
| 84 | + this.setWeekDay(null); |
| 85 | + } |
| 86 | + |
| 87 | + setWeekDay(value: CronSegmentValue) { |
| 88 | + if (value === null) { |
| 89 | + this.value.set('day', null); |
| 90 | + return; |
| 91 | + } |
| 92 | + if (value < 0 || value > 6) { |
| 93 | + this.value.set('day', new Date().getDay()); |
| 94 | + } else { |
| 95 | + this.value.set('day', value); |
| 96 | + } |
| 97 | + this.setMonthDay(null); |
| 98 | + } |
| 99 | + |
| 100 | + toString() { |
| 101 | + const minute = this.getMinute(); |
| 102 | + const hour = this.getHour(); |
| 103 | + const date = this.getMonthDay() ?? '*'; |
| 104 | + const day = this.getWeekDay() ?? '*'; |
| 105 | + return `${minute} ${hour} ${date} * ${day}`; |
| 106 | + } |
| 107 | + |
| 108 | + getSchedule() { |
| 109 | + const time = this.getTime(); |
| 110 | + const day = this.getWeekDay(); |
| 111 | + const date = this.getMonthDay(); |
| 112 | + |
| 113 | + let schedule = 'Every '; |
| 114 | + |
| 115 | + if (day === null && !date) { |
| 116 | + schedule += 'day '; |
| 117 | + } else if (day !== null && !date) { |
| 118 | + schedule += 'week '; |
| 119 | + } else { |
| 120 | + schedule += 'month '; |
| 121 | + } |
| 122 | + |
| 123 | + if (day !== null) { |
| 124 | + schedule += `on ${DAYS_OF_WEEK[day]} `; |
| 125 | + } else if (date) { |
| 126 | + schedule += `${getOrdinalNumber(date)} `; |
| 127 | + } |
| 128 | + |
| 129 | + schedule += `at ${time}`; |
| 130 | + |
| 131 | + return schedule; |
| 132 | + } |
| 133 | +} |
0 commit comments