Files
pulse/lib/api/report-schedules.ts
Usman Baig 6a1698b794 feat: add Notifications section to settings with Reports and Alerts
- Adds purpose field to report schedule API client
- Adds useAlertSchedules SWR hook
- Reorganizes settings: Reports tab becomes Notifications tab
- Groups existing Reports and new Alerts subsections
- Alert channels reuse report delivery infrastructure (email, Slack, Discord, webhooks)
2026-03-22 16:57:04 +01:00

89 lines
2.6 KiB
TypeScript

import apiRequest from './client'
export interface ReportSchedule {
id: string
site_id: string
organization_id: string
channel: 'email' | 'slack' | 'discord' | 'webhook'
channel_config: EmailConfig | WebhookConfig
frequency: 'daily' | 'weekly' | 'monthly'
timezone: string
enabled: boolean
report_type: 'summary' | 'pages' | 'sources' | 'goals'
purpose: 'report' | 'alert'
send_hour: number
send_day: number | null
next_send_at: string | null
last_sent_at: string | null
last_error: string | null
created_at: string
updated_at: string
}
export interface EmailConfig {
recipients: string[]
}
export interface WebhookConfig {
url: string
}
export interface CreateReportScheduleRequest {
channel: string
channel_config: EmailConfig | WebhookConfig
frequency: string
timezone?: string
report_type?: string
purpose?: 'report' | 'alert'
send_hour?: number
send_day?: number
}
export interface UpdateReportScheduleRequest {
channel?: string
channel_config?: EmailConfig | WebhookConfig
frequency?: string
timezone?: string
report_type?: string
purpose?: 'report' | 'alert'
enabled?: boolean
send_hour?: number
send_day?: number
}
export async function listReportSchedules(siteId: string): Promise<ReportSchedule[]> {
const res = await apiRequest<{ report_schedules: ReportSchedule[] }>(`/sites/${siteId}/report-schedules`)
return res?.report_schedules ?? []
}
export async function createReportSchedule(siteId: string, data: CreateReportScheduleRequest): Promise<ReportSchedule> {
return apiRequest<ReportSchedule>(`/sites/${siteId}/report-schedules`, {
method: 'POST',
body: JSON.stringify(data),
})
}
export async function updateReportSchedule(siteId: string, scheduleId: string, data: UpdateReportScheduleRequest): Promise<ReportSchedule> {
return apiRequest<ReportSchedule>(`/sites/${siteId}/report-schedules/${scheduleId}`, {
method: 'PUT',
body: JSON.stringify(data),
})
}
export async function deleteReportSchedule(siteId: string, scheduleId: string): Promise<void> {
await apiRequest(`/sites/${siteId}/report-schedules/${scheduleId}`, {
method: 'DELETE',
})
}
export async function listAlertSchedules(siteId: string): Promise<ReportSchedule[]> {
const res = await apiRequest<{ report_schedules: ReportSchedule[] }>(`/sites/${siteId}/report-schedules?purpose=alert`)
return res?.report_schedules ?? []
}
export async function testReportSchedule(siteId: string, scheduleId: string): Promise<void> {
await apiRequest(`/sites/${siteId}/report-schedules/${scheduleId}/test`, {
method: 'POST',
})
}