import { createRequestError } from './request-client.js' import { normalizeNormalDisableResponseStatus, normalizeOptionalNumericId } from './response-normalizers.js' const normalizeNotificationText = (value, field) => { if (value === undefined || value === null) return '' if (typeof value !== 'string') { throw createRequestError(`通知详情字段 ${field} 无效`, 'NOTIFICATION_DETAIL_RESPONSE_INVALID') } return value } const NOTIFICATION_TYPE_LABELS = Object.freeze({ join_apply: '加入申请', memo_reminder: '备忘提醒', family_feed: '家族圈互动', CEREMONY_INVITE: '礼仪邀约' }) const notificationTypes = new Set(Object.keys(NOTIFICATION_TYPE_LABELS)) const notificationTypeLabel = (noticeType) => NOTIFICATION_TYPE_LABELS[noticeType] || '通知' export const normalizeNotificationDetail = (value, expectedNotificationId = '') => { if (!value || typeof value !== 'object' || Array.isArray(value)) { throw createRequestError('通知详情响应格式无效', 'NOTIFICATION_DETAIL_RESPONSE_INVALID') } const notificationId = normalizeOptionalNumericId(value.notificationId, '通知标识', 'NOTIFICATION_DETAIL_RESPONSE_INVALID') if (!notificationId || (expectedNotificationId && notificationId !== expectedNotificationId)) { throw createRequestError('通知详情响应标识不匹配', 'NOTIFICATION_DETAIL_RESPONSE_INVALID') } const noticeType = normalizeNotificationText(value.noticeType, 'noticeType') if (!notificationTypes.has(noticeType)) { throw createRequestError('通知详情字段 noticeType 无效', 'NOTIFICATION_DETAIL_RESPONSE_INVALID') } const noticeTypeLabel = normalizeNotificationText(value.noticeTypeLabel, 'noticeTypeLabel').trim() return { notificationId, genealogyId: normalizeOptionalNumericId(value.genealogyId, '通知家谱标识', 'NOTIFICATION_DETAIL_RESPONSE_INVALID'), genealogyNo: normalizeNotificationText(value.genealogyNo, 'genealogyNo'), genealogyName: normalizeNotificationText(value.genealogyName, 'genealogyName'), senderUserId: normalizeOptionalNumericId(value.senderUserId, '通知发送人标识', 'NOTIFICATION_DETAIL_RESPONSE_INVALID'), senderNickName: normalizeNotificationText(value.senderNickName, 'senderNickName'), noticeType, noticeTypeLabel: noticeTypeLabel || notificationTypeLabel(noticeType), noticeTitle: normalizeNotificationText(value.noticeTitle, 'noticeTitle'), noticeContent: normalizeNotificationText(value.noticeContent, 'noticeContent'), bizType: normalizeNotificationText(value.bizType, 'bizType'), bizId: normalizeOptionalNumericId(value.bizId, '通知关联业务标识', 'NOTIFICATION_DETAIL_RESPONSE_INVALID'), bizSummary: normalizeNotificationText(value.bizSummary, 'bizSummary'), publishTime: normalizeNotificationText(value.publishTime, 'publishTime'), readStatus: (() => { const readStatus = normalizeNotificationText(value.readStatus, 'readStatus') if (!['0', '1'].includes(readStatus)) { throw createRequestError('通知详情字段 readStatus 无效', 'NOTIFICATION_DETAIL_RESPONSE_INVALID') } return readStatus })(), readTime: normalizeNotificationText(value.readTime, 'readTime'), status: normalizeNormalDisableResponseStatus( value.status, '通知状态', 'NOTIFICATION_DETAIL_RESPONSE_INVALID' ), remark: normalizeNotificationText(value.remark, 'remark') } } export const normalizeNotifications = (value) => { if (!Array.isArray(value)) { throw createRequestError('通知列表响应不是列表', 'NOTIFICATION_LIST_RESPONSE_INVALID') } const notifications = value.map((notification) => normalizeNotificationDetail(notification)) if (new Set(notifications.map((notification) => notification.notificationId)).size !== notifications.length) { throw createRequestError('通知列表响应包含重复标识', 'NOTIFICATION_LIST_RESPONSE_INVALID') } return notifications }