This commit is contained in:
wujinliang
2026-08-27 21:48:15 +08:00
parent 550d8add97
commit b47323edf3
53 changed files with 624 additions and 82 deletions
@@ -4,9 +4,14 @@ import {
cancelTask,
changeTemplateStatus,
createNotification,
getMyNotification,
getMyUnreadNotificationCount,
getTask,
listMyNotifications,
listTasks,
listTemplates,
markAllMyNotificationsRead,
markMyNotificationRead,
retryTask,
updateTemplate,
} from './notificationCenter'
@@ -62,4 +67,24 @@ describe('notificationCenter API', () => {
expect(result.recipients[0]?.id).toBe('21')
expect(result.lastError).toBe('gateway timeout')
})
it('uses platform inbox paths and normalizes inbox ids', async () => {
const inbox = { id: 14, category: 'ORDER', title: 'order notice', summary: 'summary', status: 'UNREAD' as const, sentAt: '2026-08-27T10:00:00', readAt: null }
const detail = { ...inbox, body: 'body', sourceType: null, sourceId: null }
vi.mocked(http.get)
.mockResolvedValueOnce(envelope({ records: [inbox], page: 1, size: 20, total: 1, pages: 1 }))
.mockResolvedValueOnce(envelope(detail))
.mockResolvedValueOnce(envelope({ unreadCount: 1 }))
vi.mocked(http.put)
.mockResolvedValueOnce(envelope(detail))
.mockResolvedValueOnce(envelope({ updatedCount: 1 }))
const result = await listMyNotifications({ page: 1, size: 20, status: 'UNREAD' })
await getMyNotification('14')
await markMyNotificationRead('14')
await markAllMyNotificationsRead()
expect(await getMyUnreadNotificationCount()).toBe(1)
expect(result.records[0]?.id).toBe('14')
expect(http.get).toHaveBeenNthCalledWith(1, '/platform/v1/notifications', { params: { page: 1, size: 20, status: 'UNREAD' } })
expect(http.put).toHaveBeenNthCalledWith(1, '/platform/v1/notifications/14/read')
expect(http.put).toHaveBeenNthCalledWith(2, '/platform/v1/notifications/read-all')
})
})
@@ -1,11 +1,13 @@
import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http'
import type { CreateNotificationRequest, CreateTemplateRequest, NotificationTask, NotificationTaskDetail, NotificationTaskStatus, NotificationTemplate, PageResult, TemplateStatus, UpdateTemplateRequest } from '../types'
import type { CreateNotificationRequest, CreateTemplateRequest, NotificationInboxDetail, NotificationInboxItem, NotificationInboxStatus, NotificationTask, NotificationTaskDetail, NotificationTaskStatus, NotificationTemplate, PageResult, TemplateStatus, UpdateTemplateRequest } from '../types'
type RawId = string | number
type RawPage<T> = Omit<PageResult<T>, 'page' | 'size' | 'total' | 'pages'> & { page: RawId; size: RawId; total: RawId; pages: RawId }
type RawTemplate = Omit<NotificationTemplate, 'id'> & { id: RawId }
type RawTask = Omit<NotificationTask, 'id'> & { id: RawId }
type RawTaskDetail = Omit<NotificationTaskDetail, 'id' | 'recipients'> & { id: RawId; recipients: Array<Omit<NotificationTaskDetail['recipients'][number], 'id'> & { id: RawId }> }
type RawInbox = Omit<NotificationInboxItem, 'id'> & { id: RawId }
type RawInboxDetail = Omit<NotificationInboxDetail, 'id'> & { id: RawId }
const notificationPath = '/platform/v1/notifications'
const templatePath = '/platform/v1/notification-templates'
@@ -15,6 +17,8 @@ const page = <T>(value: RawPage<T>): PageResult<T> => ({ ...value, page: Number(
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) })) })
const inbox = (value: RawInbox): NotificationInboxItem => ({ ...value, id: id(value.id), summary: value.summary ?? '', readAt: value.readAt ?? null })
const inboxDetail = (value: RawInboxDetail): NotificationInboxDetail => ({ ...value, id: id(value.id), summary: value.summary ?? '', readAt: value.readAt ?? null, body: value.body ?? '', sourceType: value.sourceType ?? null, sourceId: value.sourceId ?? null })
export async function createNotification(payload: CreateNotificationRequest) { const response = await http.post<ApiEnvelope<RawTask>, ApiEnvelope<RawTask>>(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<RawPage<RawTemplate>>, ApiEnvelope<RawPage<RawTemplate>>>(templatePath, { params: query }); const data = unwrapApiResponse(response); return page({ ...data, records: data.records.map(template) }) }
@@ -25,3 +29,26 @@ export async function listTasks(query: { page: number; size: number; taskNo?: st
export async function getTask(taskId: string) { const response = await http.get<ApiEnvelope<RawTaskDetail>, ApiEnvelope<RawTaskDetail>>(`${taskPath}/${encodeURIComponent(taskId)}`); return taskDetail(unwrapApiResponse(response)) }
export async function cancelTask(taskId: string) { const response = await http.put<ApiEnvelope<RawTask>, ApiEnvelope<RawTask>>(`${taskPath}/${encodeURIComponent(taskId)}/cancel`); return task(unwrapApiResponse(response)) }
export async function retryTask(taskId: string) { const response = await http.put<ApiEnvelope<RawTask>, ApiEnvelope<RawTask>>(`${taskPath}/${encodeURIComponent(taskId)}/retry`); return task(unwrapApiResponse(response)) }
const inboxPath = '/platform/v1/notifications'
export async function listMyNotifications(query: { page: number; size: number; status?: NotificationInboxStatus; category?: string }) {
const response = await http.get<ApiEnvelope<RawPage<RawInbox>>, ApiEnvelope<RawPage<RawInbox>>>(inboxPath, { params: query })
const data = unwrapApiResponse(response)
return page({ ...data, records: data.records.map(inbox) })
}
export async function getMyNotification(notificationId: string) {
const response = await http.get<ApiEnvelope<RawInboxDetail>, ApiEnvelope<RawInboxDetail>>(`${inboxPath}/${encodeURIComponent(notificationId)}`)
return inboxDetail(unwrapApiResponse(response))
}
export async function markMyNotificationRead(notificationId: string) {
const response = await http.put<ApiEnvelope<RawInboxDetail>, ApiEnvelope<RawInboxDetail>>(`${inboxPath}/${encodeURIComponent(notificationId)}/read`)
return inboxDetail(unwrapApiResponse(response))
}
export async function markAllMyNotificationsRead() {
const response = await http.put<ApiEnvelope<{ updatedCount: number }>, ApiEnvelope<{ updatedCount: number }>>(`${inboxPath}/read-all`)
return unwrapApiResponse(response)
}
export async function getMyUnreadNotificationCount() {
const response = await http.get<ApiEnvelope<{ unreadCount: number }>, ApiEnvelope<{ unreadCount: number }>>(`${inboxPath}/unread-count`)
return unwrapApiResponse(response).unreadCount
}