fix(integration): simplify manual DingTalk sync setup

This commit is contained in:
2026-07-13 14:01:45 +08:00
parent effa34434b
commit 0515e6ed27
6 changed files with 203 additions and 88 deletions

View File

@@ -1,18 +1,16 @@
/** 钉钉配置 */
export interface DingTalkThirdConfig {
agentId: string; // AppKey
appSecret: string; // AppSecret
appSecret?: string; // AppSecret;更新已有配置时可留空保留旧值
corpId: string; // CorpId
startEnable: boolean; // 是否启用同步
appId?: string; // 内部应用ID用于消息推送可选
}
/** 企微配置 */
export interface WeComThirdConfig {
agentId: string;
appSecret: string;
appSecret?: string;
corpId: string;
startEnable: boolean;
}
/** 对外返回的配置(脱敏后,不含 appSecret */

View File

@@ -0,0 +1,49 @@
import { IntegrationConfigService } from './integration-config.service';
describe('IntegrationConfigService.testConnection', () => {
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
jest.restoreAllMocks();
});
it('uses the saved AppSecret when testing an existing configuration with a blank secret', async () => {
const configRepo = {
findOne: jest.fn().mockResolvedValue({ id: 1, type: 'THIRD' }),
};
const detailRepo = {
findOne: jest.fn().mockResolvedValue({
configId: 1,
type: 'DINGTALK_SYNC',
content: JSON.stringify({
config: {
corpId: 'ding-corp',
agentId: 'saved-key',
appSecret: 'saved-secret',
},
}),
}),
};
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue({ accessToken: 'token' }),
}) as never;
const service = new IntegrationConfigService(configRepo as never, detailRepo as never);
await expect(
service.testConnection('DINGTALK', {
corpId: 'ding-corp',
agentId: 'saved-key',
appSecret: '',
}),
).resolves.toBe(true);
expect(global.fetch).toHaveBeenCalledWith(
'https://api.dingtalk.com/v1.0/oauth2/accessToken',
expect.objectContaining({
body: JSON.stringify({ appKey: 'saved-key', appSecret: 'saved-secret' }),
}),
);
});
});

View File

@@ -125,7 +125,12 @@ export class IntegrationConfigService {
config: DingTalkThirdConfig | WeComThirdConfig,
): Promise<boolean> {
try {
const token = await this.getTokenForTest(type, config as unknown as Record<string, unknown>);
const finalConfig = { ...config } as Record<string, unknown>;
if (!finalConfig.appSecret) {
const savedConfig = await this.getRawConfig(type);
if (savedConfig?.appSecret) finalConfig.appSecret = savedConfig.appSecret;
}
const token = await this.getTokenForTest(type, finalConfig);
return !!token;
} catch (e) {
this.logger.error(`连接测试失败: ${(e as Error).message}`);