Initial commit with Email Import Wizard and Task Processor updates
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
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<Email>,
|
||||
@InjectRepository(Attachment) private readonly attachmentRepo: Repository<Attachment>,
|
||||
@InjectRepository(Content) private readonly contentRepo: Repository<Content>,
|
||||
private readonly paperlessService: PaperlessService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
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')
|
||||
async getEmail(@Param('id') id: string) {
|
||||
return this.emailRepo.findOneOrFail({
|
||||
where: { Id: parseInt(id, 10) },
|
||||
relations: ['Attachments'],
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':id/attachments')
|
||||
async getAttachments(@Param('id') id: string) {
|
||||
return this.attachmentRepo.find({
|
||||
where: { EmailMessageId: parseInt(id, 10) },
|
||||
order: { Id: 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
@Get('attachments/:attachmentId/content')
|
||||
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() {
|
||||
this.logger.log('Starte manuelle Prüfung der E-Mail-Anhänge in Paperless...');
|
||||
|
||||
try {
|
||||
// Hole alle neuen E-Mails (Status = 0) inkl. Anhängen
|
||||
const emails = await this.emailRepo.find({
|
||||
where: { Status: 0 },
|
||||
relations: ['Attachments'],
|
||||
});
|
||||
|
||||
this.logger.log(`Gefunden: ${emails.length} E-Mails mit Status "Neu" (0). Beginne Prüfung...`);
|
||||
|
||||
let updatedCount = 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 und wenn eine Checksumme vorhanden ist
|
||||
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 exists = await this.paperlessService.checksumExists(attachment.Checksum);
|
||||
if (exists) {
|
||||
this.logger.log(`Treffer! Anhang ${attachment.Id} (E-Mail ${email.Id}) in Paperless gefunden.`);
|
||||
hasMatch = true;
|
||||
break; // Ein Treffer reicht für diese E-Mail
|
||||
}
|
||||
} catch (err: any) {
|
||||
this.logger.error(`Fehler bei Checksummen-Prüfung für Attachment ${attachment.Id}: ${err.message}`, err.stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn mindestens ein Anhang in Paperless existiert, markiere die Mail als verarbeitet (Status = 1)
|
||||
if (hasMatch) {
|
||||
email.Status = 1;
|
||||
await this.emailRepo.save(email);
|
||||
updatedCount++;
|
||||
this.logger.log(`E-Mail ${email.Id} auf Status 1 (Verarbeitet) gesetzt.`);
|
||||
}
|
||||
|
||||
// Zwischenstand loggen
|
||||
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} aktualisiert, ${skippedCount} ohne (PDF-)Anhänge übersprungen.`);
|
||||
return { updatedCount };
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Kritischer Fehler bei checkAttachments: ${error.message}`, error.stack);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user