diff --git a/packages/core/src/schemas/charges.ts b/packages/core/src/schemas/charges.ts index d12cbe5..e953d98 100644 --- a/packages/core/src/schemas/charges.ts +++ b/packages/core/src/schemas/charges.ts @@ -15,7 +15,7 @@ import { invoiceSchema, } from './common.js' import { contractSchema, splitSchema } from './contract.js' -import { customerSchema } from './customers.js' +import { customerSchema, partialCustomerSchema } from './customers.js' import { paymentMethodBoletoSchema, paymentMethodCreditCardSchema, @@ -39,15 +39,14 @@ const subscriptionSchema = z.object({ paymentLink: z.string().optional(), value: z.coerce.number(), status: subscriptionStatusSchema, - Customer: customerSchema - .deepPartial() - .refine(({ myId, galaxPayId, document, name, emails }) => { - if (galaxPayId) return true - if (!!myId && !document && !name && !emails) return true - if (!!document && !myId && !name && !emails) return true - if (!!myId && !!document && !!name && !!emails) return true - return false - }), + Customer: partialCustomerSchema, + // .refine(({ myId, galaxPayId, document, name, emails }) => { + // if (galaxPayId) return true + // if (!!myId && !document && !name && !emails) return true + // if (!!document && !myId && !name && !emails) return true + // if (!!myId && !!document && !!name && !!emails) return true + // return false + // }), PaymentMethodCreditCard: paymentMethodCreditCardSchema.optional(), PaymentMethodBoleto: paymentMethodBoletoSchema.optional(), PaymentMethodPix: paymentMethodPixSchema.optional(), diff --git a/packages/core/src/schemas/customers.ts b/packages/core/src/schemas/customers.ts index 528bae2..af779b2 100644 --- a/packages/core/src/schemas/customers.ts +++ b/packages/core/src/schemas/customers.ts @@ -1,3 +1,4 @@ +import { P, match } from 'ts-pattern' import { z } from 'zod' import { transformArrayToString } from '../utils/transform.js' import { addressSchema } from './common.js' @@ -29,6 +30,25 @@ export const customerSchema = createCustomerBodySchema.extend({ updatedAt: z.string(), }) +export const partialCustomerSchema = customerSchema + .deepPartial() + .refine(props => + match(props) + .with( + { + myId: P.nonNullable, + document: P.nonNullable, + name: P.nonNullable, + emails: P.nonNullable, + }, + () => true, + ) + .with({ galaxPayId: P.nonNullable }, () => true) + .with({ myId: P.nonNullable }, () => true) + .with({ document: P.nonNullable }, () => true) + .otherwise(() => false), + ) + export const createCustomerResponseSchema = z.object({ type: z.boolean(), Customer: customerSchema, diff --git a/packages/core/src/schemas/subscriptions.ts b/packages/core/src/schemas/subscriptions.ts index 3fbfdb4..db74108 100644 --- a/packages/core/src/schemas/subscriptions.ts +++ b/packages/core/src/schemas/subscriptions.ts @@ -15,7 +15,7 @@ import { invoiceSchema, } from './common.js' import { splitSchema } from './contract.js' -import { customerSchema } from './customers.js' +import { partialCustomerSchema } from './customers.js' import { paymentMethodBoletoSchema, paymentMethodCreditCardSchema, @@ -113,15 +113,7 @@ export const createSubscriptionWithPlanBodySchemaBase = z.object({ firstPayDayDate: z.string(), additionalInfo: z.string().optional(), mainPaymentMethodId: mainPaymentMethodIdSchema, - Customer: customerSchema - .deepPartial() - .refine(({ myId, galaxPayId, document, name, emails }) => { - if (galaxPayId) return true - if (!!myId && !document && !name && !emails) return true - if (!!document && !myId && !name && !emails) return true - if (!!myId && !!document && !!name && !!emails) return true - return false - }), + Customer: partialCustomerSchema, PaymentMethodCreditCard: paymentMethodCreditCardSchema.optional(), PaymentMethodBoleto: paymentMethodBoletoSchema.optional(), PaymentMethodPix: paymentMethodPixSchema.optional(), diff --git a/packages/core/src/schemas/transactions.ts b/packages/core/src/schemas/transactions.ts index eae3d32..18e6847 100644 --- a/packages/core/src/schemas/transactions.ts +++ b/packages/core/src/schemas/transactions.ts @@ -12,7 +12,7 @@ import { cardSchema } from './cards.js' import { chargesSchema } from './charges.js' import { invoiceConfigSchema, invoiceSchema } from './common.js' import { splitSchema } from './contract.js' -import { customerSchema } from './customers.js' +import { partialCustomerSchema } from './customers.js' import { paymentMethodBoletoSchema, paymentMethodCreditCardSchema, @@ -31,15 +31,7 @@ const subscriptionSchema = z.object({ paymentLink: z.string().optional(), value: z.coerce.number(), status: subscriptionStatusSchema, - Customer: customerSchema - .deepPartial() - .refine(({ myId, galaxPayId, document, name, emails }) => { - if (galaxPayId) return true - if (!!myId && !document && !name && !emails) return true - if (!!document && !myId && !name && !emails) return true - if (!!myId && !!document && !!name && !!emails) return true - return false - }), + Customer: partialCustomerSchema, PaymentMethodCreditCard: paymentMethodCreditCardSchema.optional(), PaymentMethodBoleto: paymentMethodBoletoSchema.optional(), PaymentMethodPix: paymentMethodPixSchema.optional(),