// import Inject from "@entity-access/entity-access/dist/di/di.js"; // import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; // import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; // import SocialMailContext from "../../model/SocialMailContext.js"; // import { Email } from "../../model/entities/Email.js"; // import EmailLogService from "../../services/email-logs/EmailLogService.js"; // import TempFileService from "../../storage/TempFileService.js"; // import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; // export default class MigrateEmailFilesWorkflow extends Workflow { // static taskGroup = "batch"; // async run() { // const start = this.currentTime; // const end = this.currentTime.date.addDays(1); // let i = 1; // while (end.msSinceEpoch > Date.now()){ // const migrated = await this.migrate(i++); // if (!migrated) { // return; // } // } // } // @UniqueActivity // async migrate( // i, // @Inject db?: SocialMailContext, // @Inject els?: EmailLogService, // @Inject tfs?: TempFileService // ) { // db.verifyFilters = false; // db.raiseEvents = false; // for (let index = 0; index < 100; index++) { // const batch = await db.emails.where((x) => x.originalFileID !== null && x.mimeFileID === null) // .include((x) => x.originalFile.fileContent) // .limit(10) // .toArray(); // if (!batch.length) { // return false; // } // for (const iterator of batch) { // try { // await this.migrateFile(iterator, db, els, tfs); // } catch (error) { // if(/The specified blob does not exist/i.test(error.stack ?? error.toString())) { // await db.emails.where(iterator, (x, p) => x.emailID === p.emailID) // .update((x) => ({ // originalFileID: Sql.cast.asBigInt(null) // })); // continue; // } // throw error; // } // } // db.changeSet.clear(); // } // return true; // } // async migrateFile(email: Email, db: SocialMailContext, els: EmailLogService, tfs: TempFileService) { // const { originalFile, emailID, originalFileID } = email; // if (!originalFile?.fileContent) { // await db.emails.where(email, (x, p) => x.emailID === p.emailID) // .update((x) => ({ // mimeFileID: Sql.cast.asBigInt(null), // originalFileID: Sql.cast.asBigInt(null) // })); // return; // } // const tf = await tfs.downloadFileWithoutCache(originalFile); // const log = await els.save(emailID, tf); // await tf.delete(); // const hasAny = await db.appFiles.where({ originalFileID , emailID }, (x, p) => x.appFileID === p.originalFileID // && x.originalEmails.some((o) => o.emailID !== p.emailID)) // .some(); // await using tx = await db.connection.createTransaction(); // await db.emails.where(email, (x, p) => x.emailID === p.emailID) // .update(log, (x, p) => ({ // mimeFileID: Sql.cast.asBigInt(p.logID), // originalFileID: Sql.cast.asBigInt(null) // })); // // delete original file here... // // must not have any parent... // if (originalFile.parentID) { // await tx.commit(); // return; // } // if (!hasAny) { // await db.fileContents.where(originalFile,(x, p) => x.fileContentID === p.fileContentID) // .update((x) => ({ deleted: true })); // await db.appFiles.asQuery() // .delete(originalFile, (x, p) => x.appFileID === p.appFileID); // } // await tx.commit(); // } // }