import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { CreateNotificationRequest, CreateTemplateRequest, NotificationTask, NotificationTaskDetail, NotificationTaskStatus, NotificationTemplate, PageResult, TemplateStatus, UpdateTemplateRequest } from '../types' type RawId = string | number type RawPage = Omit, 'page' | 'size' | 'total' | 'pages'> & { page: RawId; size: RawId; total: RawId; pages: RawId } type RawTemplate = Omit & { id: RawId } type RawTask = Omit & { id: RawId } type RawTaskDetail = Omit & { id: RawId; recipients: Array & { id: RawId }> } const notificationPath = '/platform/v1/notifications' const templatePath = '/platform/v1/notification-templates' const taskPath = '/platform/v1/notification-tasks' const id = (value: RawId) => String(value) const page = (value: RawPage): PageResult => ({ ...value, page: Number(value.page), size: Number(value.size), total: Number(value.total), pages: Number(value.pages) }) const template = (value: RawTemplate): NotificationTemplate => ({ ...value, id: id(value.id) }) const task = (value: RawTask): NotificationTask => ({ ...value, id: id(value.id) }) const taskDetail = (value: RawTaskDetail): NotificationTaskDetail => ({ ...value, id: id(value.id), recipients: value.recipients.map((recipient) => ({ ...recipient, id: id(recipient.id) })) }) export async function createNotification(payload: CreateNotificationRequest) { const response = await http.post, ApiEnvelope>(notificationPath, payload); return task(unwrapApiResponse(response)) } export async function listTemplates(query: { page: number; size: number; templateCode?: string; category?: string; title?: string; status?: TemplateStatus }) { const response = await http.get>, ApiEnvelope>>(templatePath, { params: query }); const data = unwrapApiResponse(response); return page({ ...data, records: data.records.map(template) }) } export async function createTemplate(payload: CreateTemplateRequest) { const response = await http.post, ApiEnvelope>(templatePath, payload); return template(unwrapApiResponse(response)) } export async function updateTemplate(templateId: string, payload: UpdateTemplateRequest) { const response = await http.put, ApiEnvelope>(`${templatePath}/${encodeURIComponent(templateId)}`, payload); return template(unwrapApiResponse(response)) } export async function changeTemplateStatus(templateId: string, targetStatus: Exclude, version: number) { const response = await http.put, ApiEnvelope>(`${templatePath}/${encodeURIComponent(templateId)}/status`, { targetStatus, version }); return template(unwrapApiResponse(response)) } export async function listTasks(query: { page: number; size: number; taskNo?: string; category?: string; title?: string; status?: NotificationTaskStatus }) { const response = await http.get>, ApiEnvelope>>(taskPath, { params: query }); const data = unwrapApiResponse(response); return page({ ...data, records: data.records.map(task) }) } export async function getTask(taskId: string) { const response = await http.get, ApiEnvelope>(`${taskPath}/${encodeURIComponent(taskId)}`); return taskDetail(unwrapApiResponse(response)) } export async function cancelTask(taskId: string) { const response = await http.put, ApiEnvelope>(`${taskPath}/${encodeURIComponent(taskId)}/cancel`); return task(unwrapApiResponse(response)) } export async function retryTask(taskId: string) { const response = await http.put, ApiEnvelope>(`${taskPath}/${encodeURIComponent(taskId)}/retry`); return task(unwrapApiResponse(response)) }