import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import EmailAddress from "./EmailAddress.js"; import type EmailRecipient from "./EmailRecipient.js"; import type EmailTag from "./EmailTag.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import AppFile from "./AppFile.js"; import Index from "@entity-access/entity-access/dist/decorators/Index.js"; import type MailboxContact from "./MailboxContact.js"; import MailboxContactEmail from "./MailboxContactEmail.js"; import type ChannelEmail from "./ChannelEmail.js"; import { User } from "./User.js"; import type SubscriberActivity from "./SubscriberActivity.js"; import { IgnoreJsonProperty, JsonProperty, ReadOnlyJsonProperty } from "@entity-access/server-pages/dist/services/ModelService.js"; import type EmailEngagement from "./EmailEngagement.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import { apiPath } from "../../../common/globalEnv.js"; import type MailboxContactEvent from "./MailboxContactEvent.js"; import type ThreadParticipant from "./ThreadParticipant.js"; import type EmailText from "./EmailText.js"; import type { ExternalAttachment } from "./ExternalAttachment.js"; const emailStatuses = [ "note", "queued", "received", "draft", "published", "pending-approval", "approved", "scheduled", "sent", "sent-failed", "deleted", "closed"] as const; export type emailStatusType = typeof emailStatuses[number]; @Table("Emails") @Index({ name: "IX_Emails_HeaderMessageID", columns: [ { name: (x) => x.headerMessageID, descending: false }], filter: (x) => x.headerMessageID !== null }) @Index({ name: "IX_Emails_ExternalEmailID", columns: [ { name: (x) => x.externalEmailID, descending: false }], filter: (x) => x.externalEmailID !== null }) @Index({ name: "IX_Emails_SentMessageID", columns: [ { name: (x) => x.sentMessageID, descending: false }], filter: (x) => x.sentMessageID !== null, unique: true }) @Index({ name: "IX_Emails_HasHtmlBody", columns: [ { name: (x) => x.emailID, descending: true }], filter: (x) => x.mimeFileID !== null && x.htmlBody !== null }) @Index({ name: "IX_Emails_LastReplyID", columns: [ { name: (x) => x.lastReplyID, descending: true }], filter: (x) => x.lastReplyID !== null }) @Index({ name: "IX_Emails_Has_Only_Html_Body", columns: [ { name: (x) => x.status, descending: true }], filter: (x) => x.htmlBodyID === null && x.htmlBody !== null }) @Index({ name: "IX_Emails_State_References_Updated", columns: [ { name: (x) => x.emailID, descending: false }, ], filter: (x) => x.stateReferencesUpdated === false && x.status !== "draft" && x.status !== "scheduled" }) @Index({ name: "IX_Emails_State_WebHook_Invoked", columns: [ { name: (x) => x.emailID, descending: false }], filter: (x) => x.statePostReceiveInvoked === false && x.stateReferencesUpdated === true && x.status !== "draft" && x.status !== "scheduled" }) @Index({ name: "IX_Email_Pending_Search", columns: [{ name: (x) => x.emailID, descending: true }], filter: (x) => x.searchIndexed === false && x.statePostReceiveInvoked === true }) export class Email { @Column({ key: true, dataType: "BigInt", generated: "identity" }) public emailID: number; @Column({ dataType: "Char", nullable: true }) public subject: string; @Column({ dataType: "Char", length: 200, nullable: true}) public summary: string; @Column({ dataType: "Char", nullable: true}) public textBody: string; @Column({ dataType: "Char", nullable: true }) public htmlBody: string; @Column({ dataType: "Char"}) public senderName: string; @Column({ dataType: "BigInt"}) @RelateTo(EmailAddress, { property: (e) => e.senderEmail, inverseProperty: (ea) => ea.senders }) public senderID: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo({ type: () => User, property: (e) => e.senderUser, inverseProperty: (u) => u.sentEmails, foreignKeyConstraint: { name: "FC_Emails_UserID", onDelete: "set-null" } }) public userID: number; @Column({ dataType: "Boolean", default: () => false}) @ReadOnlyJsonProperty public received: boolean; /** Once set to true,this email cannot be sent as an email to anyone */ @Column({ dataType: "Boolean", default: () => false}) public isNote: boolean; @Column({ dataType: "Char", nullable: true}) public headers: string; @Column( { dataType: "Boolean", default: () => false}) public hasUnsubscribe: boolean; @Column({ dataType: "Char", nullable: true }) public headerMessageID: string; @Column({ dataType: "Char", nullable: true }) public inReplyTo: string; @Column({ dataType: "Char", length: 200, nullable: true }) public externalEmailID: string; /** * This will be set by syncing to reduce * email duplication and checking direction of the email */ @Column({ dataType: "BigInt", nullable: true }) public syncedMailboxID: number; @Column({ dataType: "DateTime"}) public dateReceived: DateTime; @Column({ dataType: "DateTime"}) public dateCreated: DateTime; @Column({ dataType: "DateTime", nullable: true}) public dateSent?: DateTime; @Column({ dataType: "DateTime", nullable: true }) public dateUpdated?: DateTime; @Column({ dataType: "AsciiChar", length: 20, enum: emailStatuses }) public status: emailStatusType; @Column({ dataType: "Int", default: () => 0}) public forwardCount: number; @Column({ dataType: "Int", default: () => 0}) public replyCount: number; @Column({ dataType: "Int", default: () => 0}) public viewCount: number; @Column({ dataType: "Int", default: () => 0}) public engagementCount: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo({ type: () => EmailLog, property: (x) => x.mimeFile, inverseProperty: (x) => x.mimeFileEmails, dotNotCreateIndex: true }) public mimeFileID: number; @Column({ dataType: "Char", length: 200, nullable: true}) public sentMessageID: string; @Column({ dataType: "BigInt", nullable: true }) @RelateTo({ type: () => EmailLog, property: (x) => x.htmlBodyFile, inverseProperty: (x) => x.htmlBodyFileEmails, dotNotCreateIndex: true }) public htmlBodyID: number; @Column({ dataType: "BigInt", default: () => 0 }) public emailSize: number; // @Column({ dataType: "BigInt", nullable: true }) // @RelateTo({ // type: () => AppFile, // property: (e) => e.originalFile, // inverseProperty: (af) => af.originalEmails // }) // public originalFileID: number; /** * This is important as email size will never be * in gigabytes, calculating size of mailbox * will be cheaper in terms of memory and cpu. */ @Column({ dataType: "Int", default: () => 0}) public originalFileSize: number; /** * Workflow ID to process the email. */ @Column({ dataType: "Char", nullable: true, length: 400}) public workflowID: string; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(Email, { property: (e) => e.parent, inverseProperty: (e) => e.children }) public parentID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(Email, { property: (e) => e.thread, inverseProperty: (e) => e.threadChildren }) public threadID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(Email, { property: (e) => e.forwarded, inverseProperty: (e) => e.forwards }) public forwardedID: number; // @Column({ dataType: "DateTime", nullable: true }) // public closed: DateTime; @Column({ dataType: "BigInt" , nullable: true }) @RelateTo(Email, { property: (x) => x.lastReply, inverseProperty: (x) => x.lastRepliedThreads, dotNotCreateIndex: true }) public lastReplyID: number; /** * Caching this field here will improve the caching. * As query engine can avoid joining Email table. */ @Column({ dataType: "Char", length: 200, nullable: true}) engagement: string; @Column({ dataType: "Char", nullable: true }) tags: string; @Column({ dataType: "Boolean", default: () => false}) @IgnoreJsonProperty stateReferencesUpdated: boolean; @Column({ dataType: "Boolean", default: () => false}) @IgnoreJsonProperty statePostReceiveInvoked: boolean; // do not add tracking @Column({ dataType: "Boolean", default: () => false}) @IgnoreJsonProperty doNotTrack: boolean; @Column({ dataType: "Boolean", default: () => false}) @IgnoreJsonProperty doNotSendDR: boolean; @Column({ dataType: "Int", default: () => 0}) priority: number; @Column({ dataType: "Boolean", default: () => false}) sendAttachmentsAsLinks: boolean; @Column( {dataType: "Boolean", default: () => false }) searchIndexed: boolean; @ReadOnlyJsonProperty postID: string; @JsonProperty() public sanitizedHtml: string; public tempID: any; public mimeFile: EmailLog; public htmlBodyFile: EmailLog; public senderUser: User; public senderEmail: EmailAddress; public recipients: EmailRecipient[]; public lastReply: Email; public lastRepliedThreads: Email[]; public attachments: AppFile[]; public lastEmailThreads: MailboxContact[]; public threadContacts: MailboxContactEmail[]; public emailTags: EmailTag[]; // public originalFile: AppFile; // public mailboxes: MailboxEmail[]; public parent: Email; public thread: Email; public forwarded: Email; public forwards: Email[]; public children: Email[]; public threadChildren: Email[]; public logs: EmailLog[]; public subscriberActivities: SubscriberActivity[]; public silent?: boolean; public markAsRead?: boolean; public engagements: EmailEngagement[]; mailboxContactEvents: MailboxContactEvent[]; threadParticipants: ThreadParticipant[]; channelEmails: ChannelEmail[]; emailText: EmailText; externalAttachments: ExternalAttachment[]; } @Table("EmailLogs") export class EmailLog { @Column({ dataType: "BigInt", generated: "identity", key: true}) public logID: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(Email,{ property: (x) => x.email, inverseProperty: (x) => x.logs, foreignKeyConstraint: { name: "FC_Email_Logs_EmailID", clearExisting: true, onDelete: "set-null" } }) public emailID: number; @Column({ dataType: "Char", length: 200, default: () => "log.txt"}) public fileName: string; @Column({ dataType: "Int", default: () => 0}) public fileSize: number; @Column({ dataType: "Char", length: 20 }) public compression: string; @Column({ dataType: "DateTime" , default: () => Sql.date.now()}) public dateCreated: DateTime; @Column({ dataType: "Char", length: 200}) public cloudPath: string; public email: Email; public mimeFileEmails: Email[]; public htmlBodyFileEmails: Email[]; @ReadOnlyJsonProperty public get url() { const ext = /\.(eml|html)$/i.test(this.fileName) ? "" : ".txt"; return apiPath(`/api/emails/logs/${this.logID}/${this.fileName}${ext}`); } public set url(v) { // do nothing... } // [GraphService.appendToGraph]() { // return { // url: this.url // }; // } }