From d728b49f6789752093a9bbc0fb29062c860cfe8c Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Thu, 12 Mar 2026 14:31:44 +0100 Subject: [PATCH] feat: add report schedules API client module --- lib/api/report-schedules.ts | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/api/report-schedules.ts diff --git a/lib/api/report-schedules.ts b/lib/api/report-schedules.ts new file mode 100644 index 0000000..44b5e92 --- /dev/null +++ b/lib/api/report-schedules.ts @@ -0,0 +1,73 @@ +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' + 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 +} + +export interface UpdateReportScheduleRequest { + channel?: string + channel_config?: EmailConfig | WebhookConfig + frequency?: string + timezone?: string + report_type?: string + enabled?: boolean +} + +export async function listReportSchedules(siteId: string): Promise { + const res = await apiRequest<{ schedules: ReportSchedule[] }>(`/sites/${siteId}/report-schedules`) + return res?.schedules ?? [] +} + +export async function createReportSchedule(siteId: string, data: CreateReportScheduleRequest): Promise { + return apiRequest(`/sites/${siteId}/report-schedules`, { + method: 'POST', + body: JSON.stringify(data), + }) +} + +export async function updateReportSchedule(siteId: string, scheduleId: string, data: UpdateReportScheduleRequest): Promise { + return apiRequest(`/sites/${siteId}/report-schedules/${scheduleId}`, { + method: 'PUT', + body: JSON.stringify(data), + }) +} + +export async function deleteReportSchedule(siteId: string, scheduleId: string): Promise { + await apiRequest(`/sites/${siteId}/report-schedules/${scheduleId}`, { + method: 'DELETE', + }) +} + +export async function testReportSchedule(siteId: string, scheduleId: string): Promise { + await apiRequest(`/sites/${siteId}/report-schedules/${scheduleId}/test`, { + method: 'POST', + }) +}