Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion backend/src/queue/document.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { VerificationService } from '../verification/verification.service';
import { VerificationStatus } from '../verification/entities/verification-record.entity';
import { RiskAssessmentService } from '../risk-assessment/risk-assessment.service';
import { StellarService } from '../stellar/stellar.service';
import { MailService } from '../mail/mail.service';
import { UsersService } from '../users/users.service';
import { QueueService } from './queue.service';

@Injectable()
Expand All @@ -20,13 +22,15 @@ export class DocumentProcessor implements OnModuleDestroy {
private readonly documentsService: DocumentsService,
private readonly stellarService: StellarService,
private readonly verificationService: VerificationService,
private readonly mailService: MailService,
private readonly usersService: UsersService,
) {
const connection = this.queueService.getConnectionOptions();
this.worker = new Worker(
this.queueService.queueName,
async (job) => {
if (job.name === 'analyze') {
await this.riskService.assessDocument(job.data.documentId);
await this.handleAnalyze(job.data.documentId);
return;
}
if (job.name === 'anchor') {
Expand All @@ -41,6 +45,41 @@ export class DocumentProcessor implements OnModuleDestroy {
});
}

private async handleAnalyze(documentId: string) {
const riskResult = await this.riskService.assessDocument(documentId);
if (riskResult.flags.length === 0) {
return;
}

const document = await this.documentsService.findById(documentId);
if (!document) {
this.logger.warn(`Document ${documentId} not found for risk alert`);
return;
}

const user = await this.usersService.findById(document.ownerId);
if (!user?.email) {
this.logger.warn(`Owner ${document.ownerId} not found for risk alert`);
return;
}

try {
await this.mailService.sendRiskAlert(
user.email,
document.title,
riskResult.flags.map(String),
);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const stack = error instanceof Error ? error.stack : undefined;
this.logger.error(
`Failed to send risk alert for document ${documentId}`,
message,
stack,
);
}
}

private async handleAnchor(documentId: string) {
const document = await this.documentsService.findById(documentId);
if (!document) {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/queue/queue.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { DocumentsModule } from '../documents/documents.module';
import { RiskAssessmentModule } from '../risk-assessment/risk-assessment.module';
import { StellarModule } from '../stellar/stellar.module';
import { VerificationModule } from '../verification/verification.module';
import { MailModule } from '../mail/mail.module';
import { UsersModule } from '../users/users.module';
import { DocumentProcessor } from './document.processor';
import { QueueService } from './queue.service';

Expand All @@ -15,6 +17,8 @@ import { QueueService } from './queue.service';
RiskAssessmentModule,
StellarModule,
VerificationModule,
MailModule,
UsersModule,
],
providers: [QueueService, DocumentProcessor],
exports: [QueueService],
Expand Down