import Inject from "@entity-access/entity-access/dist/di/di.js"; import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import CacheService from "../../services/cache/CacheService.js"; import { isTestMode } from "../../../globalServices.js"; import EmailLogService from "../../services/email-logs/EmailLogService.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import TimeSpan from "@entity-access/entity-access/dist/types/TimeSpan.js"; export default class ClearEmailHtmlBody extends Workflow { static taskGroup = "batch"; async run(): Promise { this.preserveTime = this.failedPreserveTime = TimeSpan.fromMinutes(30); for (let index = 0; index < 10; index++) { await this.clear(index); } } @UniqueActivity async clear( i?: number, @Inject db?: SocialMailContext, @Inject cacheService?: CacheService, @Inject els?: EmailLogService ) { if (isTestMode) { return; } await db.emails .where((x) => x.mimeFileID !== null && x.htmlBody !== null) .limit(100) .update((x) => ({ htmlBody: null })); const emails = await db.emails.where((x) => x.htmlBody !== null && x.htmlBodyID === null && (x.status !== "draft" && x.status !== "scheduled")) .limit(100) .toArray(); for (const iterator of emails) { let htmlBodyID = null; if (iterator.htmlBody) { const file = await els.saveText(iterator.emailID, "body.html", iterator.htmlBody); htmlBodyID = file.logID; } await db.emails.where(iterator, (x, p) => x.emailID === p.emailID) .update({ htmlBodyID }, (x, p) => ({ htmlBody: null, htmlBodyID: Sql.cast.asBigInt(p.htmlBodyID) })); } if (!emails.length) { await cacheService.clear(); return; } await cacheService.clear(); } }