import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField } from "@tailor-platform/sdk"; import { customerAccount } from "./customerAccount"; import { partnerBankAccount } from "./partnerBankAccount"; export const CUSTOMER_BANK_ACCOUNT_PURPOSES = ["RECEIPT", "REFUND"] as const; const builtins = { accountId: db .uuid() .relation({ type: "n-1", toward: { type: customerAccount, as: "account" }, backward: "bankAccountUsages", }) .description("Foreign key to CustomerAccount"), bankAccountId: db .uuid() .relation({ type: "n-1", toward: { type: partnerBankAccount, as: "bankAccount" }, backward: "customerBankAccountUsages", }) .description("Foreign key to reusable PartnerBankAccount"), purpose: db .enum(CUSTOMER_BANK_ACCOUNT_PURPOSES) .description("Customer bank account purpose: RECEIPT or REFUND"), isDefault: db .bool() .description("Whether this is the default bank account for the account purpose"), }; type ReservedFields = keyof typeof builtins | CommonReservedFields; export interface CreateCustomerBankAccountUsageTypeParams< F extends Record, > { fields?: NoReservedFields; } export function createCustomerBankAccountUsageType< const F extends Record, >(params: CreateCustomerBankAccountUsageTypeParams) { return db .table("CustomerBankAccountUsage", { ...builtins, ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .indexes({ fields: ["accountId", "bankAccountId", "purpose"], unique: true, name: "customer_bank_account_usage_unique_idx", }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const customerBankAccountUsage = createCustomerBankAccountUsageType({});