import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; export const BANK_ACCOUNT_STATUSES = ["ACTIVE", "INACTIVE"] as const; const builtins = (params: { companyType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; }) => { const companyId = db.uuid().description("Foreign key to the owning Company"); const currencyId = db.uuid().description("Foreign key to the bank-account Currency"); const accountField = (description: string, optional = false) => db.uuid({ optional }).description(description); const cashAccountId = accountField("Foreign key to the actual bank GL Account"); const incomingClearingAccountId = accountField( "Optional foreign key to the incoming-payment clearing Account", true, ); const outgoingClearingAccountId = accountField( "Optional foreign key to the outgoing-payment clearing Account", true, ); return { companyId: params.companyType ? companyId.relation({ type: "n-1", toward: { type: params.companyType, as: "company" }, backward: "bankAccounts", }) : companyId, currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "bankAccounts", }) : currencyId, cashAccountId: params.accountType ? cashAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "cashAccount" }, backward: "cashBankAccounts", }) : cashAccountId, incomingClearingAccountId: params.accountType ? incomingClearingAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "incomingClearingAccount" }, backward: "incomingClearingBankAccounts", }) : incomingClearingAccountId, outgoingClearingAccountId: params.accountType ? outgoingClearingAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "outgoingClearingAccount" }, backward: "outgoingClearingBankAccounts", }) : outgoingClearingAccountId, bankName: db.string().description("Bank display name"), accountNumberMasked: db.string().description("Masked bank account number for display"), status: db.enum(BANK_ACCOUNT_STATUSES).description("Bank account availability status"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateBankAccountTypeParams> { fields?: NoReservedFields; companyType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; } export function createBankAccountType>( params: CreateBankAccountTypeParams, ) { return db .table("BankAccount", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const bankAccount = createBankAccountType({});