35 lines
937 B
TypeScript
35 lines
937 B
TypeScript
|
|
import { CronExpressionParser } from 'cron-parser'
|
||
|
|
import { tryCatch, int, HOUR } from '@welshman/lib'
|
||
|
|
|
||
|
|
export type Subscription = {
|
||
|
|
id: string
|
||
|
|
key: string
|
||
|
|
pubkey: string
|
||
|
|
email: string
|
||
|
|
frequency: string
|
||
|
|
created_at: number
|
||
|
|
confirmed_at?: number
|
||
|
|
unsubscribed_at?: number
|
||
|
|
last_digest_at?: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export const getSubscriptionError = (sub: Subscription) => {
|
||
|
|
if (!sub.email?.includes('@')) {
|
||
|
|
return 'Please provide a valid email address'
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!['daily', 'weekly'].includes(sub.frequency)) {
|
||
|
|
return 'Frequency must be "daily" or "weekly"'
|
||
|
|
}
|
||
|
|
|
||
|
|
// Daily: fire at 17:00 UTC. Weekly: fire Monday at 17:00 UTC.
|
||
|
|
// Validation: just ensure frequency is valid, cron is generated internally.
|
||
|
|
}
|
||
|
|
|
||
|
|
export const getCronExpression = (frequency: string, hour = 17, minute = 0) => {
|
||
|
|
if (frequency === 'daily') {
|
||
|
|
return `0 ${minute} ${hour} * * *`
|
||
|
|
}
|
||
|
|
// Weekly on Monday
|
||
|
|
return `0 ${minute} ${hour} * * 1`
|
||
|
|
}
|