Skip to content

Commit

Permalink
refactor: update customer schema with partialCustomerSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
lncitador committed Aug 30, 2024
1 parent c15323c commit f8e7ec9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
19 changes: 9 additions & 10 deletions packages/core/src/schemas/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/schemas/customers.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 2 additions & 10 deletions packages/core/src/schemas/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
12 changes: 2 additions & 10 deletions packages/core/src/schemas/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down

0 comments on commit f8e7ec9

Please sign in to comment.