This commit is contained in:
Andreas Knuth 2025-10-18 20:45:58 -05:00
parent c8b2fc7ef7
commit 603299a2f7
1 changed files with 17 additions and 11 deletions

View File

@ -137,23 +137,26 @@ async function syncEmailsForDomainOptimized(domainId: number, bucket: string, s3
if (allS3Keys.length === 0) return; if (allS3Keys.length === 0) return;
const existingEmails = await db // Hole ALLE existierenden E-Mails für diese Domain aus der DB (nicht nur die in S3)
const allDbEmails = await db
.select({ .select({
s3Key: emails.s3Key, s3Key: emails.s3Key,
processed: emails.processed processed: emails.processed
}) })
.from(emails) .from(emails)
.where(inArray(emails.s3Key, allS3Keys)); .where(eq(emails.domainId, domainId));
console.log(`Found ${allDbEmails.length} existing emails in DB for this domain`);
const existingKeysMap = new Map( const existingKeysMap = new Map(
existingEmails.map(e => [e.s3Key, e.processed]) allDbEmails.map(e => [e.s3Key, e.processed])
); );
console.log(`Found ${existingEmails.length} existing emails in DB`);
const toInsert: string[] = []; const toInsert: string[] = [];
const toUpdate: string[] = []; const toUpdate: string[] = [];
const toDelete: string[] = [];
// Bestimme was zu tun ist
for (const key of allS3Keys) { for (const key of allS3Keys) {
if (!existingKeysMap.has(key)) { if (!existingKeysMap.has(key)) {
toInsert.push(key); toInsert.push(key);
@ -162,15 +165,18 @@ async function syncEmailsForDomainOptimized(domainId: number, bucket: string, s3
} }
} }
console.log(`To insert: ${toInsert.length}, To update: ${toUpdate.length}`); // Finde DB-Einträge die nicht mehr in S3 existieren
for (const dbEmail of allDbEmails) {
if (!allS3Keys.includes(dbEmail.s3Key)) {
toDelete.push(dbEmail.s3Key);
}
}
// Finde und lösche E-Mails, die in der DB existieren aber nicht mehr in S3 console.log(`To insert: ${toInsert.length}, To update: ${toUpdate.length}, To delete: ${toDelete.length}`);
const toDelete = existingEmails
.filter(e => !allS3Keys.includes(e.s3Key))
.map(e => e.s3Key);
// Lösche verwaiste E-Mails
if (toDelete.length > 0) { if (toDelete.length > 0) {
console.log(`Found ${toDelete.length} orphaned emails to delete from DB`); console.log(`Deleting ${toDelete.length} orphaned email(s) from DB: ${toDelete.slice(0, 5).join(', ')}${toDelete.length > 5 ? '...' : ''}`);
try { try {
await db.delete(emails).where(inArray(emails.s3Key, toDelete)); await db.delete(emails).where(inArray(emails.s3Key, toDelete));
console.log(`Successfully deleted ${toDelete.length} orphaned emails`); console.log(`Successfully deleted ${toDelete.length} orphaned emails`);