feat: add manual Paperless ID synchronization for email attachments and update default barcode margins to 7mm.
Build and Push Multi-Platform Images / build-and-push (push) Successful in 36s
Build and Push Multi-Platform Images / build-and-push (push) Successful in 36s
This commit is contained in:
@@ -89,19 +89,21 @@ export class EmailController {
|
||||
|
||||
@Post('check-attachments')
|
||||
@RequirePermissions(Permission.MANAGE_ALL)
|
||||
async checkAttachments() {
|
||||
this.logger.log('Starte manuelle Prüfung der E-Mail-Anhänge in Paperless...');
|
||||
|
||||
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 {
|
||||
// Hole alle neuen E-Mails (Status = 0) inkl. Anhängen
|
||||
const whereCondition = includeProcessed ? [{ Status: 0 }, { Status: 1 }] : { Status: 0 };
|
||||
const emails = await this.emailRepo.find({
|
||||
where: { Status: 0 },
|
||||
where: whereCondition,
|
||||
relations: ['Attachments'],
|
||||
});
|
||||
|
||||
this.logger.log(`Gefunden: ${emails.length} E-Mails mit Status "Neu" (0). Beginne Prüfung...`);
|
||||
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()) {
|
||||
@@ -113,15 +115,23 @@ export class EmailController {
|
||||
let hasMatch = false;
|
||||
|
||||
for (const attachment of email.Attachments) {
|
||||
// Prüfe nur PDFs und wenn eine Checksumme vorhanden ist
|
||||
// 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 exists = await this.paperlessService.checksumExists(attachment.Checksum);
|
||||
if (exists) {
|
||||
this.logger.log(`Treffer! Anhang ${attachment.Id} (E-Mail ${email.Id}) in Paperless gefunden.`);
|
||||
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;
|
||||
break; // Ein Treffer reicht für diese E-Mail
|
||||
|
||||
// PaperlessDocumentId hinterlegen, falls noch nicht vorhanden
|
||||
const existingIds: Record<string, number> = 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);
|
||||
@@ -129,22 +139,21 @@ export class EmailController {
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn mindestens ein Anhang in Paperless existiert, markiere die Mail als verarbeitet (Status = 1)
|
||||
if (hasMatch) {
|
||||
// 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.`);
|
||||
}
|
||||
|
||||
// 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 };
|
||||
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;
|
||||
|
||||
@@ -229,6 +229,16 @@ export class PaperlessService {
|
||||
return response.data.count > 0;
|
||||
}
|
||||
|
||||
async getDocumentIdByChecksum(checksum: string): Promise<number | null> {
|
||||
const response = await this.client.get('/documents/', {
|
||||
params: { checksum__iexact: checksum },
|
||||
});
|
||||
if (response.data.count > 0 && response.data.results?.length > 0) {
|
||||
return response.data.results[0].id as number;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft, ob eine ASN bereits vergeben ist und wirft einen Fehler, falls ja.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user