import { type CommonReservedFields, type NoReservedFields, defaultGqlPermission, defaultPermission, } from "@tailor-platform/erp-kit/core"; import { db, type TailorAnyDBField, type TailorAnyDBType } from "@tailor-platform/sdk"; import { incomingPaymentLifecycle } from "./incomingPayment.lifecycle.generated"; const builtins = (params: { companyType?: TailorAnyDBType; customerAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; }) => { const companyId = db.uuid().description("Foreign key to Company"); const customerAccountId = db.uuid().description("Foreign key to CustomerAccount"); 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: "incomingPayments", }) : companyId, customerAccountId: params.customerAccountType ? customerAccountId.relation({ type: "n-1", toward: { type: params.customerAccountType, as: "customerAccount" }, backward: "incomingPayments", }) : customerAccountId, currencyId: params.currencyType ? currencyId.relation({ type: "n-1", toward: { type: params.currencyType, as: "currency" }, backward: "incomingPayments", }) : currencyId, paymentAccountId: params.accountType ? paymentAccountId.relation({ type: "n-1", toward: { type: params.accountType, as: "paymentAccount" }, backward: "incomingPayments", }) : paymentAccountId, paymentDate: db.date().description("Incoming payment document date"), totalAmount: db.decimal().description("Incoming payment total in transaction currency"), status: db .enum(incomingPaymentLifecycle.states) .description("Incoming 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 CreateIncomingPaymentTypeParams> { fields?: NoReservedFields; companyType?: TailorAnyDBType; customerAccountType?: TailorAnyDBType; currencyType?: TailorAnyDBType; accountType?: TailorAnyDBType; } export function createIncomingPaymentType>( params: CreateIncomingPaymentTypeParams, ) { return db .table("IncomingPayment", { ...builtins(params), ...((params.fields ?? {}) as F), ...db.fields.timestamps(), }) .permission(defaultPermission) .gqlPermission(defaultGqlPermission); } export const incomingPayment = createIncomingPaymentType({});