rm non existing emails

This commit is contained in:
Andreas Knuth 2025-10-18 20:38:33 -05:00
parent 6767d45aec
commit c8b2fc7ef7
1 changed files with 17 additions and 0 deletions

View File

@ -164,6 +164,23 @@ async function syncEmailsForDomainOptimized(domainId: number, bucket: string, s3
console.log(`To insert: ${toInsert.length}, To update: ${toUpdate.length}`); console.log(`To insert: ${toInsert.length}, To update: ${toUpdate.length}`);
// Finde und lösche E-Mails, die in der DB existieren aber nicht mehr in S3
const toDelete = existingEmails
.filter(e => !allS3Keys.includes(e.s3Key))
.map(e => e.s3Key);
if (toDelete.length > 0) {
console.log(`Found ${toDelete.length} orphaned emails to delete from DB`);
try {
await db.delete(emails).where(inArray(emails.s3Key, toDelete));
console.log(`Successfully deleted ${toDelete.length} orphaned emails`);
} catch (error) {
console.error('Error deleting orphaned emails:', error);
}
} else {
console.log('No orphaned emails found');
}
if (toUpdate.length > 0) { if (toUpdate.length > 0) {
const updateLimit = pLimit(CONCURRENT_S3_OPERATIONS); const updateLimit = pLimit(CONCURRENT_S3_OPERATIONS);
const updatePromises = toUpdate.map(key => const updatePromises = toUpdate.map(key =>