import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; import SocialMailContext from "../../../model/SocialMailContext.js"; import Inject from "@entity-access/entity-access/dist/di/di.js"; export default class RepairEmailCountsWorkflow extends Workflow<{ mailboxID? }> { static taskGroup = "batch"; async run() { if (typeof this.input === "string") { let start = 0; do{ start = await this.queueForAll(start); } while(BigInt(start)); return; } if (this.input) { let start = 0; do { start = await this.updateLastReplyID(start); } while(BigInt(start)); } if (this.input) { let start = 0; do { start = await this.computeContacts(start); } while(BigInt(start)); await this.saveResults(); } } @UniqueActivity async saveResults( @Inject db?: SocialMailContext ) { const all = await db.mailboxContacts.where(this.input, (x, p) => x.mailboxID === p.mailboxID) .sum((x) => ({ totalThreads: x.totalThreads, openThreads: x.openThreads })); await db.mailboxes.statements.update(all, { mailboxID: this.input.mailboxID}); } @UniqueActivity async updateLastReplyID( start, @Inject db?: SocialMailContext ) { const { mailboxID } = this.input; const q = db.emails .where({ start, mailboxID }, (x, p) => x.emailID > p.start && x.threadID === null && x.threadContacts.some((t) => t.mailboxID === p.mailboxID) && x.lastReplyID === null ); const list = await q .orderBy((x) => x.emailID) .limit(100) .toArray(); if (!list.length) { return 0; } for (const iterator of list) { const { emailID } = iterator ; start = emailID; const lastReply = await db.emails.where(iterator, (x, p) => x.threadID === p.emailID) .orderByDescending((x) => x.emailID) .first(); const lastReplyID = lastReply?.emailID ?? null; if (lastReplyID !== iterator.lastReplyID) { await db.emails.statements.update({ lastReplyID }, { emailID }); } db.changeSet.clear(); } return start; } @UniqueActivity async computeContacts( start, @Inject db?: SocialMailContext ) { const mbID = this.input.mailboxID; const q = db.mailboxContacts .where({ start, mbID }, (x, p) => x.emailAddressID > p.start && x.mailboxID === p.mbID && x.autoClose === false && x.blocked === false ); const list = await q .orderBy((x) => x.emailAddressID) .limit(100) .toArray(); if (!list.length) { return 0; } for (const iterator of list) { const { mailboxID, emailAddressID, autoClose, blocked } = iterator; start = emailAddressID; if (autoClose || blocked) { await db.mailboxContacts.statements.update({ totalThreads: 0, openThreads: 0 }, { mailboxID, emailAddressID}); continue; } const all = await db.emails .where(iterator, (x, p) => x.threadContacts.some((t) => t.mailboxID === p.mailboxID && t.emailAddressID === p.emailAddressID) && x.threadID === null ) .sum((x) => ({ totalThreads: 1, openThreads: x.lastReplyID === null ? 1 : 0 })); await db.mailboxContacts.statements.update(all, { mailboxID, emailAddressID}); } return start; } @UniqueActivity async queueForAll( start, @Inject db?: SocialMailContext ) { const list = await db.mailboxes.where({ start }, (x, p) => x.mailboxID > p.start) .orderBy((x) => x.mailboxID) .limit(100) .toArray(); if (!list.length) { return 0; } for (const { mailboxID } of list) { await this.context.queue(RepairEmailCountsWorkflow, { mailboxID } , { id: `repair-all-${mailboxID}`}); start = mailboxID; } return start; } }