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 DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import Index from "@entity-access/entity-access/dist/decorators/Index.js"; import { User } from "./User.js"; import Mailbox from "./Mailbox.js"; import { HostedDomain } from "./HostedDomain.js"; import Domain from "./Domain.js"; import AppFile from "./AppFile.js"; import Tag from "./Tag.js"; import { JsonProperty } from "@entity-access/server-pages/dist/services/ModelService.js"; import type MailboxContact from "./MailboxContact.js"; import GeoEntity from "./GeoEntity.js"; @Table("Contacts") @Index({ name: "IX_Contacts_Unique_ExternalID", columns: [ { name: (x) => x.externalID, descending: false } ], filter: (x) => x.externalID !== null, unique: true }) export default class Contact { @Column({ key: true, generated: "identity", dataType: "BigInt"}) contactID: number; /** * This should be used by your other application where you may want to * synchronize this contact. Difference between external ID and synchronization * is, externalID has unique index set on it. You cannot have duplicate * externalID values. */ @Column({ dataType: "Char", length: 400, nullable: true }) externalID: string; /** * If set, the owner of the contact is the user, and no one else * can read/write. */ @Column({ dataType: "BigInt", nullable: true }) @RelateTo(User, { property: (c) => c.user, inverseProperty: (u) => u.contacts, foreignKeyConstraint: { name: "FC_Contacts_UserID", onDelete: "set-null" } }) userID: number; /** * If set, the owner of the contact is the mailbox, and anyone * who can access mailbox can read/write. */ @Column({ dataType: "BigInt", nullable: true }) @RelateTo(Mailbox, { property: (c) => c.mailbox, inverseProperty: (mb) => mb.contacts }) mailboxID: number; /** * If set, the owner of the contact is the hosted Domain, and anyone * who can access any mailbox of the domain can read/write. */ @Column({ dataType: "BigInt", nullable: true }) @RelateTo(HostedDomain, { property: (c) => c.hostedDomain, inverseProperty: (d) => d.contacts }) domainID: number; @Column({ dataType: "Char", length: 200}) name: string; @Column({ dataType: "Char", length: 200, nullable: true}) description: string; @Column( {dataType: "Char", nullable: true}) longDescription: string; @Column({ dataType: "Char", length: 50, nullable: true}) summaryDateLabel: string; @Column({ dataType: "DateTime", nullable: true}) summaryDate: DateTime; @Column({ dataType: "Char", length: 1024, nullable: true}) summaryLink: string; @Column({ dataType: "Char", length: 200, nullable: true}) companyID: string; @Column({ dataType: "Char", length: 200, nullable: true}) salesTaxID: string; @Column({ dataType: "Char", length: 200, nullable: true}) gstTaxID: string; @Column({ dataType: "Char", length: 200, nullable: true}) vatTaxID: string; @Column({ dataType: "Char", length: 200, nullable: true}) incomeTaxID: string; @Column({ dataType: "Char", length: 50, default: () => "Unknown" }) type: string; @Column({ dataType: "Char", nullable: true }) synchronization: string; @Column({ default: () => Sql.date.now() }) dateCreated: DateTime; @Column({ default: () => Sql.date.now() }) dateUpdated: DateTime; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(GeoEntity, { property: (x) => x.city, inverseProperty: (x) => x.cityContacts }) cityID: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(AppFile, { property: (c) => c.preview, inverseProperty: (f) => f.previewContacts, foreignKeyConstraint: { onDelete: "set-null" } }) previewID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo({ type: () => ContactAddress, property: (x) => x.billingAddress, inverseProperty: (x) => x.billingContacts }) billingAddressID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo({ type: () => ContactAddress, property: (x) => x.shippingAddress, inverseProperty: (x) => x.shippingContacts }) shippingAddressID: number; emails: ContactEmail[]; phones: ContactPhone[]; domains: ContactDomain[]; items: ContactItem[]; user: User; city: GeoEntity; mailbox: Mailbox; hostedDomain: HostedDomain; preview: AppFile; tags: ContactTag[]; addresses: ContactAddress[]; billingAddress: ContactAddress; shippingAddress: ContactAddress; mailboxContacts: MailboxContact[]; files: ContactFile[]; } @Table("ContactFiles") export class ContactFile { @Column({ dataType: "BigInt", key: true}) @RelateTo(Contact, { property: (x) => x.contact, inverseProperty: (x) => x.files, foreignKeyConstraint: { name: "FC_ContactFiles_ContactID", onDelete: "cascade" } }) contactID: number; @Column({ dataType: "BigInt", key: true}) @RelateTo(AppFile, { property: (x) => x.appFile, inverseProperty: (x) => x.contactFiles, foreignKeyConstraint: { name: "FC_ContactFiles_AppFileID", onDelete: "cascade" } }) appFileID: number; contact: Contact; appFile: AppFile; } @Table("ContactAddresses") export class ContactAddress { @Column({ dataType: "BigInt", key: true, generated: "identity"}) addressID: number; @Column({ dataType: "BigInt" }) @RelateTo(Contact, { property: (x) => x.contact, inverseProperty: (x) => x.addresses, foreignKeyConstraint: { name: "FC_ContactAddresses_ContactID", onDelete: "cascade" } }) contactID: number; @Column({ dataType: "Char", length: 400}) title: string; @Column({ dataType: "Char", length: 400}) line1: string; @Column({ dataType: "Char", length: 400, nullable: true}) line2: string; @Column({ dataType: "Char", length: 400, nullable: true}) line3: string; @Column({ dataType: "Char", length: 50, nullable: true}) city: string; @Column({ dataType: "Char", length: 50, nullable: true}) state: string; @Column({ dataType: "Char", length: 5, nullable: true}) countryCode: string; @Column({ dataType: "Char", length: 5, nullable: true}) phoneNumber: string; @Column({ dataType: "Char", length: 5, nullable: true}) notes: string; contact: Contact; shippingContacts: Contact[]; billingContacts: Contact[]; // billedOrders: StoreJournal[]; // shippedOrders: StoreJournal[]; } @Table("ContactItems") export class ContactItem { @Column({ dataType: "BigInt", key: true, generated: "identity"}) itemID: number; @Column({ dataType: "BigInt" }) @RelateTo(Contact, { property: (ci) => ci.contact, inverseProperty: (c) => c.items, foreignKeyConstraint: { name: "FC_ContactItems_ContactID", onDelete: "cascade" } }) contactID: number; @Column({ dataType: "Char", length: 200 }) type: string; @Column({ dataType: "Char", length: 400}) title: string; @Column({ dataType: "Char" }) valuesJson: string; contact: Contact; } @Table("ContactPhones") export class ContactPhone { @Column({ key: true, dataType: "BigInt"}) @RelateTo(Contact, { property: (ce) => ce.contact, inverseProperty: (c) => c.phones, foreignKeyConstraint: { name: "FC_ContactPhones_ContactID", onDelete: "cascade" } }) public contactID: number; @Column({ key: true, dataType: "Char", length: 50 }) public phoneNumberE64: string; @Column({ dataType: "Char", length: 50, default: () => "Personal"}) public type: string; @Column({ dataType: "Char", nullable: true }) public notes: string; contact: Contact; } @Table("ContactEmails") export class ContactEmail { /** * Since we will be querying contacts through email address * while displaying emails, it is better to keep email address * as first column in primary key */ @Column({ key: true, dataType: "BigInt", order: 1}) @RelateTo(EmailAddress, { property: (ce) => ce.emailAddress, inverseProperty: (e) => e.contactEmails }) public emailAddressID: number; @Column({ key: true, dataType: "BigInt", order: 2}) @RelateTo(Contact, { property: (ce) => ce.contact, inverseProperty: (c) => c.emails, foreignKeyConstraint: { name: "FC_ContactEmails_ContactID", onDelete: "cascade" } }) public contactID: number; @Column({ dataType: "Char", length: 50, default: () => "Personal"}) public type: string; @Column({ dataType: "Char", nullable: true }) public notes: string; @Column({ default: () => false}) blocked: boolean; @JsonProperty({}) emailAddressText: string; public contact: Contact; public emailAddress: EmailAddress; } @Table("ContactDomains") export class ContactDomain { /** * Since we will be querying contacts through domain name * while displaying emails, it is better to keep domain id * as first column in primary key */ @Column({ key: true, dataType: "BigInt"}) @RelateTo(Domain, { property: (cd) => cd.domain, inverseProperty: (d) => d.contactDomains }) domainID: number; @Column({ key: true, dataType: "BigInt"}) @RelateTo(Contact, { property: (x) => x.contact, inverseProperty: (x) => x.domains, foreignKeyConstraint: { name: "FC_ContactDomains_ContactID", onDelete: "cascade" } }) contactID: number; @Column({ default: () => false}) blocked: boolean; domain: Domain; contact: Contact; } @Table("ContactTags") export class ContactTag { @Column({ key: true, dataType: "BigInt", order: 1}) @RelateTo(Contact, { property: (x) => x.contact, inverseProperty: (x) => x.tags, foreignKeyConstraint: { name: "FC_ContactAddresses_ContactID", onDelete: "cascade" } }) contactID: number; @Column({ key: true, dataType: "BigInt", order : 2}) @RelateTo(Tag, { property: (cd) => cd.tag, inverseProperty: (d) => d.contactTags, foreignKeyConstraint: { onDelete: "cascade" } }) tagID: number; contact: Contact; tag: Tag; }