import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import Mailbox from "../../model/entities/Mailbox.js"; import { IEntityQuery } from "@entity-access/entity-access/dist/model/IFilterWithParameter.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import MailboxContact from "../../model/entities/MailboxContact.js"; import TagService from "../tags/TagService.js"; @RegisterScoped export default class MailboxFolderService { @Inject db: SocialMailContext; @Inject ts: TagService; systemFolders = null as number[]; async incrementFolderCount(mailboxID: number, tagID: number, change: number, archivedChange = 0) { const updated = await this.db.mailboxFolders.where({ mailboxID, tagID }, (x, p) => x.mailboxID === p.mailboxID && x.tagID === p.tagID) .update({ change, archivedChange }, (x, p) => ({ count: Sql.math.max(0, x.count + p.change), archivedCount: Sql.math.max(0, x.archivedCount, p.archivedChange) })); if(updated) { return; } // try upsert... await this.db.mailboxFolders.withKeys({ mailboxID, tagID}) .upsert((x) => x ? ({ ... x, count: Math.max(0, x.count + change), archivedCount: Math.max(0, x.archivedCount + archivedChange) }) : ({ isSystem: true, count: archivedChange > 0 ? 0 : 1, archivedCount: Math.max(0, archivedChange) }) ); } async updateFolders(mb: Mailbox) { const { ts, db } = this; let contacts; let isNew; let isBlocked; let isSpam; let { systemFolders} = this; if (!systemFolders) { contacts = await ts.getSystemTagID("Contacts"); isNew = await ts.getSystemTagID("New"); isBlocked = await ts.getSystemTagID("Blocked"); isSpam = await ts.getSystemTagID("Spam"); systemFolders = [contacts, isNew, isBlocked, isSpam]; this.systemFolders = systemFolders; } contacts = systemFolders[0]; isNew = systemFolders[0]; isBlocked = systemFolders[0]; isSpam = systemFolders[0]; const { mailboxID } = mb; // fix tag... const counts = await db.mailboxContacts.where({ mailboxID, systemFolders }, (x, p) => x.mailboxID === p.mailboxID && (x.folderTagID === null || Sql.in(x.folderTagID, p.systemFolders)) ) .sum((x) => ( { blocked: x.blocked === true ? 1 : 0, newRequest: x.newRequest === true ? 1 : 0, spam: x.spam === true ? 1 : 0, total: 1 })); await this.saveFolder(db, ts, mb, contacts, counts.total, (q) => q.where((x) => x.newRequest === false && x.blocked === false && x.spam === false )); await this.saveFolder(db, ts, mb, isNew, counts.newRequest, (q) => q.where((x) => x.newRequest === true)); await this.saveFolder(db, ts, mb, isBlocked, counts.blocked, (q) => q.where((x) => x.blocked === true)); await this.saveFolder(db, ts, mb, isSpam , counts.spam, (q) => q.where((x) => x.spam === true)); } async saveFolder(db: SocialMailContext, ts: TagService, { mailboxID }: Mailbox, tagID: number, count: number, q?: (q1: IEntityQuery) => IEntityQuery ) { await db.mailboxFolders.statements.upsert({ mailboxID, tagID, isSystem: true, count }, (x) => { x.count = count; return x; }, { mailboxID, tagID }); const systemFolders = await ts.getMailFolderTags(); await q(db.mailboxContacts.where({ mailboxID, systemFolders }, (x, p) => x.mailboxID === p.mailboxID && (x.folderTagID === null || Sql.in(x.folderTagID, p.systemFolders)) )).update({ tagID },(x, p) => ({ folderTagID: Sql.cast.asBigInt(p.tagID) })); } }