feat(mail): Beleg beim Import als neue Version anhängen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bjoernpoettkerandClaude Opus 5.5 committed 2026-11-02 14:01:47 +01:00
1 parent bdc85f5f1f
commit 21a447e915
3 files changed
+120 -9

No files matched your search

@@ -21,6 +21,7 @@ import * as QRCode from 'qrcode';
import { EmailPageCacheService } from './email-page-cache.service';
import { ImapFolderService } from './imap-folder.service';
import { AttachmentFingerprintService } from './attachment-fingerprint.service';
import { DokumentVersionService } from '../paperless/dokument-version.service';
import { PdfService } from '../preprocessing/pdf.service';
import * as path from 'path';
import * as os from 'os';
@@ -73,6 +74,7 @@ export class EmailImportService {
private readonly pageCache: EmailPageCacheService,
private readonly imapFolderService: ImapFolderService,
private readonly fingerprints: AttachmentFingerprintService,
private readonly dokumentVersion: DokumentVersionService,
) {}
async ensurePreviews(emailId: number): Promise<void> {
@@ -568,6 +570,8 @@ export class EmailImportService {
jahr: string;
};
belegnummer?: string;
/** Stammdokument in Paperless, an das der Beleg als neue Version kommt */
versionZuDokumentId?: number | null;
}[];
emailDate: string;
}): Promise<{ success: boolean; results: any[] }> {
@@ -634,6 +638,26 @@ export class EmailImportService {
}
}
/**
* Hängt jede Upload-Datei als neue Version an das Stammdokument. Kein Task,
* keine Belegnummer-Bestätigung – Metadaten des Dokuments bleiben unverändert.
*/
private async importiereAlsVersion(
documentId: number,
uploadItems: { path: string; filename: string; rangeKey: string }[],
paperlessIds: Record<string, number>,
jobId?: string,
): Promise<void> {
for (const item of uploadItems) {
this.setJobStatus(
jobId,
`Hänge ${item.filename} als neue Version an Dokument ${documentId}...`,
);
await this.dokumentVersion.haengeVersionAn(documentId, item.path);
paperlessIds[item.rangeKey] = documentId;
}
}
private async runImport(data: {
jobId?: string;
attachments: {
@@ -650,6 +674,8 @@ export class EmailImportService {
jahr: string;
};
belegnummer?: string;
/** Stammdokument in Paperless, an das der Beleg als neue Version kommt */
versionZuDokumentId?: number | null;
}[];
emailDate: string;
}): Promise<{ success: boolean; results: any[] }> {
@@ -677,7 +703,7 @@ export class EmailImportService {
const baseFilename = sanitizeFilename(
attachmentEntity.FileName.replace(/\.pdf$/i, ''),
);
const paperlessIds: any = {};
const paperlessIds: Record<string, number> = {};
const uploadPromises = [];
@@ -735,6 +761,28 @@ export class EmailImportService {
});
}
// Vom Benutzer bestätigt: als neue Version an das vorhandene Dokument
if (att.versionZuDokumentId) {
await this.importiereAlsVersion(
att.versionZuDokumentId,
uploadPromises,
paperlessIds,
data.jobId,
);
attachmentEntity.PaperlessDocumentIds = paperlessIds;
attachmentEntity.ImportStatus = 1;
if (att.belegnummer) {
attachmentEntity.InterneBelegnummer = att.belegnummer;
}
await this.attachmentRepo.save(attachmentEntity);
results.push({
attachmentId: att.attachmentId,
paperlessIds,
alsVersion: true,
});
continue;
}
// 0. Check if ASN already exists
if (att.belegnummer) {
await this.paperlessService.validateAsnNotExists(att.belegnummer);