import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { businessPartner } from "./businessPartner"; const builtins = (params: { currencyType?: TailorAnyDBType }) => { const currencyId = db.uuid().description("Foreign key to Currency from primitives module"); return { partnerId: db .uuid() .relation({ type: "n-1", toward: { type: businessPartner, as: "partner" }, backward: "partnerBankAccounts", }) .description("Foreign key to BusinessPartner"), active: db.bool().description("Whether the bank account is available for new usages"), bankName: db.string().description("Name of the bank"), accountHolderName: db.string().description("Name of the account holder"), accountNumber: db.string().description("Bank account number"), routingNumber: db.string({ optional: true }).description("Domestic routing number"), currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "partnerBankAccounts", }) : currencyId, }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreatePartnerBankAccountTypeParams> { fields?: NoReservedFields; currencyType?: TailorAnyDBType; } export function createPartnerBankAccountType>( params: CreatePartnerBankAccountTypeParams, ) { return db .table("PartnerBankAccount", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission({ ...defaultPermission, delete: [] }) .gqlPermission(defaultGqlPermission); } export const partnerBankAccount = createPartnerBankAccountType({});