阶段六:完成生成契约迁移并永久关闭旁路
This commit is contained in:
1 parent
5675f60e52
commit
76f4e2e87d
117 files changed
+5833
-2204
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import type { MutationIDResponse,NotificationBinding,NotificationChannel,NotificationChannelResponse,NotificationEventType,OperationResponse,SystemSettings } from '../../../shared/api-contract/notifications';
|
||||
import type { MutationIDResponse,NotificationBinding,NotificationChannel,NotificationChannelResponse,NotificationEventType,OperationResponse,SystemSettings } from './models';
|
||||
import { type RequestControlOptions } from '../../../shared/http/client';
|
||||
import { contractClient, runContractRequest } from '../../../shared/api-contract/client';
|
||||
export type * from '../../../shared/api-contract/notifications';
|
||||
export type * from './models';
|
||||
|
||||
/** 通知渠道写入时使用的具名请求 DTO。 */
|
||||
export interface NotificationChannelRequest {
|
||||
@@ -50,22 +50,24 @@ const toNotificationChannel = (channel: NotificationChannelResponse): Notificati
|
||||
/** 获取全部通知渠道并转换为编辑器模型。 */
|
||||
export const getNotificationChannels = async (options?: RequestControlOptions): Promise<{ /** 操作是否完成。 */ success: boolean; /** 渠道列表。 */ data: NotificationChannel[] }> => {
|
||||
// response 是渠道列表的原始传输响应。
|
||||
const response = await runContractRequest(/* signal 控制通知渠道列表读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/channels', { signal }), options) as unknown as NotificationChannelResponse[] | { /** channels 是后端包装的渠道列表。 */ data?: NotificationChannelResponse[]; /** channels 是兼容字段。 */ channels?: NotificationChannelResponse[] };
|
||||
// channels 是兼容直接数组与包装数组后的原始渠道列表。
|
||||
const channels = Array.isArray(response) ? response : response.data || response.channels || [];
|
||||
return { success: true, data: channels.map(toNotificationChannel) };
|
||||
const response = await runContractRequest(/* signal 控制通知渠道列表读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/channels', { signal }), options);
|
||||
return { success: true, data: response.map(toNotificationChannel) };
|
||||
};
|
||||
|
||||
/** 创建通知渠道,并在传输边界序列化配置与事件。 */
|
||||
export const createNotificationChannel = async (data: Required<Pick<NotificationChannelRequest, 'name' | 'type' | 'config'>> & NotificationChannelRequest, options?: RequestControlOptions): Promise<MutationIDResponse> => runContractRequest(/* signal 控制通知渠道创建请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/channels', { body: { ...data, config: JSON.stringify(data.config), event_types: stringifyNotificationEventTypes(data.event_types) } as never, signal }), options) as unknown as Promise<MutationIDResponse>;
|
||||
export const createNotificationChannel = async (data: Required<Pick<NotificationChannelRequest, 'name' | 'type' | 'config'>> & NotificationChannelRequest, options?: RequestControlOptions): Promise<MutationIDResponse> => runContractRequest(/* signal 控制通知渠道创建请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/channels', { body: { name: data.name, type: data.type, config: JSON.stringify(data.config), event_types: stringifyNotificationEventTypes(data.event_types), enabled: data.enabled }, signal }), options);
|
||||
|
||||
/** 更新通知渠道的可编辑字段。 */
|
||||
export const updateNotificationChannel = async (channelID: string, data: NotificationChannelRequest, options?: RequestControlOptions): Promise<OperationResponse> => {
|
||||
// payload 是已完成配置和事件序列化的更新请求。
|
||||
const payload: Record<string, unknown> = { ...data };
|
||||
if ('config' in data) payload.config = JSON.stringify(data.config);
|
||||
if ('event_types' in data) payload.event_types = stringifyNotificationEventTypes(data.event_types);
|
||||
return runContractRequest(/* signal 控制通知渠道更新请求的取消和超时。 */ signal => contractClient.PUT('/api/v1/notifications/channels/{channel_id}', { params: { path: { channel_id: channelID } }, body: payload as never, signal }), options) as unknown as Promise<OperationResponse>;
|
||||
const payload = {
|
||||
name: data.name,
|
||||
type: data.type,
|
||||
config: data.config === undefined ? undefined : JSON.stringify(data.config),
|
||||
event_types: data.event_types === undefined ? undefined : stringifyNotificationEventTypes(data.event_types),
|
||||
enabled: data.enabled,
|
||||
};
|
||||
return runContractRequest(/* signal 控制通知渠道更新请求的取消和超时。 */ signal => contractClient.PUT('/api/v1/notifications/channels/{channel_id}', { params: { path: { channel_id: channelID } }, body: payload, signal }), options);
|
||||
};
|
||||
|
||||
/** 删除指定通知渠道。 */
|
||||
@@ -74,7 +76,7 @@ export const deleteNotificationChannel = async (channelID: string, options?: Req
|
||||
/** 获取并展平按账号分组存储的消息通知绑定。 */
|
||||
export const getMessageNotifications = async (): Promise<{ /** 操作是否完成。 */ success: boolean; /** 展平后的绑定列表。 */ data: NotificationBinding[] }> => {
|
||||
// response 是按账号 ID 分组的通知绑定响应。
|
||||
const response = await runContractRequest(/* signal 控制消息通知列表读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/messages', { signal })) as unknown as Record<string, NotificationBinding[]>;
|
||||
const response = await runContractRequest(/* signal 控制消息通知列表读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/messages', { signal }));
|
||||
// bindings 是供页面直接渲染的扁平绑定集合。
|
||||
const bindings: NotificationBinding[] = [];
|
||||
for (const /* accountID、channelBindings 是当前账号及其原始渠道绑定列表。 */ [accountID, channelBindings] of Object.entries(response || {})) {
|
||||
@@ -85,7 +87,7 @@ export const getMessageNotifications = async (): Promise<{ /** 操作是否完
|
||||
};
|
||||
|
||||
/** 设置单个账号与渠道之间的启用状态。 */
|
||||
export const setMessageNotification = async (accountID: string, channelID: number, enabled: boolean): Promise<OperationResponse> => runContractRequest(/* signal 控制单账号通知开关更新请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, body: { channel_id: channelID, enabled } as never, signal }));
|
||||
export const setMessageNotification = async (accountID: string, channelID: number, enabled: boolean): Promise<OperationResponse> => runContractRequest(/* signal 控制单账号通知开关更新请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, body: { channel_id: channelID, enabled }, signal }));
|
||||
|
||||
/** 删除单个消息通知绑定。 */
|
||||
export const deleteMessageNotification = async (notificationID: string): Promise<OperationResponse> => runContractRequest(/* signal 控制单条通知删除请求的取消和超时。 */ signal => contractClient.DELETE('/api/v1/notifications/messages/{notification_id}', { params: { path: { notification_id: notificationID } }, signal }));
|
||||
@@ -95,16 +97,16 @@ export const deleteAccountNotifications = async (accountID: string): Promise<Ope
|
||||
|
||||
/** 读取指定账号已绑定的通知渠道主键。 */
|
||||
export const getAccountBindings = async (accountID: string, options?: RequestControlOptions): Promise<number[]> => {
|
||||
// response 是账号渠道绑定的具名响应。
|
||||
const response = await runContractRequest(/* signal 控制账号通知绑定读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, signal }), options) as unknown as { /** channelIDs 是绑定渠道主键集合。 */ channel_ids?: number[]; /** data 是兼容包装。 */ data?: { /** channelIDs 是绑定渠道主键集合。 */ channel_ids?: number[] } };
|
||||
return Array.isArray(response.channel_ids) ? response.channel_ids : Array.isArray(response.data?.channel_ids) ? response.data.channel_ids : [];
|
||||
// response 是由 OpenAPI 约束的账号渠道绑定响应;未绑定时后端兼容返回 null。
|
||||
const response = await runContractRequest(/* signal 控制账号通知绑定读取的取消和超时。 */ signal => contractClient.GET('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, signal }), options);
|
||||
return response.channel_ids ?? [];
|
||||
};
|
||||
|
||||
/** 覆盖保存指定账号的通知渠道绑定。 */
|
||||
export const setAccountBindings = async (accountID: string, channelIDs: number[]): Promise<OperationResponse> => runContractRequest(/* signal 控制账号通知绑定覆盖请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, body: { channel_ids: channelIDs } as never, signal }));
|
||||
export const setAccountBindings = async (accountID: string, channelIDs: number[]): Promise<OperationResponse> => runContractRequest(/* signal 控制账号通知绑定覆盖请求的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/accounts/{cid}/bindings', { params: { path: { cid: accountID } }, body: { channel_ids: channelIDs }, signal }));
|
||||
|
||||
/** 向单一通知渠道发送测试投递。 */
|
||||
export const testNotificationChannel = async (channelID: string, options?: RequestControlOptions): Promise<OperationResponse> => runContractRequest(/* signal 控制通知渠道测试投递的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/channels/{channel_id}/test', { params: { path: { channel_id: channelID } }, body: {} as never, signal }), options);
|
||||
export const testNotificationChannel = async (channelID: string, options?: RequestControlOptions): Promise<OperationResponse> => runContractRequest(/* signal 控制通知渠道测试投递的取消和超时。 */ signal => contractClient.POST('/api/v1/notifications/channels/{channel_id}/test', { params: { path: { channel_id: channelID } }, signal }), options);
|
||||
|
||||
/** 将系统配置响应转换为通知 SMTP 表单使用的状态模型。 */
|
||||
const normalizeSystemSettings = (settings: Record<string, unknown>): SystemSettings => {
|
||||
|
||||
@@ -71,21 +71,21 @@ export const NotificationSmtpSettings: React.FC<NotificationSmtpSettingsProps> =
|
||||
<div className="p-2 rounded-xl bg-blue-50 text-blue-600"><Mail className="w-5 h-5" /></div>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">SMTP 服务器</label><input data-field="smtp_server" type="text" value={smtp.smtp_server || ''} onChange={handleTextChange} placeholder="smtp.qq.com" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">SMTP 端口</label><input type="number" value={smtp.smtp_port || 587} onChange={handlePortChange} placeholder="587" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">SMTP 服务器</label><input data-field="smtp_server" type="text" value={typeof smtp.smtp_server === 'string' ? smtp.smtp_server : ''} onChange={handleTextChange} placeholder="smtp.qq.com" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">SMTP 端口</label><input type="number" value={typeof smtp.smtp_port === 'number' ? smtp.smtp_port : 587} onChange={handlePortChange} placeholder="587" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
</div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件邮箱</label><input data-field="smtp_user" type="email" value={smtp.smtp_user || ''} onChange={handleTextChange} placeholder="[email protected]" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件邮箱</label><input data-field="smtp_user" type="email" value={typeof smtp.smtp_user === 'string' ? smtp.smtp_user : ''} onChange={handleTextChange} placeholder="[email protected]" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2">
|
||||
<label className="block text-sm font-bold text-gray-800">邮箱密码 / 授权码</label>
|
||||
<div className="relative">
|
||||
<input type={showPassword ? 'text' : 'password'} value={smtp.smtp_password || ''} onChange={handlePasswordChange} placeholder={smtp.smtp_password_configured ? '已配置,如需替换请输入新密码' : '输入密码或授权码'} className="w-full ios-input px-4 py-3 pr-12 rounded-xl text-sm" />
|
||||
<input type={showPassword ? 'text' : 'password'} value={typeof smtp.smtp_password === 'string' ? smtp.smtp_password : ''} onChange={handlePasswordChange} placeholder={smtp.smtp_password_configured ? '已配置,如需替换请输入新密码' : '输入密码或授权码'} className="w-full ios-input px-4 py-3 pr-12 rounded-xl text-sm" />
|
||||
<button type="button" onClick={handleTogglePassword} className="absolute right-3 top-1/2 -translate-y-1/2 p-2 text-gray-400 hover:text-gray-600 transition-colors">{showPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}</button>
|
||||
</div>
|
||||
<p className="text-xs text-gray-500">QQ 邮箱需使用授权码(QQ 邮箱设置 → 账号 → 开启 SMTP → 生成授权码)</p>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件人显示名(可选)</label><input data-field="smtp_from_name" type="text" value={smtp.smtp_from_name || ''} onChange={handleTextChange} placeholder="闲鱼自动回复系统" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件邮箱地址</label><input data-field="smtp_from_address" type="email" value={smtp.smtp_from_address || smtp.smtp_user || ''} onChange={handleTextChange} placeholder="[email protected]" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件人显示名(可选)</label><input data-field="smtp_from_name" type="text" value={typeof smtp.smtp_from_name === 'string' ? smtp.smtp_from_name : ''} onChange={handleTextChange} placeholder="闲鱼自动回复系统" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
<div className="space-y-2"><label className="block text-sm font-bold text-gray-800">发件邮箱地址</label><input data-field="smtp_from_address" type="email" value={typeof smtp.smtp_from_address === 'string' ? smtp.smtp_from_address : (typeof smtp.smtp_user === 'string' ? smtp.smtp_user : '')} onChange={handleTextChange} placeholder="[email protected]" className="w-full ios-input px-4 py-3 rounded-xl text-sm" /></div>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<label className="flex items-center gap-3 rounded-xl border border-gray-200 p-4 text-sm font-semibold text-gray-700"><input type="checkbox" checked={smtp.smtp_use_tls !== false} onChange={handleTlsChange} />STARTTLS(常用于 587 端口)</label>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @vitest-environment jsdom
|
||||
import { act,renderHook,waitFor } from '@testing-library/react';
|
||||
import { beforeEach,describe,expect,test,vi } from 'vitest';
|
||||
import type { NotificationChannel,SystemSettings } from '../../../shared/api-contract/notifications';
|
||||
import type { NotificationChannel,SystemSettings } from './api';
|
||||
import { createNotificationChannel,deleteNotificationChannel,getNotificationChannels,getSystemSettings,testNotificationChannel,updateNotificationChannel,updateSystemSettings } from './api';
|
||||
import { useNotifications } from './hooks';
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
// models 保存本 feature adapter 输出的 UI 模型;这些模型不直接代表 HTTP DTO。
|
||||
/** 由当前 feature adapter 归一后的 SystemSettings UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface SystemSettings {
|
||||
/** 默认 AI 模型名称。 */
|
||||
ai_model?: string;
|
||||
/** 全局 AI API 密钥。 */
|
||||
ai_api_key?: string;
|
||||
/** 全局 AI API 密钥是否已在服务端配置。 */
|
||||
ai_api_key_configured?: boolean;
|
||||
/** 全局 AI API 地址。 */
|
||||
ai_api_url?: string;
|
||||
/** 全局 AI 基础地址。 */
|
||||
ai_base_url?: string;
|
||||
/** 系统默认回复文案。 */
|
||||
default_reply?: string;
|
||||
/** 是否允许注册新用户。 */
|
||||
registration_enabled?: boolean;
|
||||
/** 系统 SMTP 服务器地址。 */
|
||||
smtp_server?: string;
|
||||
/** 系统 SMTP 密码是否已在服务端配置。 */
|
||||
smtp_password_configured?: boolean;
|
||||
/** 服务端日志级别。 */
|
||||
log_level?: 'debug' | 'info' | 'warn' | 'error' | string;
|
||||
/** 服务端日志格式。 */
|
||||
log_format?: 'text' | 'json' | string;
|
||||
/** 续期日志保留天数。 */
|
||||
renewal_log_retention_days?: number;
|
||||
/** 远程验证码服务地址。 */
|
||||
'captcha.remote_service_url'?: string;
|
||||
/** 远程验证码服务密钥。 */
|
||||
'captcha.remote_secret_key'?: string;
|
||||
/** 远程验证码服务密钥是否已在服务端配置。 */
|
||||
'captcha.remote_secret_key_configured'?: boolean;
|
||||
/** 远程验证码服务 Cookie 配置。 */
|
||||
'captcha.remote_pass_cookies'?: boolean | string;
|
||||
/** 兼容未来配置键的扩展字段。 */
|
||||
/** 未知设置键只能承载服务端声明的标量值,敏感值不进入该 UI 模型。 */
|
||||
[key: string]: string | number | boolean | undefined;
|
||||
}
|
||||
|
||||
/** 由当前 feature adapter 归一后的 NotificationChannelType UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export type NotificationChannelType = 'dingtalk' | 'feishu' | 'bark' | 'webhook' | 'wechat' | 'telegram' | 'email';
|
||||
/** 由当前 feature adapter 归一后的 NotificationEventType UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export type NotificationEventType =
|
||||
| 'account_offline'
|
||||
| 'account_recovered'
|
||||
| 'account_disabled'
|
||||
| 'security_verification'
|
||||
| 'token_renewal'
|
||||
| 'delivery_result'
|
||||
| 'system_error';
|
||||
|
||||
/** 由当前 feature adapter 归一后的 NotificationChannel UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface NotificationChannel {
|
||||
/** 通知渠道标识。 */
|
||||
id: string;
|
||||
/** 通知渠道名称。 */
|
||||
name: string;
|
||||
/** 通知渠道类型。 */
|
||||
type: NotificationChannelType;
|
||||
/** 通知渠道配置。 */
|
||||
config: Record<string, unknown>;
|
||||
/** 渠道绑定的事件类型。 */
|
||||
event_types?: NotificationEventType[];
|
||||
/** 通知渠道是否启用。 */
|
||||
enabled: boolean;
|
||||
/** 创建时间。 */
|
||||
created_at?: string;
|
||||
/** 更新时间。 */
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/** 通知绑定列表中的单条记录。 */
|
||||
/** 由当前 feature adapter 归一后的 NotificationBinding UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface NotificationBinding {
|
||||
/** 账号稳定标识,列表归一化后补充。 */
|
||||
cookie_id?: string;
|
||||
/** 绑定记录主键。 */
|
||||
id?: number;
|
||||
/** 通知渠道主键。 */
|
||||
channel_id: number;
|
||||
/** 通知渠道名称。 */
|
||||
channel_name: string;
|
||||
/** 绑定是否启用。 */
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
/** 简单资源创建接口的数值主键响应。 */
|
||||
/** 由当前 feature adapter 归一后的 MutationIDResponse UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface MutationIDResponse {
|
||||
/** 资源创建是否完成。 */
|
||||
success: boolean;
|
||||
/** 新资源数值主键。 */
|
||||
id: number;
|
||||
}
|
||||
|
||||
/** 简单变更接口的统一成功响应。 */
|
||||
/** 由当前 feature adapter 归一后的 OperationResponse UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface OperationResponse {
|
||||
/** 操作是否完成。 */
|
||||
success: boolean;
|
||||
/** 可选的操作说明。 */
|
||||
message?: string;
|
||||
/** 操作完成后是否需要重新登录。 */
|
||||
requires_relogin?: boolean;
|
||||
}
|
||||
|
||||
/** 通知渠道接口返回的原始具名 DTO。 */
|
||||
/** 由当前 feature adapter 归一后的 NotificationChannelResponse UI 模型;不直接暴露 HTTP DTO。 */
|
||||
export interface NotificationChannelResponse {
|
||||
/** 通知渠道主键。 */
|
||||
id: number;
|
||||
/** 通知渠道名称。 */
|
||||
name: string;
|
||||
/** 通知渠道类型。 */
|
||||
type: string;
|
||||
/** 订阅事件类型 JSON 或兼容文本。 */
|
||||
event_types?: string;
|
||||
/** 通知渠道是否启用。 */
|
||||
enabled: boolean;
|
||||
/** 所属用户主键。 */
|
||||
user_id?: number;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { expect,test } from 'vitest';
|
||||
import type { NotificationChannel } from '../../../shared/api-contract/notifications';
|
||||
import type { NotificationChannel } from './api';
|
||||
import { buildNotificationPayload,emptyNotificationForm,isCurrentNotificationRequest,normalizeNotificationForm,notificationErrorMessage,notificationEventSummary,validateNotificationForm } from './state';
|
||||
import type { NotificationForm } from './types';
|
||||
|
||||
|
||||
Reference in new issue
Block a user