import { Controller, Get, Post, Param, Query, Res, Logger, NotFoundException, Patch, Body } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import type { Response } from 'express'; import { Email } from '../database/entities/email.entity'; import { Attachment } from '../database/entities/attachment.entity'; import { Content } from '../database/entities/content.entity'; import { PaperlessService } from '../paperless/paperless.service'; import { RequirePermissions } from '../auth/permissions.decorator'; import { Permission } from '../auth/permissions.enum'; @Controller('api/emails') export class EmailController { private readonly logger = new Logger(EmailController.name); constructor( @InjectRepository(Email) private readonly emailRepo: Repository, @InjectRepository(Attachment) private readonly attachmentRepo: Repository, @InjectRepository(Content) private readonly contentRepo: Repository, private readonly paperlessService: PaperlessService, ) {} @Get() @RequirePermissions(Permission.VIEW_MAIL) async getEmails( @Query('status') status?: string, @Query('limit') limit?: string, ) { const qb = this.emailRepo.createQueryBuilder('e') .leftJoinAndSelect('e.Attachments', 'a') .orderBy('e.Date', 'DESC') .take(parseInt(limit ?? '50', 10)); if (status !== undefined) { qb.where('e.Status = :status', { status: parseInt(status, 10) }); } return qb.getMany(); } @Get(':id') @RequirePermissions(Permission.VIEW_MAIL) async getEmail(@Param('id') id: string) { return this.emailRepo.findOneOrFail({ where: { Id: parseInt(id, 10) }, relations: ['Attachments'], }); } @Get(':id/attachments') @RequirePermissions(Permission.VIEW_MAIL) async getAttachments(@Param('id') id: string) { return this.attachmentRepo.find({ where: { EmailMessageId: parseInt(id, 10) }, order: { Id: 'ASC' }, }); } @Get('attachments/:attachmentId/content') @RequirePermissions(Permission.VIEW_MAIL) async getAttachmentContent( @Param('attachmentId') attachmentId: string, @Res() res: Response, ) { const id = parseInt(attachmentId, 10); const attachment = await this.attachmentRepo.findOne({ where: { Id: id } }); if (!attachment) throw new NotFoundException('Anhang nicht gefunden'); const content = await this.contentRepo.findOne({ where: { AttachmentEntityId: id } }); if (!content) throw new NotFoundException('Inhalt nicht gefunden'); res.setHeader('Content-Type', attachment.ContentType || 'application/octet-stream'); res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(attachment.FileName)}"`); res.send(content.Content1); } @Patch(':id/status') @RequirePermissions(Permission.MANAGE_ALL) async updateStatus( @Param('id') id: string, @Body('status') status: number, ) { const email = await this.emailRepo.findOneOrFail({ where: { Id: parseInt(id, 10) } }); email.Status = status; await this.emailRepo.save(email); this.logger.log(`E-Mail ${id} auf Status ${status} gesetzt.`); return { message: 'Status aktualisiert' }; } @Post('check-attachments') @RequirePermissions(Permission.MANAGE_ALL) async checkAttachments(@Body() body: { includeProcessed?: boolean } = {}) { const { includeProcessed = false } = body; this.logger.log(`Starte manuelle Prüfung der E-Mail-Anhänge in Paperless... (includeProcessed=${includeProcessed})`); try { const whereCondition = includeProcessed ? [{ Status: 0 }, { Status: 1 }] : { Status: 0 }; const emails = await this.emailRepo.find({ where: whereCondition, relations: ['Attachments'], }); this.logger.log(`Gefunden: ${emails.length} E-Mails. Beginne Prüfung...`); let updatedCount = 0; let idsUpdated = 0; let skippedCount = 0; for (const [index, email] of emails.entries()) { if (!email.Attachments || email.Attachments.length === 0) { skippedCount++; continue; } let hasMatch = false; for (const attachment of email.Attachments) { // Prüfe nur PDFs mit Checksumme if (attachment.ContentType === 'application/pdf' && attachment.Checksum) { this.logger.debug(`Prüfe Checksumme für E-Mail ${email.Id}, Anhang ${attachment.Id} (${attachment.FileName})`); try { const docId = await this.paperlessService.getDocumentIdByChecksum(attachment.Checksum); if (docId !== null) { this.logger.log(`Treffer! Anhang ${attachment.Id} (E-Mail ${email.Id}) → Paperless-Dokument ${docId}.`); hasMatch = true; // PaperlessDocumentId hinterlegen, falls noch nicht vorhanden const existingIds: Record = attachment.PaperlessDocumentIds ?? {}; if (!existingIds['full']) { attachment.PaperlessDocumentIds = { ...existingIds, full: docId }; await this.attachmentRepo.save(attachment); idsUpdated++; this.logger.log(`Anhang ${attachment.Id}: PaperlessDocumentIds aktualisiert (full=${docId}).`); } } } catch (err: any) { this.logger.error(`Fehler bei Checksummen-Prüfung für Attachment ${attachment.Id}: ${err.message}`, err.stack); } } } // Neu gefundene Mails auf Status 1 setzen if (hasMatch && email.Status === 0) { email.Status = 1; await this.emailRepo.save(email); updatedCount++; this.logger.log(`E-Mail ${email.Id} auf Status 1 (Verarbeitet) gesetzt.`); } if ((index + 1) % 10 === 0) { this.logger.log(`Zwischenstand: ${index + 1} von ${emails.length} E-Mails geprüft.`); } } this.logger.log(`Prüfung abgeschlossen. ${updatedCount} E-Mails aktualisiert, ${idsUpdated} Paperless-IDs ergänzt, ${skippedCount} übersprungen.`); return { updatedCount, idsUpdated }; } catch (error: any) { this.logger.error(`Kritischer Fehler bei checkAttachments: ${error.message}`, error.stack); throw error; } } }