88 lines
2.2 KiB
Plaintext
88 lines
2.2 KiB
Plaintext
import supa from '@/components/supadb/aksupainstance.uts'
|
||
import { rpcOrValue } from './rpc.uts'
|
||
|
||
export type CustomReportListItem = {
|
||
id: string
|
||
title: string
|
||
description: string
|
||
period: string
|
||
updated_at: string
|
||
}
|
||
|
||
export type CreateCustomReportParams = {
|
||
title: string
|
||
description: string
|
||
period: string
|
||
metrics: Array<string>
|
||
chartType: string
|
||
}
|
||
|
||
export type UpdateCustomReportParams = {
|
||
reportId: string
|
||
title: string
|
||
description: string | null
|
||
period: string | null
|
||
}
|
||
|
||
export async function listCustomReports(ownerUserId: string): Promise<Array<CustomReportListItem>> {
|
||
const res: any = await supa
|
||
.from('analytics_reports')
|
||
.select('id, title, description, period, updated_at')
|
||
.eq('type', 'custom')
|
||
.eq('owner_user_id', ownerUserId)
|
||
.order('updated_at', { ascending: false } as any)
|
||
|
||
if (res?.error != null) {
|
||
throw res.error
|
||
}
|
||
|
||
const rows: Array<any> = Array.isArray(res.data) ? (res.data as Array<any>) : []
|
||
const list: Array<CustomReportListItem> = []
|
||
for (let i = 0; i < rows.length; i++) {
|
||
const r = rows[i]
|
||
list.push({
|
||
id: `${r.id}`,
|
||
title: `${r.title}`,
|
||
description: `${r.description || ''}`,
|
||
period: `${r.period || ''}`,
|
||
updated_at: `${r.updated_at || ''}`
|
||
})
|
||
}
|
||
return list
|
||
}
|
||
|
||
export async function createCustomReport(params: CreateCustomReportParams): Promise<string> {
|
||
const data = await rpcOrValue('rpc_create_custom_report', {
|
||
p_title: params.title,
|
||
p_description: params.description || '',
|
||
p_period: params.period,
|
||
p_metrics: params.metrics,
|
||
p_chart_type: params.chartType || 'line'
|
||
} as UTSJSONObject)
|
||
|
||
if (data == null) {
|
||
throw new Error('保存失败:未返回报表ID')
|
||
}
|
||
|
||
return `${data}`
|
||
}
|
||
|
||
export async function updateCustomReport(params: UpdateCustomReportParams): Promise<boolean> {
|
||
await rpcOrValue('rpc_update_custom_report', {
|
||
p_report_id: params.reportId,
|
||
p_title: params.title,
|
||
p_description: params.description,
|
||
p_period: params.period
|
||
} as UTSJSONObject)
|
||
|
||
return true
|
||
}
|
||
|
||
export async function deleteCustomReport(reportId: string): Promise<boolean> {
|
||
await rpcOrValue('rpc_delete_custom_report', {
|
||
p_report_id: reportId
|
||
} as UTSJSONObject)
|
||
|
||
return true
|
||
}
|