import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { outgoingPaymentLifecycle } from "./outgoingPayment.lifecycle.generated"; const builtins = (params: { companyType?: TailorAnyDBType; supplierAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; }) => { const companyId = db.uuid().description("Foreign key to Company"); const supplierAccountId = db.uuid().description("Foreign key to SupplierAccount"); const currencyId = db.uuid().description("Foreign key to payment Currency"); const paymentAccountId = db .uuid() .description("Foreign key to the company-scoped payment counterpart Account"); return { companyId: params.companyType ? companyId.relation({ type: "n-1", toward: { type: params.companyType, as: "company" }, backward: "outgoingPayments", }) : companyId, supplierAccountId: params.supplierAccountType ? supplierAccountId.relation({ type: "n-1", toward: { type: params.supplierAccountType, as: "supplierAccount" }, backward: "outgoingPayments", }) : supplierAccountId, currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "outgoingPayments", }) : currencyId, paymentAccountId: params.accountType ? paymentAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "paymentAccount" }, backward: "outgoingPayments", }) : paymentAccountId, paymentDate: db.date().description("Outgoing payment document date"), totalAmount: db.decimal().description("Outgoing payment total in transaction currency"), status: db .enum(outgoingPaymentLifecycle.states) .description("Outgoing payment lifecycle status"), postedAt: db.datetime({ optional: true }).description("Timestamp when the payment was posted"), reversalDate: db .date({ optional: true }) .description("Accounting date on which the posted payment was reversed"), reversedAt: db .datetime({ optional: true }) .description("Timestamp when the posted payment was reversed"), cancelledAt: db .datetime({ optional: true }) .description("Timestamp when the draft payment was cancelled"), }; }; type ReservedFields = keyof ReturnType | CommonReservedFields; export interface CreateOutgoingPaymentTypeParams> { fields?: NoReservedFields; companyType?: TailorAnyDBType; supplierAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; } export function createOutgoingPaymentType>( params: CreateOutgoingPaymentTypeParams, ) { return db .table("OutgoingPayment", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const outgoingPayment = createOutgoingPaymentType({});