import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import WebSite from "./WebSite.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import { StoreAccount } from "./StoreAccount.js"; import { StoreItemPrice } from "./StoreItemPrice.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import type { StoreJournal } from "./StoreJournal.js"; import type { HostedDomain } from "./HostedDomain.js"; import type SubscriptionWebSite from "./SubscriptionWebSite.js"; import type SubscriptionDomain from "./SubscriptionDomain.js"; const subscriptionStatusArray = [ "active", "pending-renewal", "pending", "cancelled"] as const; export type subscriptionStatusArrayType = typeof subscriptionStatusArray[number]; /** * There will be one subscription per one product, customer will have multiple active subscriptions. * This is easier to maintain. * * The reason this class exists as we can switch between payment providers in future and still maintain * the history as it is. The worker can fetch active subscriptions instead of webhook to activate/deactivate * plans. */ @Table("StoreSubscriptions") export default class StoreSubscription { @Column({ dataType: "BigInt", key: true, generated: "identity"}) public subscriptionID: number; @Column({ dataType: "BigInt"}) @RelateTo(WebSite, { property: (x) => x.store, inverseProperty: (x) => x.subscriptions }) public storeID: number; @Column({ dataType: "BigInt" ,nullable: true }) @RelateTo(StoreAccount, { property: (x) => x.buyer, inverseProperty: (x) => x.boughtSubscriptions, foreignKeyConstraint: { name: "FC_Subscription_BuyerID", onDelete: "restrict" } }) buyerID: number; @Column({ dataType: "BigInt"}) @RelateTo(StoreItemPrice, { property: (x) => x.itemPrice, inverseProperty: (x) => x.subscriptions }) itemPriceID: number; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) dateCreated: DateTime; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) dateUpdated: DateTime; @Column({ dataType: "DateTime", default: () => Sql.date.now()}) renewalDate: DateTime; /** * Check every 15 days... */ @Column({ dataType: "DateTime", default: () => Sql.date.addDays(Sql.date.now(), 1)}) refreshTime: DateTime; @Column({ dataType: "Char", length: 20, default: () => "pending"}) status: subscriptionStatusArrayType; @Column({ dataType: "BigInt", nullable: true}) currentOrderID: number; store: WebSite; buyer: StoreAccount; itemPrice: StoreItemPrice; invoices: StoreJournal[]; hostedDomains: HostedDomain[]; subscriptionWebSites: SubscriptionWebSite[]; subscriptionDomains: SubscriptionDomain[]; }