实现分发器模块

- 添加分发器类型定义 (distributor.ts)
- 实现分发器基类 (base.distributor.ts)
- 实现邮件分发器 (email.distributor.ts)
- 实现钉钉分发器 (dingtalk.distributor.ts)
- 实现企业微信分发器 (wechat.distributor.ts)
- 实现 Slack 分发器 (slack.distributor.ts)
- 创建分发器工厂 (distributor.factory.ts)
- 实现分发服务 (distributor.service.ts)
- 安装 nodemailer 依赖

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Opus 4.5 committed 2026-02-04 21:32:54 +08:00
1 parent 75c765af93
commit 5746676819
11 files changed
+562

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
import prisma from '../config/database';
import { DistributorFactory } from '../distributors/distributor.factory';
import { DistributorType, ReportSummary } from '../types/distributor';
import { config } from '../config';
import logger from '../utils/logger';
export class DistributorService {
/**
* 分发报告到指定渠道
*/
async distributeReport(reportId: number, channelId: number): Promise<void> {
let status: 'success' | 'failed' = 'success';
let errorMessage: string | undefined;
try {
const report = await prisma.report.findUnique({
where: { id: reportId },
include: { topic: true },
});
if (!report) {
throw new Error(`Report ${reportId} not found`);
}
const channel = await prisma.channel.findUnique({
where: { id: channelId },
});
if (!channel) {
throw new Error(`Channel ${channelId} not found`);
}
if (!channel.enabled) {
logger.info(`渠道 ${channel.name} 已禁用,跳过分发`);
return;
}
logger.info(`开始分发报告: ${report.title} -> ${channel.name}`);
const distributor = DistributorFactory.create(
channel.type as DistributorType,
channel.config as any,
channel.id
);
if (!distributor.validateConfig()) {
throw new Error('Invalid distributor config');
}
const summary: ReportSummary = {
title: report.title,
topicName: report.topic.name,
articleCount: report.articleCount,
periodStart: report.periodStart,
periodEnd: report.periodEnd,
webUrl: this.buildReportUrl(reportId),
};
const result = await distributor.distribute(summary, report.content);
if (!result.success) {
throw new Error(result.error || 'Distribution failed');
}
logger.info(`报告分发成功: ${channel.name}`);
} catch (error) {
status = 'failed';
errorMessage = error instanceof Error ? error.message : 'Unknown error';
logger.error('报告分发失败', { reportId, channelId, error: errorMessage });
} finally {
await prisma.pushLog.create({
data: { reportId, channelId, status, errorMessage },
});
}
}
/**
* 分发报告到所有关联的渠道
*/
async distributeReportToAllChannels(reportId: number): Promise<void> {
const report = await prisma.report.findUnique({
where: { id: reportId },
include: {
topic: {
include: {
topicChannels: { include: { channel: true } },
},
},
},
});
if (!report) {
throw new Error(`Report ${reportId} not found`);
}
const channels = report.topic.topicChannels
.map((tc) => tc.channel)
.filter((c) => c.enabled);
logger.info(`开始分发报告到 ${channels.length} 个渠道`);
for (const channel of channels) {
await this.distributeReport(reportId, channel.id);
}
logger.info('报告分发完成');
}
private buildReportUrl(reportId: number): string {
const baseUrl = process.env.WEB_BASE_URL || `http://localhost:${config.port}`;
return `${baseUrl}/reports/${reportId}`;
}
}