api-server/src/workers/document-import.worker.ts
WangDL 08f31dd5b6 feat: P0 后端补全 — BullMQ Workers 注册 + 用户 Profile API + 角色权限
- AppModule 注册 3 个 BullMQ Workers (AiAnalysis/DocumentImport/Notification)
- Users 模块新增 GET/PATCH /users/me/profile 端点:
  - GET 读取 UserProfile (learningIdentity, learningDirection, bio, currentGoal)
  - PATCH upsert UserProfile
  - GET /users/me 返回 profile + preferences (include join)
- 新增 RolesGuard + @Roles() 装饰器 (UserRole enum)
- QueueModule/QueueService 改进
- 各模块 controller/repository/service 完善

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 19:08:07 +08:00

33 lines
1.2 KiB
TypeScript

import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Logger } from '@nestjs/common';
import { Job } from 'bullmq';
import { QUEUE_DOCUMENT_IMPORT } from '../infrastructure/queue/queue.service';
import { DocumentImportRepository } from '../modules/document-import/document-import.repository';
@Processor(QUEUE_DOCUMENT_IMPORT)
export class DocumentImportWorker extends WorkerHost {
private readonly logger = new Logger(DocumentImportWorker.name);
constructor(
private readonly repository: DocumentImportRepository,
) {
super();
}
async process(job: Job<{ importId: string; userId: string }>) {
this.logger.log(`Processing document import job ${job.id}, importId=${job.data.importId}`);
await this.repository.updateStatus(job.data.importId, 'processing');
try {
// TODO: actual file parsing + AI knowledge generation
await new Promise((resolve) => setTimeout(resolve, 1000));
await this.repository.updateStatus(job.data.importId, 'completed');
this.logger.log(`Document import job ${job.id} completed`);
} catch (err: any) {
this.logger.error(`Document import job ${job.id} failed: ${err.message}`);
await this.repository.updateStatus(job.data.importId, 'failed');
throw err;
}
}
}